|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑
: d! H: E! Z. q: m; T9 t
* m# ~6 G, L& J9 |+ E, q直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持& Z+ F- v( B! t
: B+ m! m, R. q& d+ v# J
我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了
9 k( Q) T. e9 h$ h5 z6 P: a4 h+ @- {
3 ^9 `5 f9 h: L$ j) P1 H方便实用 简单 何乐而不用呢
. A% g: q [- N+ T6 i- <?php
/ ^! _6 F! l& ?' o1 u4 I1 I
% h1 j7 P: i4 g0 v% M) k5 T- define( 'ABSPATH', dirname(__FILE__) . '/' );
4 s! m) Z5 [" W! Q1 c2 b - require_once( ABSPATH . 'wp-config.php' );% m" r7 |; m5 i& P1 e5 U2 V: w
- require_once( ABSPATH . 'wp-settings.php' );
2 z7 `. T. Q& o! S - require_once( ABSPATH . 'wp-includes/class-wp.php' );
8 o. k0 v9 c* @" v& w5 v8 y - require_once( ABSPATH . 'wp-includes/functions.php' );: [& ~6 e: p7 K0 J2 }
- require_once( ABSPATH . 'wp-includes/plugin.php' );" t5 q* l7 D; P) k7 A
- $title = $_POST["title"];
3 o4 ]0 i, }3 h3 W9 d - $content = $_POST["content"];
0 g. }+ A( |% `8 C% B* ^ - $tags = explode("_",$_POST["tags"]);
. O% ~( o$ z7 A' n - $cate = $_POST["cate"];# `7 q, j7 s- U4 i& d6 s2 r
- //print_r $tags;
5 Y; I9 O& H, w4 \4 p& a' e - /// _1 n8 N9 m d' W- h/ O
- //; w2 D* f8 d* }
- $wp = new WP();/ \* L$ C6 i- H- H4 K$ K
- $wp->main();5 S, n" F/ F) d, v: c+ W0 {
- //4 X ?9 ~1 e6 e( `
- $my_post = array();
2 ]8 j/ }5 {: I) t& ?0 X - $my_post['post_title'] = $title;
3 }. R) ^' P, `4 f8 b# |+ b - $my_post['post_content'] = $content;
9 F3 p, d0 [, d' y+ e+ h" D - $my_post['post_status'] = 'publish';
) A* w% ?. l# z+ A - $my_post['post_author'] = 1;
8 u, p* U1 @8 u- G - $my_post['post_category'] = array($cate);
% T) Y, Q! v0 j% E9 f1 l, o0 o3 } - $my_post['tags_input'] = $tags;
% D8 s" m4 @9 L1 S, x - //$my_post['tags_input'] = array('tag1', 'tag2');) P; }! H* ` i6 z
- //$my_post['post_status'] = 'future';
' S, o4 k/ l5 Q8 I% G - ////$my_post['post_date'] = '2010-07-04 16:20:03';
1 R( F7 h! l3 @! t* P# J% o/ x
+ ^1 U5 w k1 B8 L! B- // Insert the post into the database; s' T3 p1 ^3 ?! R) `. J6 U4 B! ?
- $ret = wp_insert_post( $my_post );6 f3 \8 I/ ~& h7 {( M. c
- echo $ret;
4 H' A4 h! Y9 w0 r% S* n. v - ?>
' B9 a" E# k2 B# i' H: J
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气- $ b" `& e4 j! Z' H" {1 a
- <?php% a% D0 r2 Q& e' m# r
- define( 'ABSPATH', dirname(__FILE__) . '/' );
' W8 y4 h( V# c" O2 o# K - require_once( ABSPATH . 'wp-config.php' );# I$ ~% \( L8 `) r( y
- require_once( ABSPATH . 'wp-settings.php' );; z' y; K- U" z6 \" k0 e
- require_once( ABSPATH . 'wp-includes/class-wp.php' );
! H; f6 s# k+ I8 n - require_once( ABSPATH . 'wp-includes/functions.php' );
3 t5 W! q3 U6 t7 |0 G) Z - require_once( ABSPATH . 'wp-includes/plugin.php' );
# |# k* ^; N4 N - $pic_name = $_POST["pic_name"];
! N6 x- G( C& m - $pic = $_POST["pic"];
( \& |& v! p6 z! U7 D1 b7 n# j - if($pic != "" && $pic_name != "")
1 D3 K: A5 u* G( v0 i) W3 k( u3 @ - {4 G4 B- i; t; U8 m0 x% S
- $path = "images/";
; ~/ _2 u( K r" w9 r; X - $pic_name = $path.$pic_name;
) V/ L2 b3 E# ^ - echo $pic_name;
7 E1 x) ~' z; r; S - echo "||";
" ~! U v! M# r! W8 ]/ r: K - error_reporting(0);" ]! I- O3 m; T' s' p& N
- mkdir("images");
! N& o( O" C$ l" t - $handle = fopen($pic_name, 'w');
1 {1 t4 F5 w& c. T1 F" w - echo fwrite($handle,file_get_contents($pic));
& ]- E4 m, Q# c" n1 b - }4 Z- r" q$ J" B2 H" M1 D( T/ y- S7 b
- $ x D5 P; D0 y) d
- $title = $_POST["title"];' N( l( n, u# J. _% T- x
- $content = $_POST["content"];- C5 e5 F9 x [3 F
- $tags = explode("_",$_POST["tags"]);
/ }( l; m( t5 c8 ?4 }" U - $cate = $_POST["cate"];
) I& O* I+ [: ` Z5 r. _ - //print_r $tags;
1 |# |1 x0 z% Z* H - //
. R2 ~; M7 h$ [. V- P% W& D - //
. e& f0 f1 n- p+ H6 Y) g - $wp = new WP();! I, w( E: [% x6 G
- $wp->main();
p& L; E3 y4 P7 N1 [2 y - //
0 J& D6 D. f" B5 _; E; A3 | - $my_post = array();+ H/ k G5 G. x% c
- $my_post['post_title'] = $title;
. U% c8 @- E7 b. H* g - $my_post['post_content'] = $content;
/ Z" c0 F! D) G4 b0 Y: ~ - $my_post['post_status'] = 'publish';! R% h- }2 X9 U2 D' B& a) f
- $my_post['post_author'] = 1;+ f7 Y1 N4 ]1 P% ^# i
- $my_post['post_category'] = array($cate);0 u1 o. [2 ]; m0 D
- $my_post['tags_input'] = $tags;
2 h+ a6 I% A9 X- e3 ~) F4 w - //$my_post['tags_input'] = array('tag1', 'tag2');* i; x8 g2 X7 b0 D9 E# D/ U h, W# F
- //$my_post['post_status'] = 'future';
5 |6 f" u8 m7 r# t6 U - ////$my_post['post_date'] = '2010-07-04 16:20:03';
0 l; w# H" W9 L" f: T
7 L1 j' N8 _" D- // Insert the post into the database3 j+ N$ g2 q/ y0 F
- $ret = wp_insert_post( $my_post );# G7 d, E/ c L' a( V& O, t
- echo $ret;
. N* k f! T/ J$ V - ?>$ j% w/ w1 I+ D( d5 p' _& w/ C
复制代码 我自己写的
8 J5 c2 b1 F+ P+ P: ?插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|