|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑
/ j$ @: `7 G$ V& q* u
& i q" H0 Y8 `, V* t E直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持1 ^- x$ I5 A# q4 l' F( \8 A
; j/ N5 n( T' K5 |0 Z
我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了
. j2 Z/ G( } j& F4 }2 }- m: m( I' Y* T8 W
方便实用 简单 何乐而不用呢- 4 l; {$ i& L9 i6 L/ |2 ^% W
- <?php: G' t F ]2 E U/ Y
- . [- o' o7 |* U/ t4 U" `8 K
- define( 'ABSPATH', dirname(__FILE__) . '/' );
& t" n3 y8 Z% Z. ]& ~ - require_once( ABSPATH . 'wp-config.php' );
0 V- P2 U! `% e3 ] - require_once( ABSPATH . 'wp-settings.php' );5 b) G# m# S1 |( h$ v
- require_once( ABSPATH . 'wp-includes/class-wp.php' );
$ U% c) n, s: P! P. t9 D u" n) `! S - require_once( ABSPATH . 'wp-includes/functions.php' );
+ n* ^% f2 d& _- B. j- i* m - require_once( ABSPATH . 'wp-includes/plugin.php' );
0 H* o, p/ [, J- g8 |8 Y - $title = $_POST["title"];& a5 c5 F1 G% e# S! D
- $content = $_POST["content"];. K' ^; r5 N+ y: r% E
- $tags = explode("_",$_POST["tags"]);
. h4 A) \! P2 \: h8 J - $cate = $_POST["cate"];! q% w* ?, Y w. P+ u7 c6 f
- //print_r $tags;
1 R8 E& v- F! s7 G; ^ - //( H( {* y9 n1 |, A) ^
- //( F, ]0 d1 x7 S8 t: R
- $wp = new WP();
+ B1 p: g4 H( q' p( s - $wp->main();
4 D2 d0 S T2 w1 { - //
2 a; E6 A& N& } \ j: ^9 r+ C - $my_post = array();
0 Q6 W; I6 }+ n! D$ v - $my_post['post_title'] = $title;6 k6 {5 F7 d/ @8 |" o
- $my_post['post_content'] = $content;) z+ H+ X) v0 p5 u1 `5 M% H' P$ X2 F
- $my_post['post_status'] = 'publish'; }/ e& A! \# O3 f
- $my_post['post_author'] = 1;' U* y% ?3 n7 e7 }) R, O
- $my_post['post_category'] = array($cate);
6 X" k0 a) [! K7 Y1 t - $my_post['tags_input'] = $tags;+ Y1 M: o7 O+ P. q0 d. R F$ G
- //$my_post['tags_input'] = array('tag1', 'tag2');' x5 J. N0 F; h
- //$my_post['post_status'] = 'future';6 }$ ]. |7 L1 o7 w) f1 T4 v( H; W
- ////$my_post['post_date'] = '2010-07-04 16:20:03';
% @7 o& V( S+ r3 v - - q6 F' j- r7 H0 v# l; f( c
- // Insert the post into the database( [9 h! T" x) }1 K
- $ret = wp_insert_post( $my_post );. v8 `3 X" N6 g+ z- Z
- echo $ret;3 u5 b- Y6 q' b! x- a! A4 a8 x+ g
- ?>
0 F9 h, D- u# { n9 X: @, x
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气
' o! W3 `, w1 Y! A- <?php1 T6 g8 i( f" |, T. a- I3 ?1 e
- define( 'ABSPATH', dirname(__FILE__) . '/' );
. p R# @4 D% g5 |' ~8 h+ @% Z- M - require_once( ABSPATH . 'wp-config.php' );8 i4 `/ V9 q6 c# v5 D
- require_once( ABSPATH . 'wp-settings.php' );
/ e( ]" z2 T2 |2 d9 s2 h - require_once( ABSPATH . 'wp-includes/class-wp.php' );
- h" _1 c. [$ y2 c ]! d - require_once( ABSPATH . 'wp-includes/functions.php' );9 t+ O% w. S9 d [: D+ V0 d! A
- require_once( ABSPATH . 'wp-includes/plugin.php' );7 M: J0 T8 ~. x @" _% m
- $pic_name = $_POST["pic_name"];) m4 Y! H; O* p5 O3 F$ b
- $pic = $_POST["pic"];- F/ g( W% M3 p. H( X
- if($pic != "" && $pic_name != "")& Z+ ]) i) F N f4 F6 L% |1 c6 B
- {
( {% A$ Z, p* ^9 u+ M/ W& b) U - $path = "images/";
) C W4 u! z8 q5 X( r - $pic_name = $path.$pic_name;
2 y, ~' Q7 o! \+ v - echo $pic_name;
: ~- h8 W1 a. q0 Q - echo "||";
* H. t7 S7 p5 M7 h5 c7 \' { - error_reporting(0);
! n3 n" V7 n6 p9 G+ y" R: x - mkdir("images");
0 p$ S6 Z! T/ c$ s/ l/ E, N - $handle = fopen($pic_name, 'w');
- z+ U3 q* W: }. O4 Y - echo fwrite($handle,file_get_contents($pic));
1 ?3 g4 b$ u8 t1 ~- x9 q8 t8 v - }' [( Q! ]8 M! \9 t/ }3 v
- " Y7 Q* F3 z* ^3 ?
- $title = $_POST["title"];2 y9 p* I+ D2 E. H
- $content = $_POST["content"];, t/ W% G' T8 w0 j6 Z
- $tags = explode("_",$_POST["tags"]);
: s& i; g9 I+ v- o+ s W9 n) x% A - $cate = $_POST["cate"];: R8 G `. `9 a4 S2 }0 B
- //print_r $tags;
, }6 O6 ^# w U - //1 ^, K" W* Y$ y- p8 z; `
- //
5 C1 N3 o8 K( ^7 i* `# R0 v - $wp = new WP();
* l' z9 i. A% Q: L' P+ c - $wp->main();
- C8 O' x+ B5 |5 _' t6 ^ - //
: }( H! v* c, y M5 X8 t - $my_post = array();/ p/ h, F5 k9 G$ k8 J$ u
- $my_post['post_title'] = $title;8 O6 S) F; B) E+ i. l
- $my_post['post_content'] = $content;; J6 p* G2 Z" h. Z
- $my_post['post_status'] = 'publish';# T* {3 k1 b( q% z
- $my_post['post_author'] = 1;: l9 M( J8 Q; _: l3 m* `
- $my_post['post_category'] = array($cate);4 l+ n6 R& f) ~
- $my_post['tags_input'] = $tags;
! n Y5 v2 A3 c, |% \% O - //$my_post['tags_input'] = array('tag1', 'tag2');' W0 F' v" O5 H" X' K+ W
- //$my_post['post_status'] = 'future';
3 m" ^- v, x( {; a5 X$ O - ////$my_post['post_date'] = '2010-07-04 16:20:03';
# L5 @) p# ^, Y! X3 j
- ~4 W4 k7 F1 _0 f, P+ D- L- // Insert the post into the database
9 M% H3 b" Q( \+ G) Q - $ret = wp_insert_post( $my_post );
|9 A \2 \ N4 ^ - echo $ret;( _3 v/ \2 o1 V r( h
- ?>! Z5 P9 h$ [, L. D( S Z
复制代码 我自己写的9 y U4 Q/ K( k
插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|