|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑 2 O& f4 l$ x2 Q! T$ e: u: P
d: Y& ^' O5 o, R E) H" M% r
直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持
7 O9 o- }- D7 H! T6 h) _5 L
1 D3 V8 f0 n! o. ^8 g0 @; Y我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了
' O0 N, {; A9 h* a
+ L( Z" B) H. o H/ w方便实用 简单 何乐而不用呢
& A/ g% R4 h' f+ F2 e- <?php
U( c6 N8 @' t% B1 M2 `" F# J
1 E/ m3 G- D9 x$ ^9 ~0 u- define( 'ABSPATH', dirname(__FILE__) . '/' );8 ?0 I: [! z. d7 i
- require_once( ABSPATH . 'wp-config.php' );% a& Z) L1 T) Z. @! G7 V
- require_once( ABSPATH . 'wp-settings.php' );
* _; _4 P, G. @( k: E/ B) } - require_once( ABSPATH . 'wp-includes/class-wp.php' );: W7 I! L& v0 T5 G* }5 s
- require_once( ABSPATH . 'wp-includes/functions.php' );
; h" S7 v8 ^' c# G( L; j* _ - require_once( ABSPATH . 'wp-includes/plugin.php' );
' L) N. |& k% M, m/ U1 j - $title = $_POST["title"];
1 J t" N9 a& E3 R, ] - $content = $_POST["content"];. V' Z) Q8 M2 j) J8 M7 P, Q
- $tags = explode("_",$_POST["tags"]);
, u1 C/ k' o% f6 J. G1 S - $cate = $_POST["cate"];
) }4 [8 e& _% q' K8 f - //print_r $tags;
2 r- S/ H' ?( f- M( n7 f/ t - //
0 v2 z' D" Z! Y& d' t% k3 P4 P) i - //8 j! W4 ?$ p% ]: t3 e7 M
- $wp = new WP();4 m j& q$ d# w
- $wp->main();# N" {3 j+ q/ Q; q2 A7 c) |
- //
4 }. u! w; G, W" f - $my_post = array();8 R! f% _, V6 N( ?
- $my_post['post_title'] = $title;) w7 o7 t( |" H
- $my_post['post_content'] = $content;
& F2 r4 t H, K1 e! p6 W - $my_post['post_status'] = 'publish';# L; I$ F# A6 \0 @! K2 I- @% b
- $my_post['post_author'] = 1;: H* @# j) V9 O5 f
- $my_post['post_category'] = array($cate);
& Y0 k; E* z) [+ [" J1 q* J - $my_post['tags_input'] = $tags;
5 X9 b7 \7 q; @! s - //$my_post['tags_input'] = array('tag1', 'tag2');. G* A- E, X9 P. s
- //$my_post['post_status'] = 'future';; }/ i' I" I$ p7 U
- ////$my_post['post_date'] = '2010-07-04 16:20:03';
% |( A9 l8 g: s; w" t% K! m - 1 Z4 F. t/ I( h+ V2 N
- // Insert the post into the database
! B. R8 b/ h( ~% L( |* Y4 | - $ret = wp_insert_post( $my_post );
, l% r5 M) B% y# s3 d0 ^ - echo $ret;
. p L' O* Y+ L" m# D$ C" |' s - ?>' Q2 n6 m( r8 q5 A% U5 }
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气- : A" Y- Z, R2 Y3 ]4 l4 N3 e) R
- <?php
$ b V3 @3 n/ O% e' g. M - define( 'ABSPATH', dirname(__FILE__) . '/' );
1 C) c7 x+ b: _3 p3 X! U, _# g - require_once( ABSPATH . 'wp-config.php' );- |" p+ _! ^, a* P# m2 w8 G
- require_once( ABSPATH . 'wp-settings.php' ); `/ l2 i q3 |/ u
- require_once( ABSPATH . 'wp-includes/class-wp.php' );' p( \' t! F, U
- require_once( ABSPATH . 'wp-includes/functions.php' );
1 D7 J# A( W. @4 [9 w2 N" B - require_once( ABSPATH . 'wp-includes/plugin.php' );
$ n( I2 J6 w$ t1 w# \5 Q' _ - $pic_name = $_POST["pic_name"];
- f9 c/ C. h: n& G2 ] v - $pic = $_POST["pic"];7 |7 i) ]2 `1 d- I* e; {+ m9 O
- if($pic != "" && $pic_name != "")
. j0 K* I* `# k7 c* |( w6 I! n - {5 J0 n/ q: y: w& F4 y+ k- z+ G
- $path = "images/";8 D0 E+ Z+ R1 P/ H2 H& q+ |- n9 Q
- $pic_name = $path.$pic_name;
5 L, Y) K M6 r* B/ \ - echo $pic_name;* C: ~* C* j) a$ \3 h% J
- echo "||";; L, _" l. v1 w2 [' Y: e
- error_reporting(0);
: z* K# ~ D" h" x- N9 d# ~ - mkdir("images");# g, W1 G \. }
- $handle = fopen($pic_name, 'w');; H( ]+ a0 c- E& c: l6 y
- echo fwrite($handle,file_get_contents($pic));( V: d0 A# v: o6 o6 l2 A2 C+ I2 w
- }
X7 U$ m. q* ]* X3 y - 5 y* p; z3 w( z) H8 z' r7 e' ^
- $title = $_POST["title"];
' H- t7 {1 r6 T9 _; }- U - $content = $_POST["content"];2 ?- l( }& w9 k
- $tags = explode("_",$_POST["tags"]);5 K& |. F: _( }( x$ _8 S5 |
- $cate = $_POST["cate"];
: f P. M- P/ N - //print_r $tags;
# v3 A* e+ Y) I4 |0 c" N0 a - //+ a+ k, ]( a3 D
- //4 f; I# g; c/ o5 G
- $wp = new WP();: u. G$ C9 b/ h- a) S% \2 S
- $wp->main();5 K& w( d+ G+ @2 n( N" U: K
- //
a% F$ Z! O- ~/ S; o) K7 S( ? - $my_post = array();
! C; P4 N# J9 c - $my_post['post_title'] = $title;
+ U9 B: Y1 j& G# Z( ]& R* l% a - $my_post['post_content'] = $content;2 Y3 d+ X% T& S: Y
- $my_post['post_status'] = 'publish';, d2 v! U: T) S* }* Z2 ]
- $my_post['post_author'] = 1;
4 E4 |, a! p4 o4 K' x4 p7 g - $my_post['post_category'] = array($cate);2 v% M4 o/ y) i* j
- $my_post['tags_input'] = $tags;- O2 j/ B. |: ]' G3 @
- //$my_post['tags_input'] = array('tag1', 'tag2');5 T3 a* N5 W! x' Q/ w
- //$my_post['post_status'] = 'future';% t7 z' l* r! p. T
- ////$my_post['post_date'] = '2010-07-04 16:20:03';( c0 r" i% M7 a6 U$ n. { D
* T/ c3 ~/ L- D5 t9 c- // Insert the post into the database3 N# C) R- K$ |5 G5 n7 v
- $ret = wp_insert_post( $my_post );
* T" r6 B* O, e$ K( _# r1 m - echo $ret;
6 E& X3 D% F- h' ^' m' {! @( ^& ]8 v - ?>
* M! D6 f' P5 ?7 Q4 G
复制代码 我自己写的0 S2 t0 K* O5 J# k- ^6 J
插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|