|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑
: A4 e0 i9 p* Y" a0 N5 o# m6 G& V+ l1 K, y% S; T7 L) y
直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持% i5 T2 h# `7 o. v1 s8 O
' L1 l" C% H8 y# F: a4 Z
我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了
; V& U) R4 g( |4 m/ f6 Q* i/ c* @% d5 V1 e
方便实用 简单 何乐而不用呢
" S% g+ U; G9 O; o6 A' d% t, Q- <?php B) F8 p* r3 X
8 f7 T1 y' M |3 r$ O; G) E- define( 'ABSPATH', dirname(__FILE__) . '/' );" S# {* @9 d8 h
- require_once( ABSPATH . 'wp-config.php' );
5 {" t: R( O/ n& A# m - require_once( ABSPATH . 'wp-settings.php' );
9 B1 K" m' Z- z! c! M6 p. K - require_once( ABSPATH . 'wp-includes/class-wp.php' );# ]- V8 p7 Z' S
- require_once( ABSPATH . 'wp-includes/functions.php' );# p K. X# T+ ?" c+ W1 P
- require_once( ABSPATH . 'wp-includes/plugin.php' );& E4 u' M/ h. Q
- $title = $_POST["title"];9 f, R M8 f5 n
- $content = $_POST["content"];1 `: J- z7 C$ V# Z; [* @; a" W
- $tags = explode("_",$_POST["tags"]);
6 h) \* B6 L. q3 b. _* ?( i8 ~+ _ - $cate = $_POST["cate"];
& M+ f* B; v' r5 y, v" c( j: b - //print_r $tags;
8 ^/ a' T8 y7 h, x5 |2 j - //
# K' `( L8 T) w/ R - //
I, t1 m6 G4 K5 @: s - $wp = new WP(); c5 W5 W# \0 h' j0 R, }. L2 ]0 W
- $wp->main();
3 x* z4 W, C; B" W8 f [0 A J - //
$ g; ]3 l9 s) l( I - $my_post = array();
5 w' ` v) X: @" j( \9 X1 o - $my_post['post_title'] = $title;2 W" H3 i2 a$ o: P8 a/ I* f7 r
- $my_post['post_content'] = $content;
/ [ g! i4 {/ c" T; Z$ ?- m- q - $my_post['post_status'] = 'publish';
% M+ x2 P5 L; G& N4 q% {: R - $my_post['post_author'] = 1;7 G& D, W$ H# t
- $my_post['post_category'] = array($cate);, y, ?3 h p' S9 ]1 ~& F& _
- $my_post['tags_input'] = $tags;
& _+ i) E9 j: a - //$my_post['tags_input'] = array('tag1', 'tag2');
6 K9 g: e" Y5 h+ r1 F6 g& g, j" \, ` - //$my_post['post_status'] = 'future';2 C+ l% Z1 e, K6 N/ n
- ////$my_post['post_date'] = '2010-07-04 16:20:03';* Z3 y7 Y7 j4 s$ r9 j7 P/ ?& J
$ H" r0 y# z1 x7 K8 j* }3 t( n- // Insert the post into the database& ^* j' m* g* f7 h2 p# ~2 I$ N
- $ret = wp_insert_post( $my_post );
! {! L6 u6 {) ]+ E - echo $ret;
8 Z/ j2 o, G1 P) r# X - ?>
) _7 K6 D+ ~: [3 l! Z6 r l4 Q+ x
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气- . n0 E/ O) z) B1 h8 _
- <?php
+ G' p9 \' l2 e - define( 'ABSPATH', dirname(__FILE__) . '/' );
" j2 {' J7 f( P, A$ ~ ~6 s - require_once( ABSPATH . 'wp-config.php' );0 h& Z: U6 ?, E
- require_once( ABSPATH . 'wp-settings.php' );
* L3 u* E% U* G6 ~& c4 E- o - require_once( ABSPATH . 'wp-includes/class-wp.php' );
9 z7 l9 i0 G" m, Z5 { - require_once( ABSPATH . 'wp-includes/functions.php' );+ ~6 A$ ~& V& u. I; |5 b! j+ z
- require_once( ABSPATH . 'wp-includes/plugin.php' );
" U; u+ C# `' c - $pic_name = $_POST["pic_name"];8 K# c* H" p2 ^+ b
- $pic = $_POST["pic"];8 ^6 a# x5 B/ ]) R2 y4 C$ w4 T6 f
- if($pic != "" && $pic_name != "")
% X8 |- Q/ \, T+ C" v# z - {
{% S: C5 r7 F - $path = "images/";
3 @. K! p, L3 k% f z" k: y - $pic_name = $path.$pic_name;
/ ~+ ], X9 n7 D0 S/ n) x V8 U - echo $pic_name;
+ e& m& H% W5 ?/ u - echo "||";
% ] p9 Q* n, F% N2 Y& } - error_reporting(0);
5 c( m: [% `; N# p8 b% c - mkdir("images");0 d! _4 R7 u$ [
- $handle = fopen($pic_name, 'w');
' o/ }, R- l# c1 M - echo fwrite($handle,file_get_contents($pic));- {1 n- V$ `# ^6 ^& y' [9 Q7 v
- }
1 k+ i( q; P, `1 D$ \. W5 T
5 n' N( t2 x9 u: \- k$ Q/ l# o- $title = $_POST["title"];
5 i* {7 S; }. m5 @! a - $content = $_POST["content"];1 D# c, `: O) x
- $tags = explode("_",$_POST["tags"]);1 I. F1 J; f6 G v
- $cate = $_POST["cate"];
( G! g8 _0 P+ t. D# C9 M - //print_r $tags;
) I1 m- C: {0 h2 o# K2 {; Q - //, H, h! n/ i% u3 Z! v. ~
- //
; L& g, q4 g" k$ ~ - $wp = new WP();
* A; b3 L x& |9 r7 X - $wp->main();
, q6 K. d7 `( G1 H; |: ~ - //
' e1 C3 Q) _" W+ n: D) B! m) ?! e - $my_post = array();& z5 \6 Z2 C5 r1 u/ O1 H* M l
- $my_post['post_title'] = $title;
* `+ ?$ b! z$ p/ I5 [6 L4 E - $my_post['post_content'] = $content;7 |6 G: I' _) ~" i" z
- $my_post['post_status'] = 'publish';
% T) v- A; B$ X6 Y% F1 s; q - $my_post['post_author'] = 1;
. ^+ u. w1 K0 z9 ? - $my_post['post_category'] = array($cate);$ u" T8 t- g$ s4 \" b
- $my_post['tags_input'] = $tags;
. @) B) N! B6 f" s+ L - //$my_post['tags_input'] = array('tag1', 'tag2');
, H. k+ u0 |/ {6 o W - //$my_post['post_status'] = 'future';
# l8 T8 m% F, P7 c1 t+ N - ////$my_post['post_date'] = '2010-07-04 16:20:03';
2 I6 t% {( e% N1 L% x A" n
8 K4 ]) g$ J/ H4 x- B* s- // Insert the post into the database& `% M; Y8 f. k+ C3 w- G9 f
- $ret = wp_insert_post( $my_post );
: A4 r% E3 A( Y1 \ - echo $ret;# T4 @/ O" V' Q# h/ ]1 B4 R: u
- ?>0 ]- v2 ]0 \4 x: l/ ~& Y
复制代码 我自己写的! H B! k) Y6 Y/ h
插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|