|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑
1 Q; _# R5 w1 n9 q
3 @- m' _ r5 B! d3 ` ?, H% W直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持
& f- ^7 r3 f9 K: \8 ^
1 g5 F: c" Y: p. f. W0 o我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了/ r$ S$ M7 B! w2 d0 u8 j$ q. H; D
/ u2 C& t9 j8 Q& d1 V I4 q# O4 c方便实用 简单 何乐而不用呢- 6 u1 S1 }0 }- p6 C5 V8 A6 J5 n+ D
- <?php
) d# A) O9 U, E* I0 M$ f/ ^& T
4 t) s- ]& k' ~: t- define( 'ABSPATH', dirname(__FILE__) . '/' );8 X* ?9 }& n5 [/ t' E, g/ b
- require_once( ABSPATH . 'wp-config.php' );
5 k# c7 d& S7 V, j. u$ y* Z - require_once( ABSPATH . 'wp-settings.php' );! e# \+ u1 i/ \$ Q
- require_once( ABSPATH . 'wp-includes/class-wp.php' );+ Y' h b) t5 _% o& [
- require_once( ABSPATH . 'wp-includes/functions.php' );/ J k& {% q# r9 i2 y2 G0 X
- require_once( ABSPATH . 'wp-includes/plugin.php' );- I0 b8 X0 ]$ ~" ]% V
- $title = $_POST["title"];
9 R" G; S( u' a0 p" Q - $content = $_POST["content"];1 ]9 u2 s; w# i
- $tags = explode("_",$_POST["tags"]);/ o, H' K; C8 J9 Q v; |8 X3 A4 D3 W
- $cate = $_POST["cate"];8 J# _- N( p4 @" S; F& _
- //print_r $tags;
& u r# t) L/ p% h - //
- x7 E% k7 g, W0 w% \ - //
/ j# k4 N5 r" r - $wp = new WP();: H8 r/ |" I& f, f' B" v- D
- $wp->main();
" W7 R- F$ o* O3 A' w - //
9 K' Z) g7 D8 \% {1 x6 q - $my_post = array();
3 }' m) e5 A2 P4 E- V, K. [2 B - $my_post['post_title'] = $title;
: d% l7 w f+ B2 j6 V - $my_post['post_content'] = $content;
; @" `. ]' _( M' k- N - $my_post['post_status'] = 'publish';/ n; B) \3 n6 j4 q1 B/ U+ P
- $my_post['post_author'] = 1;0 {3 `/ E& z% a3 M+ s& ]+ g
- $my_post['post_category'] = array($cate);
`. k) ?; a. v! Z# v6 ~3 s: l - $my_post['tags_input'] = $tags;/ J! y: U0 G9 A0 [+ z
- //$my_post['tags_input'] = array('tag1', 'tag2');! J" n& l+ V* s' X6 C0 V
- //$my_post['post_status'] = 'future';5 K/ h6 F S' S3 y, c/ N
- ////$my_post['post_date'] = '2010-07-04 16:20:03';
6 p: x6 I2 c1 s6 d/ r
# n+ R) A3 e$ k7 | ~% ]- // Insert the post into the database
- G, S6 r" D$ \9 e- W- S) d+ J! C: X - $ret = wp_insert_post( $my_post );0 I) ~/ N& ]: q, F/ Y
- echo $ret;8 L, X3 g: Y; o( X; q
- ?>- k; |1 J& C! \2 g9 D( V9 N2 _, E
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气
: K6 a. i1 s% j6 K2 W4 O- <?php
u) J8 Y/ Y% T2 t" Y( X - define( 'ABSPATH', dirname(__FILE__) . '/' );, k- `& {( _* O
- require_once( ABSPATH . 'wp-config.php' );
" ?/ o) |) T, M9 M! B - require_once( ABSPATH . 'wp-settings.php' );
% O" t9 U/ A, [; U% l0 K7 r4 P - require_once( ABSPATH . 'wp-includes/class-wp.php' ); H; M0 v, Q8 h$ t1 w" U
- require_once( ABSPATH . 'wp-includes/functions.php' );9 s" Q% c- q7 H8 q
- require_once( ABSPATH . 'wp-includes/plugin.php' );3 h' f/ y# H( [
- $pic_name = $_POST["pic_name"];8 B) ]. O H* n) W- c8 H* M% E% j
- $pic = $_POST["pic"];
- Z0 l) G) ` j {/ s - if($pic != "" && $pic_name != "")
+ G7 P; l) r/ S( y4 X - {8 ~" F( ?, L/ ~, h6 q
- $path = "images/";
& e& K, a+ y! t! ]# v - $pic_name = $path.$pic_name;$ Z; j$ P; Q; S+ T
- echo $pic_name;
9 i6 I8 F) a: \' |3 k - echo "||";7 C4 L* Q8 J. ~9 o5 l, c1 l, o+ f' X
- error_reporting(0);
( L+ W+ v( D u - mkdir("images");
5 _6 F) c) ~( ?1 A) e - $handle = fopen($pic_name, 'w');: R9 X- e* y# e/ u3 u5 b7 m; J
- echo fwrite($handle,file_get_contents($pic));0 a5 N' n) e3 ?0 s
- }
9 f8 e6 S7 |. j+ e' A - ! F0 y' ?: O2 {" ?1 l
- $title = $_POST["title"];. I# D9 ^ P( k- t
- $content = $_POST["content"];+ x3 U( P* q0 p5 _' q
- $tags = explode("_",$_POST["tags"]);8 Q9 X: O8 u0 p" F# X; P
- $cate = $_POST["cate"];2 [ H" y8 R* }8 i! U
- //print_r $tags;! C1 {+ y) O X( ?
- //. s" P5 d/ l3 K: H6 _
- //
5 p* ?5 d( ~2 U$ x. Z; d9 q1 K - $wp = new WP();
2 S# B% R8 E% W/ K1 Q' L - $wp->main();
: s( x- t5 q2 q O; c7 x1 D3 g - //
' ?8 B9 P8 |( ]$ `# A, |4 O: s" n - $my_post = array();
$ w8 c! G5 S; C+ `7 Q* C - $my_post['post_title'] = $title;0 X, o$ G% J4 f- t/ }9 I+ h1 m, `
- $my_post['post_content'] = $content;8 R2 y) F7 Z$ T
- $my_post['post_status'] = 'publish';: G7 p. t/ K: `' U+ o* Q
- $my_post['post_author'] = 1;% x* }& k& p" C) s
- $my_post['post_category'] = array($cate);0 E( w3 S/ v- P) c& I2 I
- $my_post['tags_input'] = $tags;6 a% Z8 {) ^0 b& K- c
- //$my_post['tags_input'] = array('tag1', 'tag2');6 o4 Y# @* e' i$ A) ~
- //$my_post['post_status'] = 'future';5 Y/ a) m0 `: Z6 m* N
- ////$my_post['post_date'] = '2010-07-04 16:20:03';
2 O+ e, h @; T; `5 K - * u9 Y2 y1 S2 B% c$ w/ _0 D+ v4 C
- // Insert the post into the database# w9 S0 w! {1 a
- $ret = wp_insert_post( $my_post );- R, Z, x d0 D* u0 L. T
- echo $ret;
1 d& X6 r8 o0 m& m+ i9 u, P6 J - ?>) n9 a8 n2 N# L( q! u
复制代码 我自己写的# M: t- ?' G/ d" b8 d
插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|