|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑 2 d2 T5 q" r8 p/ U# _4 g
/ C8 c4 Z. } A5 p2 @
直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持5 M) }3 o# C/ F4 p' i
- d' S6 r3 ~+ e% w9 A, b
我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了
% Y( Q' ^5 H. r- L/ M
, |1 o$ p7 T. y/ p0 t5 H& B方便实用 简单 何乐而不用呢
) \6 F: l3 q' G2 C( o6 [- <?php! n! e3 W5 D/ a2 H5 u' C) @/ @
- - R0 D" m0 Y* y1 K
- define( 'ABSPATH', dirname(__FILE__) . '/' );% V- I# Q; G5 y9 H3 m
- require_once( ABSPATH . 'wp-config.php' );
: k7 E/ r: J; |8 r' w/ G) T0 i - require_once( ABSPATH . 'wp-settings.php' );8 j! |" D$ a* I4 ?4 y( ~. ~
- require_once( ABSPATH . 'wp-includes/class-wp.php' );
4 ^( O$ R( ]5 D( v k0 l7 ^ - require_once( ABSPATH . 'wp-includes/functions.php' );. @5 F4 e' n! b! d9 P( W1 [: e. n
- require_once( ABSPATH . 'wp-includes/plugin.php' );3 ^, U) h) o+ n5 c$ ~0 p- W
- $title = $_POST["title"];' G9 p) `! x5 `, C( H4 F6 N9 n
- $content = $_POST["content"];1 e" y- u0 H* X+ F% i7 H8 N
- $tags = explode("_",$_POST["tags"]);
0 [3 f& q- t; i4 ~ - $cate = $_POST["cate"]; l0 L. m* O5 {9 f7 F
- //print_r $tags;
. C3 d G2 S4 z& M1 m - //( s9 k2 J, I8 i) u. z
- //
3 g9 k; }+ y' n" I! c - $wp = new WP();& M. A4 ~- Z. B, Y% d9 }
- $wp->main();
2 ~. _4 }$ t+ j( N7 }( Z - //5 E3 p" t4 Y/ U7 g9 }. n6 k
- $my_post = array();
+ K6 b" T. O7 O# o$ u" F+ _ - $my_post['post_title'] = $title;! w9 `$ U3 U4 c1 ^
- $my_post['post_content'] = $content;
- _' B0 J2 t1 p7 |' n! g5 Y - $my_post['post_status'] = 'publish'; U: s6 q% q3 I
- $my_post['post_author'] = 1;
$ D. G( p5 U2 I - $my_post['post_category'] = array($cate);
7 W" B/ B1 o/ i$ N5 V - $my_post['tags_input'] = $tags;% b% q1 i! ^" ?' K- i$ P
- //$my_post['tags_input'] = array('tag1', 'tag2');
- G! U+ L G" T2 [' q - //$my_post['post_status'] = 'future';3 t( h l+ p* l; g( I0 b" M w) s" ^! D
- ////$my_post['post_date'] = '2010-07-04 16:20:03';
\3 ?: [8 B9 `0 A: x' X" S
3 ^3 ~$ u9 s& S8 _1 c# P }- // Insert the post into the database, ]7 s/ i$ b: y' |7 P) {: Z
- $ret = wp_insert_post( $my_post );6 y& c& b' _# _& o( j8 F
- echo $ret;2 J" B9 h. l) B9 \6 t
- ?>1 }: C7 K4 \$ m' X5 D( b
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气
# z, R1 |0 q' B. l1 C) f. o7 y5 t- <?php$ @3 t& k/ L7 e0 B8 F/ O+ P% p
- define( 'ABSPATH', dirname(__FILE__) . '/' );
) W. t6 @. G9 P; b$ u - require_once( ABSPATH . 'wp-config.php' );# n/ |( X( p% Q6 X0 }! ]5 m
- require_once( ABSPATH . 'wp-settings.php' );! N$ t9 ^6 v, X6 h
- require_once( ABSPATH . 'wp-includes/class-wp.php' );$ T$ ~$ t8 b8 H7 [! T% W
- require_once( ABSPATH . 'wp-includes/functions.php' );9 t7 ^( s- {1 c" X3 q: T, O
- require_once( ABSPATH . 'wp-includes/plugin.php' );. k! f# S: _; ~# n
- $pic_name = $_POST["pic_name"];
# f) H2 m. T1 H; ?- b: B - $pic = $_POST["pic"];
9 _! E/ [9 G2 }, Q4 F/ Z - if($pic != "" && $pic_name != "")( H. I, Z1 N+ J* ~; I9 H0 [/ Q
- {
; q7 N* v Q+ Y9 c& H+ h - $path = "images/";
, p( T! m- @1 w# r5 W, p I - $pic_name = $path.$pic_name;9 M5 U6 {! u, w9 o: M, [, k' x6 Y3 U
- echo $pic_name;
% G& A. m$ [& q, J- [1 f6 @/ F - echo "||";% ~# g& R% b4 f; d' E6 f
- error_reporting(0);
8 h6 I w. D7 ]8 W - mkdir("images");
3 e; p2 m$ P+ _+ ? - $handle = fopen($pic_name, 'w');# K1 [. [+ E5 [7 _% _1 b9 D
- echo fwrite($handle,file_get_contents($pic));* \; l/ z' o5 b4 I% E+ L5 t# J
- }
$ H8 b: U3 S7 k& S - 6 P) d8 }; E8 U3 E
- $title = $_POST["title"];
+ p; U0 g6 n4 v7 a" G) k& j8 Q - $content = $_POST["content"];; S6 h# h/ e: }0 l% l) E
- $tags = explode("_",$_POST["tags"]);
" k* N1 r8 e4 ~$ a, ~, e1 _ - $cate = $_POST["cate"];
1 n. Q6 z0 D9 U" l9 E - //print_r $tags;
0 ?% Z+ K( D1 s/ C - // u: L" v9 V0 j. t, z0 ^% Q8 Z/ I
- //# |7 |/ p; z$ \! [( d9 W
- $wp = new WP();
* K! e. @; o" G - $wp->main();+ `; E- U5 _0 b8 D
- //
$ O0 H% ~, T- A2 C - $my_post = array();# b0 A" V0 d" z' h) c u
- $my_post['post_title'] = $title;
' t2 o. p; B" T- ~; w - $my_post['post_content'] = $content;. `2 u& c2 t/ f$ E r
- $my_post['post_status'] = 'publish';
+ L: i. V! B- B9 k4 L- F - $my_post['post_author'] = 1;" ]8 Y. B0 T% ^5 O! M/ v
- $my_post['post_category'] = array($cate);
3 S3 R3 b8 U* o2 i6 o - $my_post['tags_input'] = $tags;( D% E) A+ l1 p$ E
- //$my_post['tags_input'] = array('tag1', 'tag2');' B* B( e; s1 A7 n3 A$ E/ E# }
- //$my_post['post_status'] = 'future';1 ~7 A0 {( M& Z% W @
- ////$my_post['post_date'] = '2010-07-04 16:20:03';
+ N3 c# U {6 z. j$ {6 p
_6 `7 z. Z- r/ `5 |; u' s- // Insert the post into the database
+ i/ ], ?. M5 w0 `8 g, F - $ret = wp_insert_post( $my_post );
- T* l4 X" T# i4 O1 ^4 H - echo $ret;7 Q& S0 m( G9 f4 C6 n
- ?>
) x5 _/ C7 M6 }. `* f
复制代码 我自己写的. n# w8 I9 `& p/ |/ I
插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|