|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑
! V' L$ r9 A/ ^/ f8 L" z, J. q# v8 O( k5 |# u& Y2 c. Q" H
直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持, h7 [* W: P* o; L) ?# l1 o
7 j4 o3 S1 F2 W' p) b4 `
我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了
& |" o R2 N) ?+ E
3 p. K9 c0 e q A s方便实用 简单 何乐而不用呢- & j; U* J! Y. h1 a! V7 ~& M
- <?php9 m& U, G( Z/ w: D, j
3 E* m# b5 V5 x9 N- define( 'ABSPATH', dirname(__FILE__) . '/' );" P3 s2 K2 I* N2 g
- require_once( ABSPATH . 'wp-config.php' );
8 X7 R3 {( {( M - require_once( ABSPATH . 'wp-settings.php' );, \' }" ?# h j8 l }' H
- require_once( ABSPATH . 'wp-includes/class-wp.php' );2 V& z3 `* O" D0 P; u! X
- require_once( ABSPATH . 'wp-includes/functions.php' );& m" G% J. E. Q }
- require_once( ABSPATH . 'wp-includes/plugin.php' );1 m! t$ l% i" \$ K/ c# e, S5 n5 ]
- $title = $_POST["title"];
! R+ G/ @% |' X) T% Z - $content = $_POST["content"];
! @! C! P6 o9 C0 k: Y+ r; C - $tags = explode("_",$_POST["tags"]);: m3 l7 g G: K7 d7 z
- $cate = $_POST["cate"];
0 Z1 V) P/ H* D" F3 C8 q; i- G - //print_r $tags;
g2 @# N- ]& U' H - //
) ~7 L5 M# }) |" b% c" B1 g - //- b5 e" [: G7 A1 f( d6 D; |: L
- $wp = new WP();+ K, R7 N* V4 |/ Z# ^
- $wp->main();: ^8 e9 W9 {& M
- //
7 b) [- l: ?) L6 g - $my_post = array();
) x6 K6 p, l5 x, S7 \$ c+ ~' @' y - $my_post['post_title'] = $title;
$ l. d' [& E9 P. m' W - $my_post['post_content'] = $content;
& i* y& R0 Q' W. \ - $my_post['post_status'] = 'publish'; b! ~: N3 w' q, a- P6 c4 g
- $my_post['post_author'] = 1;# H* M; C( b2 }% k9 |
- $my_post['post_category'] = array($cate);2 g& b7 K( Y. _. S3 K& N5 ]' z
- $my_post['tags_input'] = $tags;9 K" x" r+ `# n% m- Y" v$ j" |) q8 x
- //$my_post['tags_input'] = array('tag1', 'tag2');
, @# E. [0 e- e - //$my_post['post_status'] = 'future';
, [, U4 y) S: ~" B! ~ - ////$my_post['post_date'] = '2010-07-04 16:20:03';
9 j! Z3 B6 |9 r0 M - " u" e7 `6 [6 Y! B8 H [
- // Insert the post into the database' y2 _- O- F( ?6 U, v. d
- $ret = wp_insert_post( $my_post );: q& p' n8 ~9 ]- b1 i& {
- echo $ret;$ I5 u9 V5 M7 L! v6 l! b5 l
- ?>$ C }( F% h8 g0 _6 r: p% q* d
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气
' u1 [& _) d; f, L- <?php$ a7 `. _- J# _7 C8 ~
- define( 'ABSPATH', dirname(__FILE__) . '/' );
* H" M) |( v/ F$ R$ S - require_once( ABSPATH . 'wp-config.php' );
% ~6 `+ c6 R" K, a2 T. c3 N - require_once( ABSPATH . 'wp-settings.php' );
+ {. Z8 k4 v* Z$ m+ O - require_once( ABSPATH . 'wp-includes/class-wp.php' );, \; y; h$ e- \# M
- require_once( ABSPATH . 'wp-includes/functions.php' );
# Y1 `1 O: t4 E: m2 e! }! P" P - require_once( ABSPATH . 'wp-includes/plugin.php' );5 g+ E* z% `" N" V# ]# T4 S+ r1 U
- $pic_name = $_POST["pic_name"];9 t4 G' t# I U% W& {3 x( C6 ^
- $pic = $_POST["pic"];' ?" t' F. ^) p; k: M
- if($pic != "" && $pic_name != "")
0 J, q- t9 k' y0 r - {
2 I+ v+ v3 q; J - $path = "images/";
$ |* U! [# G8 _1 i) _ - $pic_name = $path.$pic_name;
7 z6 r7 D, d; o. k3 J - echo $pic_name;4 c) k u* m& i' S4 q0 `5 w7 z! c
- echo "||";, J2 s9 o% s( Q* D9 h, K
- error_reporting(0);1 d+ b; o+ `2 X! O) |. Q+ S) g
- mkdir("images");+ h2 [& P9 Y5 i2 A
- $handle = fopen($pic_name, 'w');. G! u! ]4 t/ ^# y9 v5 {
- echo fwrite($handle,file_get_contents($pic));: q# h, e% \" ]1 P$ n- R
- }
% A& e4 I; r( P# g/ a
% `" ^ G& q: u8 r$ u- $title = $_POST["title"];0 W! Q9 m: L6 Q5 G
- $content = $_POST["content"];
. a/ E- ~6 D8 e( [0 ]1 c - $tags = explode("_",$_POST["tags"]);
7 W0 M, s# j# w% S% F& U - $cate = $_POST["cate"];3 O8 M# V+ n9 ?9 F
- //print_r $tags;4 k' w. W. ~- b2 }
- //
8 e5 h% ^& H% w* b( T' s - //
5 S! m$ `) Y( e$ P& Z1 D: [) v2 l - $wp = new WP();
7 ^) i* B! q4 H$ n) A- O8 d - $wp->main();" ^" |6 E$ `: Y! A
- //* q5 E! t2 D6 R# y* e- ~( G; }
- $my_post = array();+ j! [0 o3 `0 d. c5 T# r
- $my_post['post_title'] = $title;
$ M% ?$ I: ]% O X# h - $my_post['post_content'] = $content;' s& F/ N9 h9 T) r
- $my_post['post_status'] = 'publish';
3 |9 N2 T& G# L1 X - $my_post['post_author'] = 1;
. l8 X* \( ^. |" o5 Z - $my_post['post_category'] = array($cate);
, L! z* N- K' J6 |' G - $my_post['tags_input'] = $tags;
% R4 E8 M" |* o - //$my_post['tags_input'] = array('tag1', 'tag2');
% s& a% [1 G4 N$ W1 S+ u - //$my_post['post_status'] = 'future';
2 d( I9 F* y E3 @8 d - ////$my_post['post_date'] = '2010-07-04 16:20:03';! G- u7 c7 G9 \5 q' j7 Q% I2 u0 R
) n: r9 C% C0 l7 X5 Z4 W4 V- // Insert the post into the database
* t) J$ _4 c4 V. l6 W - $ret = wp_insert_post( $my_post );
G, l. D2 V: f0 ~8 l - echo $ret;; p: H! B) {) L7 X! ?/ D; q B% A
- ?>
2 d9 Y* s+ J8 T$ {; U' a
复制代码 我自己写的
0 J4 b' q4 C$ [! n* m+ N插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|