|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑
$ |2 r. n; Y! C( l; _6 y# E/ c4 ]- M$ y1 G/ d. d5 V+ Y4 ~
直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持3 T. o. c1 Y- C5 c! N, y
0 B( x" v+ j2 o' F3 x3 X
我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了
6 w, C# w7 A1 t, m! W6 _, ~. k
% y% P6 ~7 n4 B+ v1 D% P. r. }方便实用 简单 何乐而不用呢
' M5 {8 r: |2 h6 [7 D) C# J8 b% W- <?php; Y2 s$ ?* S! z" k2 |
8 o, L' k. h1 V- define( 'ABSPATH', dirname(__FILE__) . '/' );
2 x3 ^% \. x4 p8 l9 L Y - require_once( ABSPATH . 'wp-config.php' );2 `2 _ T% t* G
- require_once( ABSPATH . 'wp-settings.php' );' P% t: S: \' Z# z
- require_once( ABSPATH . 'wp-includes/class-wp.php' );
/ c2 l! g7 e- e4 Q- N" L - require_once( ABSPATH . 'wp-includes/functions.php' );7 u- r* Z! N; @+ r
- require_once( ABSPATH . 'wp-includes/plugin.php' );6 J9 R& Q, y2 h
- $title = $_POST["title"];
7 ~+ Y; o/ {& p$ {7 j - $content = $_POST["content"];
+ X+ o' D \0 ?3 T5 f& k' P - $tags = explode("_",$_POST["tags"]);
, @) d$ y- p0 \& r( K) ~7 A - $cate = $_POST["cate"];8 U. @9 H8 B: C7 t$ C
- //print_r $tags;
/ i" p" T: y; Z" f3 t! M - //
- f6 {9 m) f8 C- P3 V+ c - //
: |+ a6 [; t8 {, b$ P) l - $wp = new WP();
c- J+ @( y* K8 b5 c - $wp->main();$ e) I% V( s* E: ?, U
- //
& e$ T4 W' V- |5 r9 {7 E - $my_post = array();: N8 V! z7 y/ {* L
- $my_post['post_title'] = $title;
* Z8 q W/ y& ^4 p8 l F6 y. m - $my_post['post_content'] = $content;$ U+ q7 @- x6 m- T: y% d% ~1 x
- $my_post['post_status'] = 'publish';2 n! s9 U. \: ]+ z2 @& |6 R2 ^) Q
- $my_post['post_author'] = 1;& r# S9 g' I: P4 X m, R" G' o) G$ Q
- $my_post['post_category'] = array($cate);
5 J3 N* B9 w& t# n1 ~: K/ d: C5 g - $my_post['tags_input'] = $tags;
" J0 e* P) l& o4 Y - //$my_post['tags_input'] = array('tag1', 'tag2');+ c3 u; t8 {: ?8 \: N
- //$my_post['post_status'] = 'future';
5 B/ F; @, A0 O, p& ]4 t1 \7 w) J/ _ - ////$my_post['post_date'] = '2010-07-04 16:20:03';
) y, Z8 E7 W# |6 H5 k: M# z7 D - 3 W7 K- Q" ^- L4 J5 n
- // Insert the post into the database
6 g$ T5 ?6 U+ f - $ret = wp_insert_post( $my_post );' m; E& }+ u4 K) X3 g, T
- echo $ret;
+ L! ]( {* d! ]. f# n6 E7 {2 o - ?>5 B6 R) E+ P& W5 O5 r4 z* N
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气- 7 @, Q; f8 K9 n7 i
- <?php2 n" q: k: d% J
- define( 'ABSPATH', dirname(__FILE__) . '/' );
% d" Z5 _( y: l. n$ w+ |, H - require_once( ABSPATH . 'wp-config.php' );
+ C( v! `3 i8 N0 d! c - require_once( ABSPATH . 'wp-settings.php' );6 {' v; i- v p+ V
- require_once( ABSPATH . 'wp-includes/class-wp.php' );' d5 m* ~" E0 p6 Z$ w
- require_once( ABSPATH . 'wp-includes/functions.php' );( o5 L- S9 d0 L# C0 [- Z9 D' h
- require_once( ABSPATH . 'wp-includes/plugin.php' );7 G, k4 \, _3 b& l- I6 v
- $pic_name = $_POST["pic_name"];, e: H/ Q( s; j9 K; z# S* L
- $pic = $_POST["pic"];/ S( S( M& G3 ^# H# L: M
- if($pic != "" && $pic_name != "")) _1 b, g6 O! }; T
- {
: C; r( b' M4 F/ Q! X! X - $path = "images/";5 s5 }, s4 ~7 x, K& H8 {
- $pic_name = $path.$pic_name;
" n* x7 Y7 G8 C" T0 j - echo $pic_name;
3 j8 O) ^9 g8 S( r/ S0 }9 P- O - echo "||";7 |# p' e; `. l2 Q; x: ?
- error_reporting(0);; o9 y, _& W B0 Z& s+ H
- mkdir("images");
, D& @# h/ x. v2 d. ^4 U& W, j V' J6 O - $handle = fopen($pic_name, 'w');
! P" I t% c: ^# p) l$ ] - echo fwrite($handle,file_get_contents($pic));
5 e) T; C) [/ m$ O: O - }
/ H$ b+ V' _; ], B* N
9 D7 [$ G' T! j3 ^3 K8 F$ p7 j- $title = $_POST["title"];1 I7 S2 F# T8 k' A% h5 ?
- $content = $_POST["content"];
: e6 V, M; i" G6 X: f% h& O6 _ - $tags = explode("_",$_POST["tags"]);
& V0 e U3 D& R2 u% Q3 o - $cate = $_POST["cate"];
) p5 _- z5 C" ^ - //print_r $tags;
7 U% {# ]3 V' w( d( ] - //
1 _' Q0 Q- I# i5 o3 Y! e2 u - //
" x6 {. t3 h% C* z# O) M: U8 _ - $wp = new WP();
% g: y, t$ S6 l - $wp->main();0 F& l- H8 f+ F @5 g/ `+ T
- //' q! v9 A* @: t0 a8 [* K* i8 a9 Q) |
- $my_post = array();
, W4 f3 d6 \- S% Q% H: i% ^ - $my_post['post_title'] = $title;
- L$ Q- y2 v2 ^ - $my_post['post_content'] = $content;
+ d3 B# P1 L1 P( B$ J - $my_post['post_status'] = 'publish';) c: U3 Z+ o* H( |( k1 S- X
- $my_post['post_author'] = 1;
9 {% r- d/ R. J - $my_post['post_category'] = array($cate);$ ^* L) Z3 X# F4 H( j* A0 ?
- $my_post['tags_input'] = $tags;
0 Z3 ?1 R5 ~' Q# V" F) V- ~ - //$my_post['tags_input'] = array('tag1', 'tag2');
" W/ h0 J u4 b5 l - //$my_post['post_status'] = 'future';( ^2 ]9 b3 H1 S/ L. b+ q' M
- ////$my_post['post_date'] = '2010-07-04 16:20:03'; d2 `3 }2 ?* v
- 3 _( ^+ U% H! O6 H/ l' h
- // Insert the post into the database D- n5 d" Z9 w0 U9 g
- $ret = wp_insert_post( $my_post );
0 o# e; K) i i: g) }9 o - echo $ret;
* I3 B, m5 m6 M4 K) W* W' x - ?>- Q+ i7 O/ S S7 Z. k( U( f0 S( t `; f
复制代码 我自己写的
' C0 r P8 A% I) y5 [, K2 S0 n7 N插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|