|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑
1 o% q% P! t' s: J) m4 Y5 e8 l$ c3 l' M$ p7 C+ @: N% E: @
直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持$ A, Z$ u, t$ I1 g( [
7 f5 E, i8 T. e# T* E4 V" n
我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了: U+ ^# {" A; U- |
* o9 K Z e V( E6 z) Y
方便实用 简单 何乐而不用呢
# ^3 q) m/ N& ?3 m- <?php: `3 k( C% C% `8 a& }
8 T1 z7 t* u9 h ^' v. j1 F7 v0 I) Z- define( 'ABSPATH', dirname(__FILE__) . '/' );; J- ^& W0 e3 J1 ^
- require_once( ABSPATH . 'wp-config.php' );# ]& z) x }( s5 O$ Y) f
- require_once( ABSPATH . 'wp-settings.php' );" u! o6 N* P, k( z
- require_once( ABSPATH . 'wp-includes/class-wp.php' );& w+ I' j( E6 J2 j+ [' _- G3 V
- require_once( ABSPATH . 'wp-includes/functions.php' );
( a6 Y9 J% L2 N - require_once( ABSPATH . 'wp-includes/plugin.php' );) D6 H' M& ` d/ G6 W2 @
- $title = $_POST["title"];; d% q& w2 x3 C7 d5 N5 v
- $content = $_POST["content"];
6 B& _4 R P1 { N0 I1 ? d" A, j% G - $tags = explode("_",$_POST["tags"]);+ a Z" O r! g+ k
- $cate = $_POST["cate"];
! y Q/ C0 Q8 I" T% E - //print_r $tags;, A. }1 ?' P4 `; {/ p, I
- //' c4 n: I+ I1 n( }7 ]$ ?* Y* K, t) I
- //6 P7 ^( {; G0 ?
- $wp = new WP();
0 H1 M+ s4 a6 D6 M! i - $wp->main();) D1 N4 _/ U- W
- //: X; j. N# O7 x# V; q. Q
- $my_post = array();
, G9 l) S' n& ?, S5 A0 ] q# M3 M - $my_post['post_title'] = $title;
3 n1 u3 D4 p+ @7 Q3 u/ M0 H - $my_post['post_content'] = $content;5 {/ N( J0 ]+ y( h. Z+ Y9 ~* o' B
- $my_post['post_status'] = 'publish';
8 G+ ~. w* Z7 T' }% a) f - $my_post['post_author'] = 1;
9 N: H- y* M5 |) O7 e3 e6 P) f - $my_post['post_category'] = array($cate);
0 }/ i& l- l0 Z$ F8 G8 }. C) H - $my_post['tags_input'] = $tags;
0 N( F* Y6 G' i0 s3 d+ l - //$my_post['tags_input'] = array('tag1', 'tag2');
* ^8 V( ~2 L) G" i; ?# e" l8 @. R3 @- M - //$my_post['post_status'] = 'future';9 X/ T6 a5 O7 R/ x$ |4 f0 z
- ////$my_post['post_date'] = '2010-07-04 16:20:03';
: ~1 N5 Y: i; L$ @) ]- P! Q) c - ) W3 a X* m; [5 b3 [9 x
- // Insert the post into the database' u0 y4 r* x" R: F0 Z
- $ret = wp_insert_post( $my_post );
2 ]$ V7 P Q6 l# L - echo $ret;7 c7 o2 A5 ~$ r0 G. x& u0 ~6 s
- ?>/ @" L( o n/ o. O7 v7 C" Y
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气
5 J. F0 F9 k, Q% K- <?php
2 \! `: ~6 y* i0 R; m, j - define( 'ABSPATH', dirname(__FILE__) . '/' );
* W+ t6 r- P+ [3 N- O, | - require_once( ABSPATH . 'wp-config.php' );
* \+ q: O! e9 g2 `0 l) G2 l - require_once( ABSPATH . 'wp-settings.php' );
9 c8 @" n I8 [3 y - require_once( ABSPATH . 'wp-includes/class-wp.php' );& u& r$ D+ K- j' l* D1 f' n+ o
- require_once( ABSPATH . 'wp-includes/functions.php' );7 {! n/ Z" Y4 D4 e: X. o, Y; Z" y
- require_once( ABSPATH . 'wp-includes/plugin.php' );' g6 A/ {' s& o j; o O
- $pic_name = $_POST["pic_name"]; {2 v0 U9 `5 r& a% _
- $pic = $_POST["pic"];
0 n* x1 @. }" H N- A* |, F# ] - if($pic != "" && $pic_name != "")
+ Y% k6 |+ R7 q - {
/ G/ c; j& n6 I2 @' M& ^' Q @ - $path = "images/";! m6 l- c! h: x
- $pic_name = $path.$pic_name;
# ~/ y" h- a; g9 T4 Q' D - echo $pic_name;. e; D" W/ j+ x
- echo "||";
W7 Z9 _$ L# A" B& e, S - error_reporting(0);
^9 f( g9 G" p7 S - mkdir("images");
% v, c4 I) h$ a* o2 C - $handle = fopen($pic_name, 'w');& e* B9 C, W& E* f
- echo fwrite($handle,file_get_contents($pic));2 Z+ ~; i8 U. E. K
- }
' G% c, q0 H2 ]( i# Y$ @# u: D - 0 |% S+ M u: z! n# q$ k
- $title = $_POST["title"];
4 B: R2 \& B$ m" H# F - $content = $_POST["content"];" j' w" V* n6 l8 @9 z p# R! u
- $tags = explode("_",$_POST["tags"]);2 R0 P) q9 T" @6 i& Q
- $cate = $_POST["cate"];
: y" R! {2 D: G% u4 | - //print_r $tags;
: i% P7 ]* s' \ - //
# n3 h1 m2 D6 h. o - //
. j$ w) g1 A7 {8 O3 [: M1 Z - $wp = new WP();
- V3 h; x" M1 i$ `- b# j - $wp->main();/ k! \+ M5 u2 g( C; T$ S
- //) G7 B& X( i9 V$ V" S0 {
- $my_post = array();
$ O# R) {, d( `4 w+ Q' M - $my_post['post_title'] = $title;2 @* {3 n6 F) `: \+ F; Q7 @
- $my_post['post_content'] = $content;
. G9 i2 J, ?7 _8 b& P - $my_post['post_status'] = 'publish';
' H' d" G& C; P- [- r2 g q - $my_post['post_author'] = 1;3 o! v! t2 R. N* J& l: r# {
- $my_post['post_category'] = array($cate);
& s) Z- u- b" t* ~* N( ] - $my_post['tags_input'] = $tags;/ k2 t: U( c. l
- //$my_post['tags_input'] = array('tag1', 'tag2');5 m: s/ o4 j% i" A5 ^# k
- //$my_post['post_status'] = 'future';
* X0 w$ j) h9 p" r; o7 g# d& r* v* o - ////$my_post['post_date'] = '2010-07-04 16:20:03';
1 k8 a ~5 `/ m+ _
$ C) z: M# p1 D' |1 l' |' `- // Insert the post into the database1 j o# t& ]9 R2 f: y7 S- k
- $ret = wp_insert_post( $my_post );8 F" Y6 D1 b+ I/ i7 E
- echo $ret;
( j/ e, F) m) p9 l3 r - ?>2 x2 ]; ~: m2 t* \
复制代码 我自己写的; B8 f' g+ x, \5 C- h5 M/ T& }
插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|