|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑
6 R- y2 J4 ?5 F: z9 U$ E; ~% ]9 K1 N8 i* _( [# J1 E
直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持
; Q8 ~3 T/ V/ E& {/ Y9 s
; U; G* y6 s# B k/ P U; B% }; [- y$ f我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了3 {% k5 ^$ Q8 `) b& U/ b
( A* ~8 g( R8 X3 R a方便实用 简单 何乐而不用呢
7 [ g0 k* k" H3 W$ R* Y* N- <?php1 J3 n4 m+ ` H/ U
- ! F7 R$ d' Y+ Z, J: o6 F6 z+ m) b) F
- define( 'ABSPATH', dirname(__FILE__) . '/' );
7 c4 Z( Z/ h; N - require_once( ABSPATH . 'wp-config.php' );: z4 S; G0 Y; V0 _3 F# g
- require_once( ABSPATH . 'wp-settings.php' );
3 n9 Z5 ?6 s8 ]2 k' J - require_once( ABSPATH . 'wp-includes/class-wp.php' );( J0 S$ T# G! S! c d9 \3 O
- require_once( ABSPATH . 'wp-includes/functions.php' );, s6 o" Y/ \ ` A& w
- require_once( ABSPATH . 'wp-includes/plugin.php' );. _4 i: }! }! [! s; Y1 g; |( Z
- $title = $_POST["title"];
8 {1 F* y, G9 L - $content = $_POST["content"];
" A3 r6 h; i) T - $tags = explode("_",$_POST["tags"]);; z7 g7 x1 M# ]
- $cate = $_POST["cate"];
( @) d% V/ d/ S- T" O1 n% H' b, k' F ] - //print_r $tags;' e0 f8 u/ T" t1 [ M4 W& o
- //3 I4 f. g: H. X) j& x+ a5 v
- //
6 z+ G( I6 i& N/ e) V* I1 j - $wp = new WP();
# q* }+ ]& }$ ]6 k - $wp->main();
6 Y- o, T$ W1 T! S e: d6 R3 l: f - //
7 _5 \3 ]- z y& a - $my_post = array();/ ~' c7 |& _% z+ ~
- $my_post['post_title'] = $title;; M& S$ [; t# {* b" \+ } t
- $my_post['post_content'] = $content;
# R: Z+ X3 z" Q! o+ @ - $my_post['post_status'] = 'publish';
, K. R0 k K ^ - $my_post['post_author'] = 1;
T5 D9 z2 W( s - $my_post['post_category'] = array($cate);
) }0 ~7 r$ i- |8 i) ~' d# o% Y - $my_post['tags_input'] = $tags;" l, K! V% A# \' ^. }
- //$my_post['tags_input'] = array('tag1', 'tag2');
' I2 Z5 E, K$ |# c8 P( C - //$my_post['post_status'] = 'future';
6 w. c7 w6 f# r- }& C) a: A& j - ////$my_post['post_date'] = '2010-07-04 16:20:03';! n! V' e5 K' E7 y& N F( u2 b! H
- - W# t( I( i# |5 o, Z( H5 D
- // Insert the post into the database. b2 i y3 N" a6 b" `8 p5 J$ u1 I
- $ret = wp_insert_post( $my_post );. U8 Y% z# G" l5 U: Q
- echo $ret;, z7 G/ u3 O( A' b% h
- ?>
$ c; z0 o1 r: n4 g: J! y
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气- 7 u9 u9 L" ]5 }) X E
- <?php; p a% m. t$ c" M- H
- define( 'ABSPATH', dirname(__FILE__) . '/' );) D- c& M/ d+ X! F
- require_once( ABSPATH . 'wp-config.php' );
9 X0 S6 u- w+ q) G, ~3 _) C - require_once( ABSPATH . 'wp-settings.php' );! s* G) B2 W, i8 h. j) N
- require_once( ABSPATH . 'wp-includes/class-wp.php' );
7 S, L- ~6 r) W2 B* r - require_once( ABSPATH . 'wp-includes/functions.php' );, c+ u( _0 t0 u
- require_once( ABSPATH . 'wp-includes/plugin.php' );1 d: |6 J O" v O( w# {" |, f
- $pic_name = $_POST["pic_name"];
; M( k2 \: [; S; o& O& q - $pic = $_POST["pic"];
6 E) m1 b j# K) v" |! ^ - if($pic != "" && $pic_name != "")
8 [4 W Q) I. R0 x1 W" x$ O8 y9 N, y - {
1 [1 h( J. |! c4 L0 u - $path = "images/";4 l' E: g0 y" l5 y( Z0 Q" p
- $pic_name = $path.$pic_name;
3 u' W' L7 T4 n' t - echo $pic_name;
& |7 K) A7 N% Q( }5 d4 B - echo "||";7 z% C, K9 b5 b+ M
- error_reporting(0);
6 F x) y6 p D; ^& u - mkdir("images");
4 h" p8 i/ j$ v+ E& G& O - $handle = fopen($pic_name, 'w');+ l+ K7 ^& \3 c( W
- echo fwrite($handle,file_get_contents($pic));! h. e" ^0 _: p* r; D- j4 t
- }
# h5 P; A% q+ f9 A# S$ S - " k" ^' L( K' ?# k
- $title = $_POST["title"];
2 p1 ]1 r/ f5 o. l: \: Y7 } - $content = $_POST["content"];1 g9 L# m6 a# D% q R; y6 a
- $tags = explode("_",$_POST["tags"]);
1 r7 F& g2 l' Y. W" _2 ? - $cate = $_POST["cate"];
! w& E5 i% Y% u# r - //print_r $tags;! l. p8 X: r8 O8 B
- //
& }9 j" D: d, H! k* M - //
- ~ y) H+ h+ v3 n* E - $wp = new WP();
" E* Q( I- B) t5 x3 W - $wp->main();
5 x6 a l# {! s# V9 O8 F9 {; { - //5 D* `2 k/ W4 a- O0 L
- $my_post = array();+ r/ t8 e5 o6 i
- $my_post['post_title'] = $title;
* P9 R# M* n+ Q: y5 b( l6 K - $my_post['post_content'] = $content;, Z0 \* o, U4 ~( `! h- B
- $my_post['post_status'] = 'publish';
- _, P, i2 A4 Z$ {; @ - $my_post['post_author'] = 1;8 ~6 P, r5 w- v4 n' L' @1 d4 \
- $my_post['post_category'] = array($cate);
' L/ E! O# k4 g7 B - $my_post['tags_input'] = $tags;
4 k0 J) q+ s8 ]3 |/ P1 O* w2 ]+ M - //$my_post['tags_input'] = array('tag1', 'tag2');
5 J8 H5 k# i8 B0 w - //$my_post['post_status'] = 'future';' m$ t2 y, t' ]' Z; l
- ////$my_post['post_date'] = '2010-07-04 16:20:03';
0 W Q: g/ ~" S' u
# L* v6 T. x, I+ D- // Insert the post into the database/ |) u9 m5 J" q( \1 p
- $ret = wp_insert_post( $my_post );
9 \2 s$ `3 {! Q3 n - echo $ret;
- Y3 ?, r1 X i - ?>4 P6 t/ ~5 j1 R8 f# @
复制代码 我自己写的
, O! F' M8 \1 G/ l) O' h$ ?插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|