|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑
& X+ F: h$ u5 k I w
% R) X0 \; n8 f1 p. e6 ]$ Z, ?直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持 k; t4 T9 t$ s' `! z
5 P3 `2 U3 m' C, y8 N7 z9 H
我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了) }. F8 s* F& R) @2 `; j4 Z
+ K, ]. B* Y8 Z ^
方便实用 简单 何乐而不用呢
* j7 f1 ?! `' r( B- <?php7 j9 P; @$ g; p4 D
- . M4 \0 k0 \9 O6 h6 X! F
- define( 'ABSPATH', dirname(__FILE__) . '/' );
* }7 {8 ~/ x( a9 l* J: z - require_once( ABSPATH . 'wp-config.php' );
8 `% O+ ?( r+ Q4 w/ E# X) j' g0 w - require_once( ABSPATH . 'wp-settings.php' );
; Z( A) ], d, Y& V - require_once( ABSPATH . 'wp-includes/class-wp.php' );+ Z) r6 y" I: [1 \) U8 e
- require_once( ABSPATH . 'wp-includes/functions.php' );
/ o) q) o1 k4 S) l5 |; A6 `& M& a - require_once( ABSPATH . 'wp-includes/plugin.php' );1 W% }4 Z! S9 H3 d9 Y
- $title = $_POST["title"];
' |5 d/ K u. Y4 N8 D% ^8 S - $content = $_POST["content"];
[1 C9 A f& u* a - $tags = explode("_",$_POST["tags"]);& p1 I) t6 S- b) k) m% r. f2 a
- $cate = $_POST["cate"];
& A: K3 |1 l: n. N* z - //print_r $tags;
2 b' v, |% ~' [, i% c( M- C - //9 S' |: D- Y& e8 I, s
- //4 p- k+ B; G4 l3 I& i+ J
- $wp = new WP();
) t8 C# ^ Z, D5 ] - $wp->main();
, h1 P2 R1 G) v7 f+ A - //+ V: T, [/ f4 \' B
- $my_post = array();
9 L8 r0 }) y5 H* c - $my_post['post_title'] = $title;1 t) [+ |2 w5 a9 W2 J
- $my_post['post_content'] = $content;( Y" m" p# n* A F Q B' v: P+ @
- $my_post['post_status'] = 'publish';
) @) Y8 C# H. f% ^$ `2 ~ - $my_post['post_author'] = 1;
7 K/ k% P; i# ~% W7 }" `. G7 c - $my_post['post_category'] = array($cate);
9 q; |7 P6 F& L. i. h8 _6 k - $my_post['tags_input'] = $tags;% U ^& A- e9 ?0 S" p; l
- //$my_post['tags_input'] = array('tag1', 'tag2');3 b! j6 M: j( @9 _/ M2 ?
- //$my_post['post_status'] = 'future';3 ~# x6 _5 S3 f2 L7 L' |- L
- ////$my_post['post_date'] = '2010-07-04 16:20:03';
$ i& e9 H: `& o3 n
7 @* S5 G" }0 p- // Insert the post into the database
8 b' ]! i/ z/ M3 O+ ?; _ - $ret = wp_insert_post( $my_post );: W+ y5 P7 a" n# Z, P% ^! {
- echo $ret;* E0 `# j- o! }/ E, h8 M/ L
- ?>( z: e {6 Q" A% `
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气
1 F) e# T6 S: X( Q/ s# i- <?php
1 H5 M& g6 C: K - define( 'ABSPATH', dirname(__FILE__) . '/' );: [' w0 L: i( d" r3 I. M1 h
- require_once( ABSPATH . 'wp-config.php' );
0 L& i* {7 [4 r1 X7 H - require_once( ABSPATH . 'wp-settings.php' );4 K5 ~& s( y+ [% X
- require_once( ABSPATH . 'wp-includes/class-wp.php' );
8 z! V% [4 f9 M! Z6 B - require_once( ABSPATH . 'wp-includes/functions.php' );
# m$ `. u, [2 v' ]0 E$ R3 v - require_once( ABSPATH . 'wp-includes/plugin.php' );" g8 w" j. l3 w5 j- I+ e
- $pic_name = $_POST["pic_name"];4 G- O) v& q/ g- ?9 Y: T
- $pic = $_POST["pic"];8 w+ k5 Y& c8 y. p. ^
- if($pic != "" && $pic_name != "")% f) @ H) R1 j0 @* s
- {# d9 N" Q# E- | \ W0 z
- $path = "images/";
9 N# q0 L9 H7 Q - $pic_name = $path.$pic_name;
& L9 T- U/ `+ S/ _. O. B7 U - echo $pic_name;
8 Q, M+ N" H$ g$ {: p- H1 O - echo "||";
+ o3 l, x( h: X) M+ Q - error_reporting(0);; ]3 q" Q' \, y$ q" e% C
- mkdir("images");
" _/ }& {% o2 A: e: Z - $handle = fopen($pic_name, 'w');
8 F. g. z ^9 Y3 X8 O) } - echo fwrite($handle,file_get_contents($pic));% ~2 ]6 h; M* J/ w- y' m
- }: {( h2 F3 j& i; b# l' k! Z' k
1 U9 I8 S$ d7 L- $title = $_POST["title"];
+ C. l# j \0 U, V7 @) ^6 y - $content = $_POST["content"];' S' b/ t9 I6 m6 Q# S: k; U' `
- $tags = explode("_",$_POST["tags"]); _3 V2 ?/ e1 m% t. w" c* o! Z
- $cate = $_POST["cate"];* O0 {. {* p; I; x; ^
- //print_r $tags;
! ~8 @9 L5 M" U' {. b% x - //
) \7 Y7 a" S: n) Z* x - //
1 Y, r6 j7 m) \! J, P+ H) [1 A - $wp = new WP();
( c2 j. L. t3 o8 s - $wp->main();( W, d3 O9 \- Z" T7 q
- //
0 {0 ?. K2 n7 J& r% o; l( u - $my_post = array();" J3 t. N8 i' W8 f
- $my_post['post_title'] = $title;6 C4 t0 b( g8 f0 M* a# E9 R
- $my_post['post_content'] = $content;! E; Y* U6 ` ]7 i/ U }
- $my_post['post_status'] = 'publish';
# A! C$ ^$ A& k8 m - $my_post['post_author'] = 1;7 q4 O$ d' {0 o! O. N1 K
- $my_post['post_category'] = array($cate);
4 t& g) x! ?; x0 ~ - $my_post['tags_input'] = $tags;2 H8 p5 m4 K/ k, ^+ B+ M
- //$my_post['tags_input'] = array('tag1', 'tag2');
3 p1 n) I) b: H; j n I- o - //$my_post['post_status'] = 'future';6 ^* W1 `* c" c0 F5 L* q8 D" f
- ////$my_post['post_date'] = '2010-07-04 16:20:03';
$ a( j9 ~ Z1 u5 H - # R/ f8 S, f& O& b! @5 }' s) U# ?
- // Insert the post into the database
$ k) Z% e, |- o C& B+ W( D" M - $ret = wp_insert_post( $my_post );( v( v) i X, N3 a( \& R
- echo $ret; W$ f4 d/ U5 U9 i, K4 M
- ?>" K5 D# M! X1 r/ s' ~7 j
复制代码 我自己写的
6 p) f( \& ?# n8 [' H+ K2 F插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|