|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑 4 r2 g. U3 ]. T5 \
7 z r3 k6 d. }3 G6 @' v直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持( }+ s' q9 {# G5 E
0 y, v1 P% v) }4 _5 m% n* @我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了
. _' y* c2 D( [: i8 y/ H( I* l+ E9 A6 ]* `
方便实用 简单 何乐而不用呢
. I' N6 S1 I9 e0 T- <?php
# G8 `* V8 w( w. W - 5 M0 N( H2 [: W4 P% z
- define( 'ABSPATH', dirname(__FILE__) . '/' );/ z; q' g9 E ~0 u) f
- require_once( ABSPATH . 'wp-config.php' );
% c+ P% @. W* ~" H6 f8 F# m - require_once( ABSPATH . 'wp-settings.php' );
9 o9 c0 M+ f h( [7 X - require_once( ABSPATH . 'wp-includes/class-wp.php' );
4 |2 j4 {" b, M) O' c% d: r - require_once( ABSPATH . 'wp-includes/functions.php' );
1 G* `" \5 [; d) r5 e- l, x6 S, ^ - require_once( ABSPATH . 'wp-includes/plugin.php' );# z. o+ O' p& @
- $title = $_POST["title"];
, e2 ^% @- i* ]1 w+ X - $content = $_POST["content"];
1 ?% ]' k4 E) {: o: [6 D- q6 I - $tags = explode("_",$_POST["tags"]);9 S! z& z( M) P
- $cate = $_POST["cate"];
/ m6 J5 U# M2 k( k. o2 ] - //print_r $tags;
- p4 z3 C8 D# b0 d2 l ` - //
+ o+ a p% o( q - //& t8 w5 g0 v# G; m$ r
- $wp = new WP();5 [! f6 l0 C+ B- s/ @
- $wp->main();
# D2 e: |1 _& R: s" W1 ?, e! W - //6 s& c8 G4 ^+ B x! j
- $my_post = array();' n" |8 V H& i6 m' _. f
- $my_post['post_title'] = $title;
, R$ }! l* V: `( H7 o - $my_post['post_content'] = $content;" s8 p+ z3 o* o6 j# K5 M
- $my_post['post_status'] = 'publish';
& U" l$ O8 c2 C' x0 t+ c - $my_post['post_author'] = 1;) Y* |/ X- U5 \4 P0 ~
- $my_post['post_category'] = array($cate);
5 z* p1 T( n# z- s" }" ~7 x) q - $my_post['tags_input'] = $tags;0 l" z6 U+ F8 N) M( a& g/ T5 F" ~: l
- //$my_post['tags_input'] = array('tag1', 'tag2');
, T) Z/ O, ~! ^* ?* U - //$my_post['post_status'] = 'future';. }* |: z6 g8 F9 M% B
- ////$my_post['post_date'] = '2010-07-04 16:20:03';& v/ F1 M! |7 c6 k% g, w! S8 |
: }- R9 }% A9 V8 d- // Insert the post into the database
' j# e5 P% d- q0 H* Q& W# P ? - $ret = wp_insert_post( $my_post );1 u6 U {' N8 R+ c& N+ X9 N( s
- echo $ret;! m. k3 \7 q% i! \. p! U# E8 b
- ?>8 |+ {; L1 \, A& K" d5 Q# e
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气
4 h9 j; v! l `( `- <?php
+ h9 ~& c/ M# W* z - define( 'ABSPATH', dirname(__FILE__) . '/' );) o; a5 G. n, A1 Z, M: _
- require_once( ABSPATH . 'wp-config.php' );. p( X$ [7 q# e0 R* Q6 c
- require_once( ABSPATH . 'wp-settings.php' );
8 H% Z; v! E& C; T- } - require_once( ABSPATH . 'wp-includes/class-wp.php' );
1 S( P, ]1 F* o5 C: {" e - require_once( ABSPATH . 'wp-includes/functions.php' );
t1 j. c9 G" @( o% h4 I0 o' u - require_once( ABSPATH . 'wp-includes/plugin.php' );. M0 g) \9 X7 F' S9 H/ ]# y
- $pic_name = $_POST["pic_name"];
5 J: T& T+ U, m- L: @, h - $pic = $_POST["pic"];
/ p7 k8 x/ ]4 g! ~8 | - if($pic != "" && $pic_name != "")
. H" T7 ]- i$ O$ F. K5 ` - {2 m! b, E* i% p# e! X& u$ D5 R: ^/ b" I
- $path = "images/";
$ q, {, k5 X0 m' c- n' w. S5 p- n - $pic_name = $path.$pic_name;! R& Z0 `; H1 c" z5 U) c: T
- echo $pic_name;# S; x2 ?3 j; r; ^: [( a' v1 \
- echo "||";: p' t* B* J X: y$ E
- error_reporting(0);
% L3 m7 L8 k5 ?6 ` - mkdir("images");
% L/ }9 Q( U1 b3 f& ]/ w! h - $handle = fopen($pic_name, 'w');% E. S; `" e/ U& ~: X
- echo fwrite($handle,file_get_contents($pic));
% |5 I9 ]: W/ ^. U8 _2 ^" o3 h - }
9 u( P- q# F- i
/ w( c1 u/ }1 J& ^- $title = $_POST["title"];, W! ^/ m. C# M8 k6 I
- $content = $_POST["content"];9 T% `1 I3 t, P, W
- $tags = explode("_",$_POST["tags"]);
$ Q# y9 E6 Q& [3 ? - $cate = $_POST["cate"];
8 s( |6 @6 X% y/ Z - //print_r $tags;
( _$ m$ s9 |& V: Z - //. \, Z( _) y+ A: K; y* d, M3 T
- //
$ M9 }- j1 B' N4 t6 {; ~0 n" I - $wp = new WP();4 r/ z, z# X* w3 _* J. x" L: y
- $wp->main();
' e L e5 V4 E1 O2 a - //
$ x) Q# R7 ]" o' b* h1 z1 d! y - $my_post = array();
( g1 D5 L) u' r8 Y0 n! I0 R - $my_post['post_title'] = $title;
1 N+ y! F" g2 ^+ X - $my_post['post_content'] = $content;% j- J u5 ~0 @5 x9 I5 W
- $my_post['post_status'] = 'publish';
6 w6 X0 r" {& [1 s - $my_post['post_author'] = 1;: I2 I: I; M2 v& q
- $my_post['post_category'] = array($cate);6 X8 C; k! {9 \0 @3 \! @7 ^0 q- d
- $my_post['tags_input'] = $tags;
# ^' ?# Z; \6 R: o% I0 L2 o8 F! V% F - //$my_post['tags_input'] = array('tag1', 'tag2');
: M- s; K/ l, \) a7 ~. L+ [) G - //$my_post['post_status'] = 'future';! Z, t# C/ g; d, A7 M6 l; ?
- ////$my_post['post_date'] = '2010-07-04 16:20:03';
/ i ^, u4 C- A. m
1 H" x4 v! F' @0 w6 _/ N" c- Q- // Insert the post into the database
8 x; i I0 f8 f/ W - $ret = wp_insert_post( $my_post );
/ V! A, b* V' F t5 ? - echo $ret;
) e0 x" r4 P' ]6 ]" G - ?>
, @5 |- s& J1 j* `! S! W. x6 M
复制代码 我自己写的 y- W, [3 D2 I7 b2 u1 `9 e
插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|