|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑 / l8 ]: F, x2 B1 h% J
0 B- [/ N. a+ s3 I0 ~/ H7 x直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持5 i9 A. B+ |: S: Q% \5 w& m
# b2 T1 S- i( `
我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了/ l# u |' _& x, o' T8 v
4 }7 E, p w0 V. }7 ?3 {# J方便实用 简单 何乐而不用呢- . Z( \' B: D3 d+ M* a& o3 c
- <?php$ J1 ^. o& D4 K' N
- / W+ H O/ v/ f, X" L2 r3 F7 |
- define( 'ABSPATH', dirname(__FILE__) . '/' );
3 M( f( `# T6 h - require_once( ABSPATH . 'wp-config.php' );
2 B: \7 ?7 ]' f6 G0 q7 R5 M - require_once( ABSPATH . 'wp-settings.php' );2 R" x y. t0 `8 w" w
- require_once( ABSPATH . 'wp-includes/class-wp.php' );
1 W6 M5 p. r* r - require_once( ABSPATH . 'wp-includes/functions.php' );! B2 @4 T F! f' j8 B
- require_once( ABSPATH . 'wp-includes/plugin.php' );
( j7 e. l3 z* X/ |. d+ p( P% @ - $title = $_POST["title"];
0 @- J+ n- m$ @& N, A1 M - $content = $_POST["content"];
) ^2 V) N0 @0 L - $tags = explode("_",$_POST["tags"]);
) |: z# K; ]2 d6 ~; p - $cate = $_POST["cate"];( J* [4 ]; m( P' ]
- //print_r $tags;
8 G, G6 k9 K7 x1 u7 c# Y - //- D, @3 Y ?+ p0 e, H/ m
- //6 B, K5 n3 z* `. F. y+ t6 `- z n
- $wp = new WP();% g2 X4 ^% B1 u0 r9 Y
- $wp->main();* v: G+ c. f6 i% A( _6 e
- //
# Z% A X" t' Z - $my_post = array();
# c% {+ C. e0 @# k - $my_post['post_title'] = $title;
. Z% [/ U. I5 q6 ?$ D - $my_post['post_content'] = $content;1 `/ B y7 d( I" I! p
- $my_post['post_status'] = 'publish';
# D! G* C$ N- K+ l2 q. @ - $my_post['post_author'] = 1;/ h& J. l9 f, k+ R0 @# z
- $my_post['post_category'] = array($cate);
5 s: o3 t/ D8 W0 E - $my_post['tags_input'] = $tags;( T3 e) }) y: @
- //$my_post['tags_input'] = array('tag1', 'tag2');- v1 x5 u. Q) u6 k4 K/ H2 r
- //$my_post['post_status'] = 'future';8 Q) f# O" F% Z9 ?& O0 C
- ////$my_post['post_date'] = '2010-07-04 16:20:03';
" j( A2 C4 m2 G - " h4 ^: O- @0 q* A4 d' \
- // Insert the post into the database R# A: Y# _3 E* I& A
- $ret = wp_insert_post( $my_post );& |% R9 K, Q7 K
- echo $ret;
1 q1 G m" [ f5 ? - ?>
+ m, Z3 N+ p: \) W0 L
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气- 9 y/ ~; V2 A: y; q1 U& @
- <?php& s: Z7 f) I' Z" c9 C2 C6 z3 Q
- define( 'ABSPATH', dirname(__FILE__) . '/' );2 _5 Z5 ^( d0 |/ X
- require_once( ABSPATH . 'wp-config.php' );: z' h3 q% i4 O! [& m- Z8 s
- require_once( ABSPATH . 'wp-settings.php' );
% c% `: C: n# [6 q6 O& n" s! Y - require_once( ABSPATH . 'wp-includes/class-wp.php' );
; }5 D! W3 P A8 U/ n2 Q, R, c3 O* G - require_once( ABSPATH . 'wp-includes/functions.php' );* V% }6 R2 U) Y+ V' `' W
- require_once( ABSPATH . 'wp-includes/plugin.php' );( R% n! c; o6 {4 o
- $pic_name = $_POST["pic_name"];
* ^8 z2 [. ~1 t* e! {; r - $pic = $_POST["pic"];" @# v% p$ h0 B" @
- if($pic != "" && $pic_name != "")' B% }! A* n7 m: [* i# M0 D
- {4 L, a9 E' z5 _ ~* z' V
- $path = "images/";: a; R6 N% g" h4 y4 l: v7 G9 q
- $pic_name = $path.$pic_name;
( r& Y9 Q2 i5 T6 U, d - echo $pic_name;6 f: z0 I$ v T. g" j0 c* _8 ~
- echo "||";
$ U `% P7 r# m4 ^ - error_reporting(0);( a& t" T6 @( [) ^! q' x
- mkdir("images");7 o7 Q5 I# T; a
- $handle = fopen($pic_name, 'w');
: Y. {4 V4 N$ x - echo fwrite($handle,file_get_contents($pic));
7 d# d* y3 z o" d( g - }8 \- i0 ^- a7 J: t3 N9 ]
- " X) j4 T$ A1 R! e! I3 _7 `; n% j" M# A
- $title = $_POST["title"];
+ p. G# |* b7 M) ^- Y, U0 C+ ~ - $content = $_POST["content"];
' _# s5 w& [6 x* s - $tags = explode("_",$_POST["tags"]);
0 |* u# @% W% m+ |! E( h( j0 Z - $cate = $_POST["cate"];1 K5 b: j- c4 v2 i, I6 {$ x% G) {/ i( O
- //print_r $tags;' _: f. I: j$ A* ^
- //
+ F' `$ L8 v" A - //. I* b, u$ n q! N% ~6 f% p
- $wp = new WP();% Y2 ~, c- o0 L& v! Z- u6 F, s
- $wp->main(); h2 v9 F3 F* V+ f5 d, ~
- //
% { Q2 P h* t, q7 h4 S g - $my_post = array(); j1 A5 B3 n$ }) ~
- $my_post['post_title'] = $title;1 M3 s; I1 y/ e# R
- $my_post['post_content'] = $content;
# w1 R2 [" J1 \7 {' ? - $my_post['post_status'] = 'publish';$ d5 Q" b5 a2 y: r* ?
- $my_post['post_author'] = 1;+ B$ J2 n/ e# U0 D9 @
- $my_post['post_category'] = array($cate);- R9 ^8 ]( S" R' {% ^ U
- $my_post['tags_input'] = $tags;3 ~- S5 `- l4 }5 B
- //$my_post['tags_input'] = array('tag1', 'tag2');
3 L0 A0 o, p1 ?+ H0 O1 C" u - //$my_post['post_status'] = 'future';
9 B! m$ D) i- g - ////$my_post['post_date'] = '2010-07-04 16:20:03'; h1 L$ {& ~& M) ^
- & A* _5 R" I( v4 k8 Y/ T
- // Insert the post into the database
3 d7 F. X4 [9 l - $ret = wp_insert_post( $my_post ); M& o# K' n' N! p
- echo $ret;
/ Y6 a) ]! D9 Y- w; u6 O - ?>
' t! [0 [# y+ {( G6 F
复制代码 我自己写的
- Q. d! D( R6 S5 ?插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|