|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑
; o' |2 I6 [& ]1 A' ~" f8 A
8 r" S; S) Y/ \) z7 Q直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持
" e& X- _ |7 F: w2 H* E$ V* G, h8 P' H( Q% z& N
我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了! W7 Q) p; P/ M# L: @% \8 {5 k$ ~
" |% V, q6 N# Z5 M: }方便实用 简单 何乐而不用呢- ( J; K9 U- N9 b/ n& P7 _: M1 K$ ]
- <?php
1 V+ y" _4 s7 n+ r( j+ ]/ C - r1 o' a$ C, r6 q" _
- define( 'ABSPATH', dirname(__FILE__) . '/' );& o: Z. R* n- e. j( o+ J* Q) f
- require_once( ABSPATH . 'wp-config.php' );
! r4 w" K% j7 g2 G" R - require_once( ABSPATH . 'wp-settings.php' );+ B$ D# c4 K' d$ [
- require_once( ABSPATH . 'wp-includes/class-wp.php' );7 @/ W2 b. b l
- require_once( ABSPATH . 'wp-includes/functions.php' );( f# x/ [5 d/ N6 b" z% o/ B
- require_once( ABSPATH . 'wp-includes/plugin.php' );& @0 F! I" F8 H6 \2 L3 o
- $title = $_POST["title"];3 ]/ t& A) }: O9 X/ j4 U' W# p
- $content = $_POST["content"];: c6 @0 d% U5 y3 L& c. X
- $tags = explode("_",$_POST["tags"]);* q2 u7 D# W$ Y
- $cate = $_POST["cate"];
6 P0 z8 G/ z! F9 a. e, |0 l, p. { - //print_r $tags;0 ~8 X0 F( N* [7 E, ~0 w
- //
% z' o! D6 b& Q2 {8 X3 i+ G; \ - //
; y8 e/ S0 b( w1 ]7 F8 I D' O0 \ - $wp = new WP();
7 Y, K( _1 q' ?+ e, D \; z - $wp->main();
5 W, Y6 P6 M6 U( A8 u3 z9 N' I - //
; b5 M9 J. t; w6 O7 [7 e { - $my_post = array();
, X+ A: V7 C. @ z, t8 i - $my_post['post_title'] = $title;( C# r0 o; h' |8 E% E
- $my_post['post_content'] = $content;
" G' a. \5 ^0 Z - $my_post['post_status'] = 'publish';. n, o- g; ^# d
- $my_post['post_author'] = 1;# g% q ?3 K5 W W8 N x7 }
- $my_post['post_category'] = array($cate);( Z0 d# C3 b. A6 \. e
- $my_post['tags_input'] = $tags;
: d9 n5 g7 k, M0 R - //$my_post['tags_input'] = array('tag1', 'tag2');
0 b, ^8 W5 K, S7 d9 U. x - //$my_post['post_status'] = 'future';- S4 ?2 [2 u% T! h& O
- ////$my_post['post_date'] = '2010-07-04 16:20:03';
$ C" S9 H1 G3 l# y& W - 7 m8 S& G! F6 v
- // Insert the post into the database$ k- ~! E8 f" v z4 k2 e' h# |
- $ret = wp_insert_post( $my_post );
. B- ^9 N. d" s |$ g; S0 T - echo $ret;
/ `7 n5 g% A/ s - ?>
# A1 @7 }# v/ |+ o5 o
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气
, v/ B5 U$ C5 D- <?php o* D3 C7 K7 l* ]& ]
- define( 'ABSPATH', dirname(__FILE__) . '/' );" Z9 d- u& B6 k
- require_once( ABSPATH . 'wp-config.php' );
. Z- \4 m2 b- F3 b( X - require_once( ABSPATH . 'wp-settings.php' );
: S8 d! N% ^% i. f* c. ^* Z - require_once( ABSPATH . 'wp-includes/class-wp.php' ); o/ J% J3 Y! n0 q1 |3 m
- require_once( ABSPATH . 'wp-includes/functions.php' );
8 }0 Y0 i& _. V% ?& B - require_once( ABSPATH . 'wp-includes/plugin.php' ); n+ D2 n- X7 y! p S
- $pic_name = $_POST["pic_name"];
/ O- E9 k& X9 g0 J* p - $pic = $_POST["pic"];0 I- |7 M3 Y3 g1 X
- if($pic != "" && $pic_name != ""). O* Y0 P. [3 Q3 r
- {
; \: F# o5 R. @4 s - $path = "images/";& \0 e( B" L3 i2 T4 A, ]
- $pic_name = $path.$pic_name;
; M6 T( g" Q4 b+ x: t. b/ T9 J& ? - echo $pic_name;
# d4 R$ s9 E& }8 d/ Y - echo "||";
* L& z5 D" z7 X* N - error_reporting(0);6 H" d% x6 I: j! y0 M! [
- mkdir("images");' e7 S+ V0 [3 A8 i
- $handle = fopen($pic_name, 'w');) C+ S: A6 M: s5 W0 m
- echo fwrite($handle,file_get_contents($pic));
5 M9 ]' u8 K& b5 C) X$ S - }
$ U8 k, \4 ^9 {* L/ R6 J q
" C8 `( a j' \5 E- $title = $_POST["title"];
) a! C/ R5 k, g8 o( \2 o - $content = $_POST["content"];! X, U* k' d9 R/ P5 x$ U0 a- C
- $tags = explode("_",$_POST["tags"]);
+ A: [# c( E4 _3 ^$ V0 f6 q - $cate = $_POST["cate"];" H, P! H" w* `$ ^, t
- //print_r $tags;
# o$ u- X1 @5 V3 y3 i - //
7 R5 L" G8 y8 D0 w1 \ - //
: `# c9 I" A7 u- o) ?3 r) \% c - $wp = new WP();/ z8 }4 }* c6 p: p2 L K
- $wp->main();
( C8 O) \* }/ {9 X - //! e) B5 F% g }" J" K
- $my_post = array();
% Q3 C: @& U/ }) K. C8 ?, B: [ - $my_post['post_title'] = $title;
5 q3 v4 c: ^$ C0 f. n - $my_post['post_content'] = $content;
, X; A$ o: x6 Q6 ?; s$ H0 R7 F - $my_post['post_status'] = 'publish';
P& c' ?0 D. ^3 `( }% g" y - $my_post['post_author'] = 1;
3 R; U3 A ]" b* R3 K; n - $my_post['post_category'] = array($cate);
, B0 s, o7 B+ p* q$ c7 G$ U - $my_post['tags_input'] = $tags;3 S9 O( Q7 ?6 a9 V$ |
- //$my_post['tags_input'] = array('tag1', 'tag2');9 x& C1 w1 U+ {! X ^5 y
- //$my_post['post_status'] = 'future';4 w5 Y" B- n u, S! G# w( A+ g" T
- ////$my_post['post_date'] = '2010-07-04 16:20:03';$ Y8 `% F# c% |$ O: t9 U
; Y/ m5 S6 ^2 X* |0 M3 M- // Insert the post into the database
9 K( f- O4 X% f, n0 y: H - $ret = wp_insert_post( $my_post );
& \, o. D5 j# g" S- [+ M - echo $ret;
/ g" z1 T# q0 [# o - ?>
& O( f1 ?4 i8 G/ Z9 j! A
复制代码 我自己写的
& V# Q8 I" d3 y6 q插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|