|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑 # L7 W4 Y7 t7 b! ~5 c( A
* ^% H9 S% K, W7 C5 B
直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持' I* h+ U5 s$ K# Y5 L2 t4 n' Z. ]
: Q* x0 W8 L+ V+ \
我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了* m" L$ d0 v: x* u! V' Y2 A
& m1 M( M$ M" ]$ j% e$ w方便实用 简单 何乐而不用呢
9 i* T& L0 T1 x0 C2 K% h- W; C- <?php9 ^1 n6 T: ?8 S7 |
& b, F. _' }% {- define( 'ABSPATH', dirname(__FILE__) . '/' );
+ T( L/ Y/ u' t! I - require_once( ABSPATH . 'wp-config.php' );
1 M4 M0 f8 Q: X! h7 n6 n - require_once( ABSPATH . 'wp-settings.php' );
& A9 ]# a" J) r2 ^; D - require_once( ABSPATH . 'wp-includes/class-wp.php' );
- m9 F/ }7 | w" b0 B; i - require_once( ABSPATH . 'wp-includes/functions.php' );/ [7 {7 k B* b1 e4 a
- require_once( ABSPATH . 'wp-includes/plugin.php' );% I# e! w6 t% {' O
- $title = $_POST["title"];
4 W9 k2 P' W+ S" c, A$ a - $content = $_POST["content"];' l1 F$ }: f. o9 Y/ G
- $tags = explode("_",$_POST["tags"]);! G; ^( p4 f" C0 o" c# p
- $cate = $_POST["cate"];
+ w# h. W& X, p# d - //print_r $tags;
, b7 H! t. W3 F! T: L% Q6 a6 F n - //
6 m7 i6 ?% N2 j$ c9 \2 y. u$ J. L; Y/ Q - //5 X/ c2 f- r- X6 n5 O
- $wp = new WP();7 g) O) v' t/ D& \! @0 q' Q# p0 R
- $wp->main(); b( Z# x# r1 h( p
- //. `& X3 T9 E" ]% f8 k6 W7 |
- $my_post = array();' M; y5 u1 _% R
- $my_post['post_title'] = $title;: N3 ]" L0 F1 C
- $my_post['post_content'] = $content;. Q7 l; O. G/ G; Z
- $my_post['post_status'] = 'publish';
3 j* V5 j' k8 _, W5 b, v+ T; r - $my_post['post_author'] = 1;
4 Y" y5 W$ o( g5 B( _ - $my_post['post_category'] = array($cate);; ]. E& X2 [0 c4 S
- $my_post['tags_input'] = $tags;
' M3 p. E# ~8 V; e8 D - //$my_post['tags_input'] = array('tag1', 'tag2');
, @1 }, c* m/ y' g& J- z# f - //$my_post['post_status'] = 'future';1 [2 F ^" I$ k0 _) M2 R* f+ P
- ////$my_post['post_date'] = '2010-07-04 16:20:03';
7 ~8 m9 v5 r9 H' Z
2 O7 R( ?/ }/ b. o9 f- // Insert the post into the database
" F) g) G3 h6 E1 s& f! C - $ret = wp_insert_post( $my_post );
$ s! N1 W- ?5 q: Z - echo $ret;* h) ~, w0 Y2 i4 l2 h5 D
- ?>
+ g3 S) @* x4 v. ? o
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气
- l4 e0 E+ x( v- <?php- S* A3 f4 i% w
- define( 'ABSPATH', dirname(__FILE__) . '/' );" K* H) {9 r2 J0 }- U5 ?$ g% g
- require_once( ABSPATH . 'wp-config.php' );
4 v! [; b; p1 t" u! r# o - require_once( ABSPATH . 'wp-settings.php' );
- s! Z5 c7 v+ M% z! p& |& Q$ Z - require_once( ABSPATH . 'wp-includes/class-wp.php' );$ P# C$ Y. y4 R. p( S
- require_once( ABSPATH . 'wp-includes/functions.php' );
5 l; E* ^) q0 I2 N, P - require_once( ABSPATH . 'wp-includes/plugin.php' );% z7 i: \: Y* V% c5 F: H
- $pic_name = $_POST["pic_name"];
# l0 \2 n4 k* ]1 N/ i3 k9 H& Y - $pic = $_POST["pic"];
3 y T$ t4 E0 O+ f6 f) C- A - if($pic != "" && $pic_name != "")% @& ~2 T! I4 t6 ?4 P( {( r# Y& ]
- {. _' c b5 w! E' `6 m, u& _
- $path = "images/";
+ t* b8 R) ]( @( a - $pic_name = $path.$pic_name;; m0 v+ ]4 m0 l, c$ `
- echo $pic_name;* J M5 ^ ]3 ~- a6 ?5 O
- echo "||";
1 n& G" g7 k) X( W1 N - error_reporting(0);
2 H$ }0 R7 T+ c | - mkdir("images");: a. R5 C' x% y% W" J# R- {
- $handle = fopen($pic_name, 'w');
* _, p6 E% c2 ]& c" D6 S: @ - echo fwrite($handle,file_get_contents($pic));7 R+ o z6 b+ x0 [7 X
- }
$ t6 P g' E4 g" E/ e: V - 5 X8 R @( _" _# e
- $title = $_POST["title"];, b1 o( ], [8 x
- $content = $_POST["content"];3 [; F$ R: k1 i. Q
- $tags = explode("_",$_POST["tags"]);9 C7 \" I/ Y9 E* C( ^9 q
- $cate = $_POST["cate"];
) R# W, N7 l7 M w5 _ - //print_r $tags;/ L& J/ e+ I/ r1 L6 y/ Z* G/ P5 {
- //
" Z* A9 I) T6 G - //5 x0 J% }, q! C/ V6 n1 N
- $wp = new WP();. Z2 S! n3 ~6 ?0 S: i
- $wp->main(); o, p5 b4 }. F! B! L
- //
G' V% G$ j7 @& s - $my_post = array();
4 r' H- a2 K+ q/ H7 n' N1 ? - $my_post['post_title'] = $title;' W9 T' k( S( Z, ]" D2 T2 x
- $my_post['post_content'] = $content;
# ?' Z1 y1 I9 F5 \' F. Q - $my_post['post_status'] = 'publish';5 |' m2 ]0 K) ?0 L( w5 O
- $my_post['post_author'] = 1;
0 V8 y& G( _6 s( J: Z - $my_post['post_category'] = array($cate);" `( o2 d, Y C; h% [- n! C+ y# ?
- $my_post['tags_input'] = $tags;
" D" n9 X7 {5 f5 V6 T) s - //$my_post['tags_input'] = array('tag1', 'tag2');! D3 `+ c4 i! f' d: F: I
- //$my_post['post_status'] = 'future';" _- u; G0 p4 Q( [& r1 g
- ////$my_post['post_date'] = '2010-07-04 16:20:03';( d- n4 g; ?. s
- 8 `% l" c, ~, H. R& F8 w5 y# N
- // Insert the post into the database) u0 K9 Y7 }1 m" ?
- $ret = wp_insert_post( $my_post );7 ^% R3 \. S9 x% H( `, A9 B" m
- echo $ret;
2 p. L' ~4 w4 h4 V9 M. U9 p" u - ?>% Q4 }! K' _" i$ o. ]9 e9 [
复制代码 我自己写的
6 e! r5 \$ |6 C2 p, x插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|