|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑 # _) k; t" b. K4 u3 v2 c5 Q9 f p% p
5 J; {8 J, q9 |1 [& t8 R直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持9 }- y5 ~& T; Q3 f- L' N6 {6 D7 N. f
9 Z- ]3 }; _2 |9 M8 H% l我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了
0 g. m3 Q+ ~- O- Y, `" ?5 |5 }: t7 t0 W1 z6 d
方便实用 简单 何乐而不用呢
- V& }" B9 w& c* A! G& _/ e- <?php
6 g: N, f" u! H0 G$ i - 6 [5 e, f4 \" c8 `9 q
- define( 'ABSPATH', dirname(__FILE__) . '/' );
3 L+ E1 a2 A% N# I+ g# }( H - require_once( ABSPATH . 'wp-config.php' );
1 M6 C1 N0 F) ?2 G8 o - require_once( ABSPATH . 'wp-settings.php' );* |: T* H, D- L9 t
- require_once( ABSPATH . 'wp-includes/class-wp.php' );
( B4 s$ b' t& i. w6 g3 K - require_once( ABSPATH . 'wp-includes/functions.php' );; }+ L! i* ~ |; q
- require_once( ABSPATH . 'wp-includes/plugin.php' );
, X- z; F- d9 Q# O - $title = $_POST["title"];: \" H0 c& J. x, {
- $content = $_POST["content"];' O+ Z+ L0 Z7 W0 ]# b# Q- N$ C
- $tags = explode("_",$_POST["tags"]);
( q; Q7 E3 ]4 y$ @ - $cate = $_POST["cate"];
/ k3 e( K$ o3 y$ a: @& R - //print_r $tags;
; k: d( Z: g7 ~. y E0 J - //
/ b/ N0 n' _) T' z - //
" ^& p, }" A( j8 X. L+ b J. i- l# a - $wp = new WP();0 Q0 {6 m3 X1 k% U. G9 p( I
- $wp->main();! v: L/ P+ l; H: u0 W5 c
- //6 T+ O; P& |) @% y! N$ I# v
- $my_post = array();/ b3 v# g3 ]" r0 k6 b% C2 @
- $my_post['post_title'] = $title;
+ M5 T) a+ _9 \$ H - $my_post['post_content'] = $content;
* g a$ v* a; J+ o- ~! q - $my_post['post_status'] = 'publish';7 |$ ^# t8 v4 C
- $my_post['post_author'] = 1; c+ _, ~6 G& g' X5 v9 D8 z
- $my_post['post_category'] = array($cate);
J& }% ~ R2 j7 i7 P+ G3 \: }$ J - $my_post['tags_input'] = $tags;
2 I: C* R3 E, }1 p! E$ b - //$my_post['tags_input'] = array('tag1', 'tag2');
* x9 _! ~4 n B. V) d8 q) `* f - //$my_post['post_status'] = 'future';+ r+ {3 y# I' X/ ?1 r" ]- O
- ////$my_post['post_date'] = '2010-07-04 16:20:03';
, P @" e1 c9 ]6 e4 Y1 w) f
+ G) y t3 d+ @! o1 c- // Insert the post into the database% x+ N9 g, s6 z8 X
- $ret = wp_insert_post( $my_post );
) i7 l! b2 }$ A, s0 B - echo $ret;
8 H+ J* ?8 L, g - ?>1 Y# }( q! n) ?6 S) c2 @ Y
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气
6 S% X5 u, t" R5 R O0 k- <?php$ ^ b! P5 q! O! }; D0 ~
- define( 'ABSPATH', dirname(__FILE__) . '/' );
! _6 _/ f9 l6 {8 ` - require_once( ABSPATH . 'wp-config.php' );; D, c3 F6 G$ p( G: J7 f! r
- require_once( ABSPATH . 'wp-settings.php' );
5 y* M( F; _4 M. x' v - require_once( ABSPATH . 'wp-includes/class-wp.php' );
0 w, K5 V/ J5 B) y - require_once( ABSPATH . 'wp-includes/functions.php' );$ r/ U5 m% N* i$ B8 G" g, a
- require_once( ABSPATH . 'wp-includes/plugin.php' );
9 ]: Y9 O9 Y1 C, z' E( w% X - $pic_name = $_POST["pic_name"];3 K% t. a5 ~- I/ r3 ^3 G
- $pic = $_POST["pic"];
9 c! ]* ]" A2 L - if($pic != "" && $pic_name != "")
- r6 n9 B1 C4 P7 C& z3 h$ ] - {
8 u f5 S. M! S! b1 l+ E! K D - $path = "images/"; K6 B. Q* ]% D/ f t* x
- $pic_name = $path.$pic_name;
$ d' U# A2 l+ G, j; d4 \/ `- \ - echo $pic_name;
& g6 |: [# ?7 e# E* e \ - echo "||";
$ q) o ~$ E* ?9 Q1 k3 _% S - error_reporting(0);
& `( D5 Y* g+ s4 r! W, S! z, V - mkdir("images");
h: q3 h' E8 g; j1 j4 Q - $handle = fopen($pic_name, 'w');, d p& ^9 C' g5 g
- echo fwrite($handle,file_get_contents($pic));
9 q% ?) R+ X- e0 |7 `5 [ - }2 g2 z; S; I, K3 ?" F# v
, D) C' c3 e8 a7 K: W- $title = $_POST["title"];- ?' F, S4 _" b9 P! w
- $content = $_POST["content"];
7 N% r( ~9 Y% ?' J* A, _ - $tags = explode("_",$_POST["tags"]);+ c; A9 L5 `3 v8 |, b! {
- $cate = $_POST["cate"];
* c1 k0 U/ g/ a! X- y - //print_r $tags;$ \# ^0 v$ Y- B* V1 L- E
- //3 r) b) t" A% _8 |4 l( Y
- //
; E5 A7 d) h+ ~8 J& N1 R: { - $wp = new WP();) T" ]$ P: [/ J& T
- $wp->main();% Y* K$ M* c( X% t0 @& C8 r
- //* y* ^! m* N4 Q& w: h
- $my_post = array();
N8 }$ m Z$ U* G5 z! c1 h - $my_post['post_title'] = $title;
0 D, \$ l$ k" s8 b9 M& a - $my_post['post_content'] = $content;
; H. w6 h: L! O+ t0 I+ t, T - $my_post['post_status'] = 'publish';" t& n7 A6 z5 l, \9 W% X1 W
- $my_post['post_author'] = 1;/ K; t* ~. D( m: b& t
- $my_post['post_category'] = array($cate);
& G& l, h2 k2 F - $my_post['tags_input'] = $tags;) m. u) d3 I$ K7 ]$ l& ]
- //$my_post['tags_input'] = array('tag1', 'tag2');
9 U* Z" g3 U' \, i - //$my_post['post_status'] = 'future';0 w& O$ ] N, j& O
- ////$my_post['post_date'] = '2010-07-04 16:20:03';
" g: o: z1 d3 X7 Z5 J - # f" N G. B( E: H, k& B4 r
- // Insert the post into the database3 k; z! j1 R3 t* f6 N5 {' y7 S# A6 N
- $ret = wp_insert_post( $my_post );
; @! ?/ H& D: j7 k - echo $ret;
5 k& s5 F; F3 \. z - ?>: y3 [& x3 F: b8 m; e* e, S
复制代码 我自己写的
2 x3 P* O$ Z% S9 u插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|