|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑
' m; ]4 O* t. s5 E
: Q! G% T, \, ~: F& s直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持1 o V4 _6 Q* R7 p) C" i- [
( h4 ]$ P. S8 s
我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了+ Q) Q z( @; _* c! u$ [3 z
6 u6 C8 p; Q6 n
方便实用 简单 何乐而不用呢
1 ]; `8 |( H! |- <?php
- q% }; z `; k2 C0 c - . ^ A& g; p& l7 L
- define( 'ABSPATH', dirname(__FILE__) . '/' );
( Y1 ?$ G& i4 r$ [7 C; j9 [ - require_once( ABSPATH . 'wp-config.php' );
/ O& J! ]- ]; Y - require_once( ABSPATH . 'wp-settings.php' );2 _$ G' m1 _ I4 ~* U. x
- require_once( ABSPATH . 'wp-includes/class-wp.php' );
, n9 f' y4 A$ e1 C - require_once( ABSPATH . 'wp-includes/functions.php' );6 Y- a# l! m$ u" w3 q
- require_once( ABSPATH . 'wp-includes/plugin.php' );
' n+ f/ b& H, C( K1 F9 c - $title = $_POST["title"];4 w% S3 j8 n! y3 H( t- I0 A
- $content = $_POST["content"];
) z* D8 w% o: L: V4 Y+ H2 o& t) f) _ - $tags = explode("_",$_POST["tags"]);. g" }, m4 e: k4 Z- ?& L6 }4 I
- $cate = $_POST["cate"];
7 \( C% l6 Q" w - //print_r $tags;' Y+ k$ H& P8 E
- //* f6 M& D9 S: }1 h$ J+ r% j4 n
- //
+ L" B4 ?$ Q4 e. k, V - $wp = new WP();
4 }. ^. D% _6 |; y - $wp->main();* L' m7 }" {% r: f" x/ M! ~7 b4 e6 d
- //
2 I5 L% F+ a( u, y1 e' t - $my_post = array();
I, @8 N5 o8 ]% T+ j- h; _+ I - $my_post['post_title'] = $title;
) x# g* I) h7 x - $my_post['post_content'] = $content;7 V6 R3 a+ [( f1 {( g$ Z
- $my_post['post_status'] = 'publish';
+ W8 A3 e) A2 x$ j; ] - $my_post['post_author'] = 1;
; |; r: g# w4 S6 O2 A" k - $my_post['post_category'] = array($cate);9 y I! c- P k+ u' {
- $my_post['tags_input'] = $tags;
( x! U& B1 \' G0 U5 `9 e. l- J7 _ - //$my_post['tags_input'] = array('tag1', 'tag2');- b& k# v5 ^, g$ r
- //$my_post['post_status'] = 'future';+ \0 c7 O% B# R$ J
- ////$my_post['post_date'] = '2010-07-04 16:20:03';, F6 K8 |8 \* A9 e! E
- : z- @9 \$ { B( f0 e. X( Q
- // Insert the post into the database
3 e2 Q8 d" s( ]7 ]8 z! d* Z - $ret = wp_insert_post( $my_post );' o( ? W1 l9 M I7 A3 c
- echo $ret;
; }0 u( R/ u* U$ D+ E/ G; v - ?>0 U5 b: \' P6 }" C
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气- ( Y' G- u0 D U
- <?php9 u: G0 V& v1 w2 f$ Q, V
- define( 'ABSPATH', dirname(__FILE__) . '/' );; A$ x' g7 o% v+ n# u
- require_once( ABSPATH . 'wp-config.php' );7 I/ V, V& X' J# Y6 M
- require_once( ABSPATH . 'wp-settings.php' );+ d: v( O2 ? w& ~; {7 {
- require_once( ABSPATH . 'wp-includes/class-wp.php' );: C4 J( N& j y" s
- require_once( ABSPATH . 'wp-includes/functions.php' );
' ^% @" ^. n) v0 q) b" j. f - require_once( ABSPATH . 'wp-includes/plugin.php' );
/ y/ R+ ], B: h& I" _8 { - $pic_name = $_POST["pic_name"];
" @# m% g; _3 Z: [9 Z; V - $pic = $_POST["pic"];
4 p5 ?( y+ U: r0 N" J- z8 g - if($pic != "" && $pic_name != "")
% Z) c; G0 @* h+ E0 T - {3 }' P% I3 P8 S# y
- $path = "images/";
) l5 y/ M$ ]' B: K1 Y4 ^* J - $pic_name = $path.$pic_name;
, K1 s9 l' R. j5 O8 q - echo $pic_name;
% ~- E% ~* p5 [8 D' \/ i6 s8 u - echo "||";+ }0 y0 ~' v& v& h- t
- error_reporting(0);% _9 a) p0 c2 H7 J6 n' s4 t
- mkdir("images");
( A( m8 T7 |6 T; i3 E - $handle = fopen($pic_name, 'w');6 g: o+ A6 h* _0 b' Z, b
- echo fwrite($handle,file_get_contents($pic));
+ j9 C6 o7 H5 f% H3 ]$ q; e8 W - }
- B( D6 g3 ^& d9 z& s - 3 j* ^+ N: M; G/ U' Q5 J; o; f$ J
- $title = $_POST["title"];
/ m# l# r* u* `2 ~% T- c- F5 o - $content = $_POST["content"];- f4 U8 |6 K* k8 Q9 F8 _8 d
- $tags = explode("_",$_POST["tags"]);
6 a9 z: s8 \2 E" V7 S j& f) @0 k - $cate = $_POST["cate"];: f |; Z* H2 l4 I2 u7 Z
- //print_r $tags;- O" x5 A+ T+ X9 O, ~* C
- //
8 G; {! s. E0 _8 T1 { - //
& R5 ^ V8 i# U9 l V - $wp = new WP();
# z- y. t1 v, j3 \ - $wp->main();
3 g" a9 N: q8 P4 x - //7 a+ L0 r: C1 R. T
- $my_post = array();7 X1 v w+ R2 Y) C/ a
- $my_post['post_title'] = $title;
3 j A4 W: Q9 ^- g+ K2 l - $my_post['post_content'] = $content;- \- z0 a8 ]8 [, z3 R( L% m6 c
- $my_post['post_status'] = 'publish';& u1 l. N' [; W& r# Y: F
- $my_post['post_author'] = 1;* i o" }$ |$ {' s- Y. N
- $my_post['post_category'] = array($cate);
3 T& y9 W* f/ Y# z: E) _ - $my_post['tags_input'] = $tags;
2 m# c Q T2 S0 S - //$my_post['tags_input'] = array('tag1', 'tag2');# c$ [( e: J4 L( U4 r
- //$my_post['post_status'] = 'future';1 ]! n g& l |: \$ g4 l
- ////$my_post['post_date'] = '2010-07-04 16:20:03';
9 o; t* j) I: m
! i, W; K e% K( ] F# H! T2 k+ a2 ~- // Insert the post into the database
( q m1 O L Z" A - $ret = wp_insert_post( $my_post );
v5 J. V) b7 C% \6 a - echo $ret;6 o5 m8 Y$ S, e/ a
- ?>1 b0 D1 b# H% u# i7 @
复制代码 我自己写的' b5 m6 I9 z' r9 C
插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|