|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑 8 L; P& v& d0 g4 i' ?
3 f" r9 r0 v, v" ^直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持2 G/ ?/ x. o0 T8 Q
. e6 w9 H2 T3 I O* ?我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了- @$ g4 C, Y, O& u9 @. `
# \, F! A2 \/ |4 w, W方便实用 简单 何乐而不用呢- 7 X# ?4 k0 P. n0 U4 e S* d L
- <?php1 b4 b6 b- b" D) r" l
- 0 E, N! h% B/ W. w6 S {$ Z
- define( 'ABSPATH', dirname(__FILE__) . '/' );
' z" R+ G. D! L% O - require_once( ABSPATH . 'wp-config.php' );
$ f* U6 u3 A$ t- P - require_once( ABSPATH . 'wp-settings.php' );
5 J+ ?. F" i2 p U6 N# e0 ~5 n - require_once( ABSPATH . 'wp-includes/class-wp.php' );) m% @8 s8 u) T# Z1 B
- require_once( ABSPATH . 'wp-includes/functions.php' );
' E# p+ B: U! T. r+ J! u6 P - require_once( ABSPATH . 'wp-includes/plugin.php' );6 Y& B) b6 J, G! t, J
- $title = $_POST["title"];$ f( I0 P$ P- z& o5 n3 K' V
- $content = $_POST["content"];
) T( N7 S) } ?% O - $tags = explode("_",$_POST["tags"]);
( x* h& S- X! g" G$ b2 P0 D - $cate = $_POST["cate"];
, w4 k0 V6 ~- S( F3 { T( d - //print_r $tags;3 a" o, L/ E/ A
- //
4 s, H4 O+ M8 c6 T# ]2 o- n6 f - //
- y" ^6 a# V, ~1 ~8 q5 w. ` - $wp = new WP();
$ `! S7 |( t' u3 B1 w# L1 ?, R6 O# O - $wp->main();
" U+ a( ~* P% H3 n6 o& T2 ~: J - //
" y7 h/ Y5 v0 h - $my_post = array();5 ?0 _$ R6 R t& t# K$ E$ ^
- $my_post['post_title'] = $title;3 {7 ~7 ?1 O& h- S- X" W
- $my_post['post_content'] = $content;/ r8 [2 l4 {( s& o9 G
- $my_post['post_status'] = 'publish';
2 o7 t/ c: i9 R8 u& _. w - $my_post['post_author'] = 1;
8 K% ]* V% t) q" v - $my_post['post_category'] = array($cate);- S/ |& P( R% M3 b' a
- $my_post['tags_input'] = $tags;! A" W) [$ v$ C# E% n% j+ g+ k2 U3 z3 l
- //$my_post['tags_input'] = array('tag1', 'tag2');
0 V+ J& r2 O% S# X - //$my_post['post_status'] = 'future';, F4 N2 U0 T! e* V! K
- ////$my_post['post_date'] = '2010-07-04 16:20:03';
' Y! m) k9 X; Y - 3 g5 p& [, n, [
- // Insert the post into the database
' b+ a0 D! }; z/ F1 n' G6 @5 M - $ret = wp_insert_post( $my_post );
' Y( v3 S# p' W1 O - echo $ret;
) d- r! e, ? t" Y7 X2 J - ?>. g: k4 D7 \$ [$ _
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气- 2 i9 S# d. w+ L- h$ C8 u2 A1 O/ w1 z
- <?php4 d7 s, }: l: O8 c [2 |; \
- define( 'ABSPATH', dirname(__FILE__) . '/' );, N8 c1 O) u! P
- require_once( ABSPATH . 'wp-config.php' );
6 x; J1 h3 S* N2 Z `! B( X - require_once( ABSPATH . 'wp-settings.php' );6 ^: d$ E" P& v/ z1 _7 h
- require_once( ABSPATH . 'wp-includes/class-wp.php' );5 g2 b4 Y& [* U6 E! y) K- B
- require_once( ABSPATH . 'wp-includes/functions.php' );
$ W: g# v4 r/ G* d* x. M& G! z3 K! N - require_once( ABSPATH . 'wp-includes/plugin.php' );7 Z5 K8 |/ l& C0 R8 e. U' s7 v& u
- $pic_name = $_POST["pic_name"];- s9 g9 K9 I2 W5 ]9 I% x3 N C2 M
- $pic = $_POST["pic"];! N. K, w4 L+ U1 p
- if($pic != "" && $pic_name != "")
& I: j$ `$ W3 f( F, T( u" ] - {
, w F( f/ V: _5 y- I" V - $path = "images/";
H. E1 A" U% C+ g& ~- u - $pic_name = $path.$pic_name; P+ \) [# v) N' T3 \
- echo $pic_name;% H4 y) [+ D5 v+ m/ N* S
- echo "||";, }8 j5 M0 Y5 U, Z7 @, y# W
- error_reporting(0);
1 E0 D. `5 P; v1 m - mkdir("images");5 N \$ Y* _$ S7 L; f
- $handle = fopen($pic_name, 'w');
9 b/ C: }) g3 d5 L - echo fwrite($handle,file_get_contents($pic));
. Y- ` e/ T; [) f3 s - }2 K5 L& i# e2 e/ l _# n
( `3 x* N0 ?6 F- M" F5 A- $title = $_POST["title"];
* p" N, Q/ ^6 J: } - $content = $_POST["content"];- J2 r3 O$ ]" l' ~
- $tags = explode("_",$_POST["tags"]);
1 n# A7 v, {" y1 ?, J3 J+ Y - $cate = $_POST["cate"];; @$ P- n) o9 x m; K$ J: \: _; \/ u
- //print_r $tags;
B) {4 u# T# @9 b8 u - /// r# M! a! W9 c: h; Q
- //
+ L8 b, U' ^! ~+ e* v - $wp = new WP();
1 M. w0 o& B* c' | - $wp->main();
" M$ k2 A7 ?4 g: J# ?% U: \ - //
. O0 W5 S9 l2 W% N% h _ - $my_post = array();" i: Z, h! Z! q7 A) X
- $my_post['post_title'] = $title;
+ Y2 h6 m, A8 ~4 j& R$ L$ f - $my_post['post_content'] = $content;
" E( \/ g8 t2 M9 ]" E6 [. b$ L - $my_post['post_status'] = 'publish';
; m8 p) ~# o4 Y - $my_post['post_author'] = 1;& Z$ i1 @' [% q5 z
- $my_post['post_category'] = array($cate);, V1 L) M; |1 j% D
- $my_post['tags_input'] = $tags;9 U. s" @) b, L4 d
- //$my_post['tags_input'] = array('tag1', 'tag2');0 g# Q: G: E: d2 ]$ K
- //$my_post['post_status'] = 'future';
) q- Y! f, u( d3 ` - ////$my_post['post_date'] = '2010-07-04 16:20:03';' ^( u4 r/ R9 ]0 c8 ^- ^
- + P$ _, m$ \" D5 z& ]# g
- // Insert the post into the database
7 E# ^3 R4 o2 ]( w* L8 d2 \' s - $ret = wp_insert_post( $my_post );
& _1 k, U; Y0 g - echo $ret;
' y. Y W% k3 @/ h* c - ?>
9 P& @' x7 v: i
复制代码 我自己写的
/ E/ w1 N3 h0 C6 z; r插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|