|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑
0 {# U) ]4 J, q6 Z5 l0 l1 }& p
# D! Q2 I8 t$ C2 J# \ Y% C3 z5 B直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持; z' H- B! f# {$ C
1 E8 j. T; h5 ~4 i9 y+ F+ S, s
我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了8 S# }/ ^$ u% Z! @' X( p2 s
! \' `4 n) f7 k/ V) Z
方便实用 简单 何乐而不用呢
3 n/ k# C1 w, f( ~+ k- <?php _- s+ p7 D! ~
- " s5 h+ Y$ K3 B4 n: [
- define( 'ABSPATH', dirname(__FILE__) . '/' );
1 F1 N! z+ x# D- l/ P! O1 @% a - require_once( ABSPATH . 'wp-config.php' );; W7 J+ I6 @# b
- require_once( ABSPATH . 'wp-settings.php' );
0 L1 z. Y0 u$ `7 H" b" z' I" U4 O8 u - require_once( ABSPATH . 'wp-includes/class-wp.php' );
" D8 | O! X# {+ Y) a) O - require_once( ABSPATH . 'wp-includes/functions.php' );
) _6 f, s. s @" b% ?* f - require_once( ABSPATH . 'wp-includes/plugin.php' );
- q8 _ [/ M4 v# u/ N" S* m - $title = $_POST["title"];& o7 |: b, h& T/ p6 T4 N0 ^
- $content = $_POST["content"];% o# o8 h. g& Y* P1 d/ j2 P q
- $tags = explode("_",$_POST["tags"]);
2 L8 i6 G1 W: u3 C B8 k - $cate = $_POST["cate"];/ L0 P- T/ y- C8 X0 P, X
- //print_r $tags;7 Y3 r* |) w5 s0 n
- //3 l \! i5 a$ r% Y. d. y$ _6 N" f
- //
* p- o; P+ q8 H7 \& V* d - $wp = new WP();
9 s$ b) D3 L' J: q3 R A - $wp->main();
6 E. Y3 v9 e/ Q* [/ t - //
% l+ X) _. m! A# K* P - $my_post = array();1 I% d& z+ U7 h) ^$ N
- $my_post['post_title'] = $title;, d4 ]6 p Y4 A; U, w
- $my_post['post_content'] = $content;5 {/ v- `# W8 `$ |
- $my_post['post_status'] = 'publish';
$ V7 j9 V& u/ _( a ?! ` - $my_post['post_author'] = 1;1 ^/ m& x `& L; S5 h
- $my_post['post_category'] = array($cate);4 y. \: G) }7 ~; t. k6 E) e( a
- $my_post['tags_input'] = $tags;) v: _7 z& G5 H6 N G0 X
- //$my_post['tags_input'] = array('tag1', 'tag2');2 ]0 j* ?- W, M* @
- //$my_post['post_status'] = 'future';
$ y8 b: ]& u3 R4 w& J2 J ] - ////$my_post['post_date'] = '2010-07-04 16:20:03';0 r0 Q& _, b4 {" A2 Q7 \4 [9 ?
- 8 E! B; ?# a! k
- // Insert the post into the database, C+ B: U% A. u4 h# M
- $ret = wp_insert_post( $my_post );
! `6 L, u" {7 ~# h1 K- y - echo $ret;
' P( v% K- o% T9 C* `! b - ?>4 U, a# K- Z+ G) [2 Z
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气
9 L/ m) D& b" ^6 | ?- <?php: x+ W: W e h
- define( 'ABSPATH', dirname(__FILE__) . '/' );5 Z! `+ m5 c: v5 I! |+ v5 N5 K
- require_once( ABSPATH . 'wp-config.php' );
0 r$ \7 ?5 F7 _) N0 f; u - require_once( ABSPATH . 'wp-settings.php' );
" P1 {0 T6 g# r6 c7 ~0 f - require_once( ABSPATH . 'wp-includes/class-wp.php' );* M G5 \) \9 [+ `6 w/ T; C
- require_once( ABSPATH . 'wp-includes/functions.php' );
3 Z7 `# \ w# s D: i1 D - require_once( ABSPATH . 'wp-includes/plugin.php' );- E7 x* s' ~" A
- $pic_name = $_POST["pic_name"];2 s" ?2 w% s+ ~$ n* @; _& c
- $pic = $_POST["pic"];6 N, |( F3 f% }( K! ?
- if($pic != "" && $pic_name != "")2 k( d9 g3 t& Z; K& [ t
- {
6 i( K9 P7 J: p4 H1 ]6 m0 m3 |( ~5 A1 Q - $path = "images/";
, _9 N" B" M" [ f% L - $pic_name = $path.$pic_name;
: b6 y" I- N; @8 J4 P' R8 q - echo $pic_name;$ p* W' F# O: \1 }
- echo "||";/ l4 t" U B: i# ?$ [* j1 ?: I
- error_reporting(0);
4 }- U0 F* W2 N! W* [9 T - mkdir("images");
* E5 }6 M0 T1 }. M- B4 r8 d) L - $handle = fopen($pic_name, 'w');
|# I5 i7 G2 z' W6 P) Q - echo fwrite($handle,file_get_contents($pic));
* F6 ]1 ~/ ?: h - }$ Y j& C* M$ ?& O+ _5 ^0 ~$ m5 V# Q2 D
5 z7 I1 ]& H, W6 G4 B+ u1 B- $title = $_POST["title"];
& l& }- k1 m4 n0 F8 ?/ w& j - $content = $_POST["content"];9 i: ]* F0 ~5 x3 T4 W
- $tags = explode("_",$_POST["tags"]);9 ~1 S% D8 K. T/ I
- $cate = $_POST["cate"];
; X8 q% E3 s# B) d - //print_r $tags;% ?8 Z& g5 k9 P6 a* y8 U8 R1 Z& {# Z/ |
- //: I Y5 l2 Z' i9 W( W
- //+ C. C+ P: N2 {! |+ V& }; q
- $wp = new WP();
. t; Z$ L8 N) l; R* N# R6 J1 Y - $wp->main();' o, f/ B" @9 J% C: h \
- //
; C5 w3 @ c ^ - $my_post = array();& ?) {; W& C' `( J4 a. w
- $my_post['post_title'] = $title;* [: k* H. Z0 V% H, _! O1 {/ T
- $my_post['post_content'] = $content;
9 c, c: O8 c. l7 O - $my_post['post_status'] = 'publish';
3 O! D5 R" u w3 B: Q/ h4 _ - $my_post['post_author'] = 1;
8 N, s& y. H5 S2 c3 H - $my_post['post_category'] = array($cate);# H6 o" O: j. b! |
- $my_post['tags_input'] = $tags;
% H- e- {6 | f- e5 i% c+ M - //$my_post['tags_input'] = array('tag1', 'tag2');
6 A. l8 \8 C# |! W - //$my_post['post_status'] = 'future';+ y" A& g& ~+ u; F
- ////$my_post['post_date'] = '2010-07-04 16:20:03';% \3 K' @5 r7 O6 X- c' j# t
/ ~. K0 [1 m: \5 }; E- // Insert the post into the database9 H* @' W' ?8 B
- $ret = wp_insert_post( $my_post );
( y7 _/ @, x3 a$ z. S8 \+ i5 g+ Q - echo $ret;- g* z6 S1 M& q0 }7 h
- ?>
8 Q0 q3 O6 E& k1 _
复制代码 我自己写的
8 W6 q% @1 n: F u( a, z插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|