|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑
6 C- O6 J1 I! E. X, A
, a* _ R$ F9 G9 ` P" w' p# s/ ^直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持/ q- Q' o: Y9 w% h. i7 ]( }
4 H0 z! o# F6 i7 i+ t! D: i/ t我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了: `$ H+ q; H3 B5 A1 \0 h
/ B7 q1 D4 N: H% e0 @6 J; z
方便实用 简单 何乐而不用呢
1 q w* _/ Y$ {7 U- <?php* J" B; t; F/ X, D+ Z
G, ~5 A( q: y- define( 'ABSPATH', dirname(__FILE__) . '/' );7 y7 `' E3 f+ P; O) X( ]
- require_once( ABSPATH . 'wp-config.php' );
# f3 j' r- A8 c, i6 q Y% K A/ { - require_once( ABSPATH . 'wp-settings.php' );
. v8 L2 R0 c3 |* @; i0 R' S. Q0 L - require_once( ABSPATH . 'wp-includes/class-wp.php' );
: j1 N; Z: ? v0 d$ n" I - require_once( ABSPATH . 'wp-includes/functions.php' );
% S% q3 b* O# R7 J - require_once( ABSPATH . 'wp-includes/plugin.php' );
8 |5 r; o0 c% g4 H, a8 U6 e) |" I* u - $title = $_POST["title"];
5 j% j$ G3 g. i3 C- o! x/ G - $content = $_POST["content"];$ F2 U2 A: x0 f: K
- $tags = explode("_",$_POST["tags"]);
6 v6 M( y: R5 M( l - $cate = $_POST["cate"];3 }+ ~- w7 T" h3 }: \/ [
- //print_r $tags;
" j# m& L- p! j/ V! A, Q- `! T" I - //* w0 G. ]& r- y/ S5 J3 S
- //
- { f2 j5 d7 k$ g) c9 S5 m/ H - $wp = new WP();
' V3 `' x8 c+ j9 x - $wp->main();+ N4 A0 A A: h( i
- //! |- d% P" L8 N( r2 B
- $my_post = array();
2 [2 |$ W6 u7 P& F* A - $my_post['post_title'] = $title;) \5 O' L; u6 I: `! V
- $my_post['post_content'] = $content;
& B( }+ w0 K% y9 g% s - $my_post['post_status'] = 'publish';& b( s: S/ ]3 l) n. q$ M
- $my_post['post_author'] = 1;
7 M* y* U8 l6 H: a! l6 w( ` - $my_post['post_category'] = array($cate); f3 f- K9 m) C; P" B7 c
- $my_post['tags_input'] = $tags;. X2 B, d+ |4 h `
- //$my_post['tags_input'] = array('tag1', 'tag2');! _5 _( V% R! p+ S& |
- //$my_post['post_status'] = 'future';/ e3 F- E& i4 `5 f- C" A* Z
- ////$my_post['post_date'] = '2010-07-04 16:20:03';$ h* |" a1 i2 d
: D" ?3 J7 M3 V5 Y3 p+ G- // Insert the post into the database
: } q6 F5 ~% r: a7 A - $ret = wp_insert_post( $my_post );
. ~4 y, j3 Z; }, n - echo $ret;
, Y- F7 b* y) J/ d* Z. Y4 x; v8 A - ?># E2 X& Z f5 c. i1 r1 j" @
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气
4 _5 q: S8 ~/ C8 o; S- b6 A- <?php) i% i- \2 S: ]7 v" ]
- define( 'ABSPATH', dirname(__FILE__) . '/' );
% A0 V4 B2 K4 `' R4 v7 b- r! [ - require_once( ABSPATH . 'wp-config.php' );
8 y7 l# I8 V& K6 O - require_once( ABSPATH . 'wp-settings.php' );
5 k! A3 m C; e) B5 R - require_once( ABSPATH . 'wp-includes/class-wp.php' );
' X$ H# ?# i9 d" x) T - require_once( ABSPATH . 'wp-includes/functions.php' );
: R0 @5 ?: [/ s* b9 b. d2 U: X - require_once( ABSPATH . 'wp-includes/plugin.php' );6 u" j# Q$ e- O# |3 Z1 i
- $pic_name = $_POST["pic_name"];4 r' U4 k1 {8 d: \: d' j
- $pic = $_POST["pic"];
7 J% q7 T, R$ o R$ K - if($pic != "" && $pic_name != "")
6 n9 P' _8 s) T( ^# f - {
* k, H0 c0 K( P& O' D- A - $path = "images/";
, ]; u" Y. m; x( V# B - $pic_name = $path.$pic_name;
- B4 l" Y( ^5 i0 U( ` - echo $pic_name;
! e7 F6 }9 Y% s: b8 K2 I - echo "||";
; V; H4 t3 Q# K% h8 R" C - error_reporting(0);
8 y0 o2 w. R, G3 V - mkdir("images");% M* z4 W& ~+ f+ v
- $handle = fopen($pic_name, 'w');
1 W' h6 [8 n5 \5 f9 U- W7 w6 }/ Y# \ - echo fwrite($handle,file_get_contents($pic));: D3 j/ @* m( x! W( r
- }& j& D" L9 Y' F# k( ^1 I# c; r0 u
- 9 H( F2 H* S2 d8 L
- $title = $_POST["title"];
5 L5 V0 r) p) F d7 E. f$ P - $content = $_POST["content"];
6 W+ a9 H% B; d5 k6 u - $tags = explode("_",$_POST["tags"]);
6 B" O" \* i) S1 j" z4 a2 v! H# |/ s - $cate = $_POST["cate"];- \+ e5 E _+ C2 R/ I
- //print_r $tags;
( e# k, B. a" \% J& P' @ - //% F) E* |; x) l) p
- //
0 ]; |. A4 C! X: q - $wp = new WP(); p: `9 x3 {- V* C% k! [! L
- $wp->main();
* g9 g6 ~. Z! [! D5 v+ p - //
( H6 _# p4 Y! Y# i - $my_post = array();0 \3 h+ ^, ^* e* ?8 N3 j1 C. y
- $my_post['post_title'] = $title;+ Y( r4 l5 f1 o9 b
- $my_post['post_content'] = $content;
$ C& U" _. S! B6 b& o( ?1 s, ~ - $my_post['post_status'] = 'publish';& ^, \; {( z% k( ?! }* V+ M7 Q% h
- $my_post['post_author'] = 1;
! g$ w) ?& L+ M4 Q - $my_post['post_category'] = array($cate);
, c) l5 v6 X: J) e" e" a7 T6 M7 N - $my_post['tags_input'] = $tags;) a- j) }7 {* H; |7 k7 ]
- //$my_post['tags_input'] = array('tag1', 'tag2');
$ ]( B( N- d8 u2 b8 H: h# w$ ] - //$my_post['post_status'] = 'future';
1 a. ~8 u; }$ Q - ////$my_post['post_date'] = '2010-07-04 16:20:03';
9 P' X# e, O x# ~6 H/ P- I5 G - $ |# H4 T* K" Z q" Q4 U
- // Insert the post into the database
7 i4 y+ g6 q- O# S& q* T) r - $ret = wp_insert_post( $my_post );
; S! [7 x+ v/ t8 `! L V - echo $ret;
/ G3 f# l* C/ `/ w% Y - ?>
" t* @: ^$ H& }% }/ s( ?
复制代码 我自己写的
5 @3 r3 k- y& F插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|