|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑
# S* M J9 k% t6 f- p& m1 u, K2 x0 E% F7 g4 D
直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持/ _2 [6 j- G$ `. N5 M3 a! K2 w- V
2 b! H: O1 l/ `+ \我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了
0 S* ~' @5 d) j9 \+ s( N9 j, |% S U+ ]# M( N) v
方便实用 简单 何乐而不用呢- 2 b6 h* T* Z+ L, f' D8 u1 ~5 P
- <?php: w9 l$ |3 P5 y; ?' M" v
! `5 Q4 D+ O5 T- define( 'ABSPATH', dirname(__FILE__) . '/' );! x: ~5 h5 W8 L$ {1 |
- require_once( ABSPATH . 'wp-config.php' );
$ Y' G, @" }6 F2 {5 X' _. O% R& g - require_once( ABSPATH . 'wp-settings.php' );
( j5 G& M' B" y. F( w) I" S* v9 p - require_once( ABSPATH . 'wp-includes/class-wp.php' );% \ u) b9 ~4 w2 ~: Z
- require_once( ABSPATH . 'wp-includes/functions.php' );
9 l- x" l: ~4 w& }0 ^ - require_once( ABSPATH . 'wp-includes/plugin.php' );
" ]8 r, ~3 ~ B9 @: m h( C - $title = $_POST["title"];* _+ f. b: x% W
- $content = $_POST["content"];
0 m8 Q; \* z5 Y# } - $tags = explode("_",$_POST["tags"]);
7 j$ L! w( Y' g3 |/ B: ~2 S - $cate = $_POST["cate"];
- q# R, W( B$ x2 y7 S - //print_r $tags;, A' t( T' J0 z7 i5 X/ g
- //1 I) Z& [3 ?; X( C7 \
- /// K$ K& F" [! d9 c; ^
- $wp = new WP();
5 G! i9 S U5 N2 C - $wp->main();
8 S: {: U+ }$ D+ O4 Z) v - //
( _% {+ t3 s) ^4 }3 D& M$ L0 | - $my_post = array();% h3 ]7 P% Y% I7 f9 i: y
- $my_post['post_title'] = $title;
6 x' x0 `0 l1 l. x$ ] - $my_post['post_content'] = $content;9 {$ A( S1 s$ l7 S2 l( m
- $my_post['post_status'] = 'publish';& Z( y& g! a @ [9 c F
- $my_post['post_author'] = 1;- z i6 ^. e! b* k" F
- $my_post['post_category'] = array($cate);
8 D* [$ ? g5 A# {! G - $my_post['tags_input'] = $tags;
' ^, U* i' J# G, e8 J9 Y/ A J: w" e - //$my_post['tags_input'] = array('tag1', 'tag2');( K$ H) H2 W) V6 ?& u
- //$my_post['post_status'] = 'future';
3 C3 `8 F( C$ n+ E6 O' M - ////$my_post['post_date'] = '2010-07-04 16:20:03';
5 u W( m! N7 @! i7 o - 4 o/ F6 ^+ a, E& M! v5 n
- // Insert the post into the database
6 M& ]0 Q8 ?+ C. z* [* b - $ret = wp_insert_post( $my_post );( k5 k a; {# q
- echo $ret;
$ d$ m( j& _. Z) c- ^ - ?>- j* c P( ~6 d
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气- 9 E$ {3 }7 y1 `) J/ o: S( z
- <?php' T$ }! z7 X. o
- define( 'ABSPATH', dirname(__FILE__) . '/' );. t7 h1 ~" X7 }
- require_once( ABSPATH . 'wp-config.php' );4 C. x7 m8 |- U+ f4 o& f; P; m
- require_once( ABSPATH . 'wp-settings.php' );
! D& _! B4 ]8 r, G - require_once( ABSPATH . 'wp-includes/class-wp.php' );
) }! F r" U- {$ c - require_once( ABSPATH . 'wp-includes/functions.php' );: i1 g ?1 m* Q" [. S
- require_once( ABSPATH . 'wp-includes/plugin.php' );
q$ [) G. i& X - $pic_name = $_POST["pic_name"];3 j# A& d1 I4 f2 M C7 }# V
- $pic = $_POST["pic"];$ ~1 w) {1 r( g% g, c( J/ F
- if($pic != "" && $pic_name != "") F. Q' U; R4 o, S. c0 T% A& s
- {' e1 Q" N! J: D+ J7 G( F/ H8 l4 P
- $path = "images/";
+ U3 M0 f$ X( x6 m - $pic_name = $path.$pic_name;) Y% I& f% g. @& F7 Q
- echo $pic_name;* [, `3 R5 n4 y z7 |, Q, f2 J |
- echo "||";
0 T9 [3 U, K! m7 u1 S - error_reporting(0);
7 E2 G t8 m7 B' R+ Q6 x - mkdir("images");
7 p+ i0 ?1 t: E( G- w; n - $handle = fopen($pic_name, 'w');
3 h0 F& k4 \% m) [ - echo fwrite($handle,file_get_contents($pic));5 y1 E F0 i$ y* J- L1 f
- }
& F( s% \- T( J* x% E. N8 ~
& q, w, i, l, m# m- W& Y" K T5 u- $title = $_POST["title"];; _" `2 r% \! D- i1 I6 O$ ]5 E
- $content = $_POST["content"];. L( @0 K' t _( i
- $tags = explode("_",$_POST["tags"]);
. e( \& |1 s7 s: [9 _ - $cate = $_POST["cate"];
& J5 u& @, E. J2 P - //print_r $tags;- U' t# T5 i4 M5 m5 d
- //
8 D O. l; T/ y4 {. L$ e9 D* ] - //3 ^- V6 e1 s/ C
- $wp = new WP();8 k8 n% q& \! k- r
- $wp->main();8 I9 S- Y( v+ m: Q: Z! e7 V
- // I- O" f4 Y* F j4 t, V
- $my_post = array();
B2 b6 p# U1 v3 }5 k - $my_post['post_title'] = $title;
: q- ^0 y6 S- M- c$ C. [/ R) U3 X - $my_post['post_content'] = $content;
+ C0 Y- R# B# ]- i0 b$ W% O - $my_post['post_status'] = 'publish';) A* K" T7 O* G {0 _+ `, p$ `
- $my_post['post_author'] = 1;
8 ?2 T$ n8 x7 Z! m) ]9 p - $my_post['post_category'] = array($cate);
: r# P5 e) [& \/ o, d: ^* l - $my_post['tags_input'] = $tags;0 I. K3 v8 {4 g9 l) P6 _
- //$my_post['tags_input'] = array('tag1', 'tag2');
. g$ L) ~9 j4 ^+ F - //$my_post['post_status'] = 'future';+ {0 ^) k. O5 u: O5 d. e. L- l
- ////$my_post['post_date'] = '2010-07-04 16:20:03';: i! q- ~/ v1 b& R
* w7 b* s. Q1 p% h g6 ]- // Insert the post into the database) X0 L1 M" _5 g: {
- $ret = wp_insert_post( $my_post );: y/ u: M4 n& }- R+ L5 Y
- echo $ret;
" t1 T! U9 f/ ~ - ?>
3 E9 `" P+ ^ g% D
复制代码 我自己写的
5 |& c* R( R5 T. n% J1 u插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|