|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑 8 y' Z/ g& G) q' B& H
: Y6 _3 x$ ^6 n7 b M4 Z- b& `直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持" W' {8 A6 s9 Z) _7 ^
?. ?2 V& t i5 |4 Y3 v
我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了9 L/ Z4 }. I7 h$ v
" q+ |7 \+ y4 T. C
方便实用 简单 何乐而不用呢- u4 H+ N: Z, Y
- <?php
1 @6 W R" h+ H F+ r0 R; {3 g - * S+ P' [ `; }+ b, E* U9 Z& a
- define( 'ABSPATH', dirname(__FILE__) . '/' );
* J5 y* \* ~: L( V; } - require_once( ABSPATH . 'wp-config.php' );
% |+ T" x( Z' Y, }; h - require_once( ABSPATH . 'wp-settings.php' );
$ k& A p% T" Y& k& q( _; w) Z9 ~ - require_once( ABSPATH . 'wp-includes/class-wp.php' );
) L" ]. m$ [( C - require_once( ABSPATH . 'wp-includes/functions.php' );7 V/ k* O) z$ j; f6 y
- require_once( ABSPATH . 'wp-includes/plugin.php' );0 h- z% \( D% z) w
- $title = $_POST["title"];
2 @1 C8 a! R4 z( L1 e8 x0 x - $content = $_POST["content"];
6 n7 f7 z" ^ `. `" A( f - $tags = explode("_",$_POST["tags"]);
% h3 m3 d; w4 c1 S( T/ O( X - $cate = $_POST["cate"];0 z1 t q4 Q9 w; Q8 `- e$ f& i
- //print_r $tags; E. I$ n i/ v
- //
/ l( N3 G' c: i+ _) g4 J - //4 h$ N1 o. L1 S% y0 m1 u1 E/ V; G; u
- $wp = new WP();; Q! G8 P# X4 Y0 l6 ~+ g
- $wp->main();/ l Y6 ^; Z( X5 A
- //
4 W1 R4 @' d7 U9 [ - $my_post = array();
) l* x- D, w) I; U% D - $my_post['post_title'] = $title;. E5 J( f z, P3 B3 f4 ~ E
- $my_post['post_content'] = $content;
0 p( m J6 m' G- G - $my_post['post_status'] = 'publish';/ |6 T* L9 q- Y) U- M! U
- $my_post['post_author'] = 1;+ Q; L2 t P) l0 r3 k
- $my_post['post_category'] = array($cate);5 h; e0 F4 v4 o
- $my_post['tags_input'] = $tags;: W/ m1 Y2 O4 ~% K' ?8 x: X3 X
- //$my_post['tags_input'] = array('tag1', 'tag2');
5 C% W$ k! z* |/ ~; ?% Y, E( H - //$my_post['post_status'] = 'future';% n8 Z {6 }6 j# a, b! d! m& }
- ////$my_post['post_date'] = '2010-07-04 16:20:03';% }. V$ V, n$ n. }, P
- $ x5 o3 [. q- y5 r' s, E8 w
- // Insert the post into the database
& c* {- E1 ]1 w( f I2 y$ S- m - $ret = wp_insert_post( $my_post );* {* c3 Q" |1 [5 o+ e7 ?
- echo $ret;5 i" s1 i+ F5 Y2 Q* L
- ?>( ^! T4 n* _5 ^# N% ?1 _! f
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气- " p5 w. u( S% ~
- <?php' X" t* Q/ E! R4 m
- define( 'ABSPATH', dirname(__FILE__) . '/' );
, W7 e. A5 a4 M9 f% e7 } - require_once( ABSPATH . 'wp-config.php' );
' Z2 B9 t) d" _, ~ - require_once( ABSPATH . 'wp-settings.php' );3 G1 |7 W9 V4 ^0 q* i a
- require_once( ABSPATH . 'wp-includes/class-wp.php' );
9 i! l2 \5 a0 P P/ } - require_once( ABSPATH . 'wp-includes/functions.php' );
+ x* H3 Y) g) s! N: j% F2 z/ z( j+ X* a - require_once( ABSPATH . 'wp-includes/plugin.php' );
0 |- n* Q$ T0 t4 N - $pic_name = $_POST["pic_name"];
. G3 ^6 T; Q' | - $pic = $_POST["pic"];$ G4 L& n% v" z1 v4 p' r$ b, r
- if($pic != "" && $pic_name != "")# ]8 b) ~* P6 w" U+ ?% K8 F6 D
- {* k* |2 B' l: |' n( Z. V
- $path = "images/";
: q( W8 T5 o; _1 u7 Q0 | - $pic_name = $path.$pic_name;# ^2 g# d+ C1 }7 X4 Z% s
- echo $pic_name;
) ~5 l+ K/ }" b; V4 T - echo "||";. [3 U. t5 p* ~1 I' {
- error_reporting(0);
+ N, j* w; j( L# N) K - mkdir("images");! |' x- ^- G8 n- M. c" `$ M) O
- $handle = fopen($pic_name, 'w');
$ K8 b5 ^) z( M; O4 [. w - echo fwrite($handle,file_get_contents($pic));% U+ F* s3 `$ N- P& ]
- }
9 Y2 S8 H7 I# Y: z
/ D, F% @9 L3 F& ~6 }, \6 O- $title = $_POST["title"];
" w% [. H4 e4 P! y7 A# I - $content = $_POST["content"];
+ |7 U* P$ }' x7 ~. N3 ~ - $tags = explode("_",$_POST["tags"]);5 R5 b$ `7 ]& W: z
- $cate = $_POST["cate"];! N. S+ U. k( R
- //print_r $tags;; \. L v' ~" y9 o9 k3 q0 I( C7 f; G
- //
% T! |# J$ c3 T. G) J) K4 B - //
/ b3 P$ v- m9 b; E! p3 E6 C, \ - $wp = new WP();
5 C* a& X0 T! r+ r1 b2 p" i( V - $wp->main();
+ Y- n6 _- Z% e - //" |" @8 x p* v! ^
- $my_post = array();
$ e/ F9 X. M. P3 w9 G) U - $my_post['post_title'] = $title;& I5 m/ l: q% M4 Z9 {1 Y. k- ?
- $my_post['post_content'] = $content;7 w' A5 k+ H; S; E |. i3 ], A3 T
- $my_post['post_status'] = 'publish';
) s) Y6 v5 i; M p- _+ z' n+ p; d( l - $my_post['post_author'] = 1;
4 ~# q6 {( @# {( b - $my_post['post_category'] = array($cate);/ v& c8 k* R: C9 K; Z
- $my_post['tags_input'] = $tags;
/ K9 i; b, N2 d - //$my_post['tags_input'] = array('tag1', 'tag2');
# U# z* ]0 y" N* @9 a - //$my_post['post_status'] = 'future';& I4 c9 z. |+ K
- ////$my_post['post_date'] = '2010-07-04 16:20:03';: Z. K5 N6 `3 @# M6 U
7 G# w7 A, I3 w. O0 `& j7 |- // Insert the post into the database
5 ?6 ], S1 ~6 F9 k/ I x0 }6 @ - $ret = wp_insert_post( $my_post );
/ c' s; ?5 l$ P1 ` - echo $ret;
9 n" o {+ x1 P2 J+ c - ?>4 l, F7 ^/ f6 ~+ Q1 Y4 |
复制代码 我自己写的
7 H7 Z1 ^* h& T' n8 e插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|