|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑
" U# p7 H6 O# i% ^8 [3 X" Z: y3 u
# k3 _- x. y; P3 x直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持: ?" j5 [/ A" E: W+ O4 S$ h8 C
* }3 M3 R+ k5 E; c0 E+ E
我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了. l7 x2 L i% v8 X7 @! U* W7 p% C" ~
" ^3 E$ }% |# W$ N方便实用 简单 何乐而不用呢- 1 c' S# g9 L7 F: _8 }( Z
- <?php
- F5 s8 `2 @! t# ^" Q8 d - 8 _) W. i2 X( Y
- define( 'ABSPATH', dirname(__FILE__) . '/' );
& P# a5 R- m2 Z1 w5 D. _! ] - require_once( ABSPATH . 'wp-config.php' );" x+ J9 n% y$ n
- require_once( ABSPATH . 'wp-settings.php' );
I+ K1 T9 d6 a3 P& F! r - require_once( ABSPATH . 'wp-includes/class-wp.php' );7 K2 G C: X( D. k
- require_once( ABSPATH . 'wp-includes/functions.php' );4 s2 C( W1 o* x$ z, Q
- require_once( ABSPATH . 'wp-includes/plugin.php' );
$ H9 ?- x( x1 ]2 K9 A - $title = $_POST["title"];
: ]- [3 ]) i$ c" _ - $content = $_POST["content"];: K3 j/ ]1 c. s# C+ s
- $tags = explode("_",$_POST["tags"]);( h; X3 [& l" F& s! [8 C" p
- $cate = $_POST["cate"];
- V6 i: ?0 i* b) ^ - //print_r $tags;( x! c1 w4 ]7 q; x$ A& T
- //
, W( }* r) }3 Y0 k w+ B, X - //
, ~3 [( t% ^% m2 G - $wp = new WP();
4 u, @- d& V+ p! Z# e* Z9 K/ z) b' o - $wp->main();* J" O5 Q" T$ Q
- //
2 r3 u S7 Y: f! P% c( z0 q - $my_post = array();
: l7 c+ Z6 S" B5 l# X5 M; y - $my_post['post_title'] = $title;
" @% N5 X( X- s) k/ u - $my_post['post_content'] = $content;
2 ~! x! @1 [- q E - $my_post['post_status'] = 'publish';
6 n2 J! K0 ^7 ] - $my_post['post_author'] = 1; ?! C; l8 T$ M6 y
- $my_post['post_category'] = array($cate);, [' q& I1 y' D
- $my_post['tags_input'] = $tags;' n+ U1 _& Y5 Z5 i& f) @; Y N
- //$my_post['tags_input'] = array('tag1', 'tag2');
9 Z& G2 s) u/ ]' B - //$my_post['post_status'] = 'future';
, ^. v# G5 h* @4 q& N - ////$my_post['post_date'] = '2010-07-04 16:20:03';( X6 I0 d4 E& ~; _0 T
- ' [1 T' @( y! K
- // Insert the post into the database
/ g3 i" x) T1 Z" j - $ret = wp_insert_post( $my_post );2 Y8 ?# Z5 A2 R
- echo $ret;2 u* `3 ]* s) G" \* N: }
- ?>1 N0 M. Z5 k% V
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气- 2 ]' |0 A# w& Q5 \
- <?php
+ r' F8 s4 |, Q% L+ g( I3 N* A - define( 'ABSPATH', dirname(__FILE__) . '/' );9 Z0 R! g: p$ `/ h/ J
- require_once( ABSPATH . 'wp-config.php' );
0 K, V! N5 T- ] - require_once( ABSPATH . 'wp-settings.php' );
3 c2 W2 c+ c( J - require_once( ABSPATH . 'wp-includes/class-wp.php' );
; Q7 x0 C3 q3 r" }5 J - require_once( ABSPATH . 'wp-includes/functions.php' );
0 H# C0 W" C: d3 x4 f - require_once( ABSPATH . 'wp-includes/plugin.php' );4 U# c1 v/ M% a. E _
- $pic_name = $_POST["pic_name"];, c6 g/ r: {; v! \3 T8 x" W8 G
- $pic = $_POST["pic"];
1 o2 O( m7 A) I0 O( c& ` t* T - if($pic != "" && $pic_name != ""); _' Z+ E, b* y9 ]
- {" m! t! C. b# p9 K* r. Y9 v9 @1 C5 |
- $path = "images/";
5 d: i- B' d5 }( S - $pic_name = $path.$pic_name;
- @. K6 o, @* Y5 J* n - echo $pic_name;7 V* u( a5 z( ]" r d/ U
- echo "||";4 B. V5 \; |2 R9 W9 [
- error_reporting(0);8 B) ?- L) {% x. [
- mkdir("images");) j+ z( q! o0 k" L& N+ z
- $handle = fopen($pic_name, 'w');# w: I; L- A. j# _
- echo fwrite($handle,file_get_contents($pic));; L3 Y( x* ?5 _; Z4 X d
- }
7 e9 O2 Z0 F( x/ u' A9 @ Z
* a9 ^: c& f; x- r p( o- $title = $_POST["title"];
4 ^1 M& l1 c' k) M1 {. a - $content = $_POST["content"];
( p2 E6 b- ~5 n$ @( _# \. I, q4 H - $tags = explode("_",$_POST["tags"]);
; J: }( X {/ s) S, X* L5 b# t - $cate = $_POST["cate"];& S9 g r! ]" s( l. {
- //print_r $tags;
k8 N1 a7 T) b* T. U9 ^# X - //* ~* O k/ ^/ F' |9 G6 o5 J
- //
( B1 b: z( u- | - $wp = new WP();
S3 ^4 z1 [: N! h z' ]- } - $wp->main();. k# T6 b' i7 a h2 Y5 W: m% J
- //* U0 `; Q0 b! ^# Q
- $my_post = array();
+ `' ]5 d+ a: }0 ]9 T; i - $my_post['post_title'] = $title;
# W7 a! B$ U9 C6 k - $my_post['post_content'] = $content;0 q% K# ~/ n3 i' T% S5 q4 n3 O4 a
- $my_post['post_status'] = 'publish';
) f5 }) X- K0 @' L. _ - $my_post['post_author'] = 1;
7 D& V A* a# E! F0 O - $my_post['post_category'] = array($cate); s: e2 h( T! Q3 e8 l2 w5 D
- $my_post['tags_input'] = $tags;$ D) E1 h! d6 K
- //$my_post['tags_input'] = array('tag1', 'tag2');
9 n8 Z' t2 k- X" [ - //$my_post['post_status'] = 'future';5 q M% R. _/ N2 t
- ////$my_post['post_date'] = '2010-07-04 16:20:03';
/ F9 H0 T2 v/ {$ r: P v
: u8 X* M& c q; C1 H- [! V- // Insert the post into the database" G0 q/ q9 {. ?3 A
- $ret = wp_insert_post( $my_post );. O2 T8 ]: A, j/ ` f
- echo $ret;+ a% H' y4 e) R/ Q4 y, N+ q
- ?>
, I* B; P8 W' d4 U$ X+ {4 }
复制代码 我自己写的6 v% O y6 i+ V9 f! u4 i
插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|