|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑 % i ~ E$ h* o$ C5 g( X
: Z. ?2 t ]- p
直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持
6 i- y8 g- |+ h2 i, F' J3 i) E& R, m
2 \ h3 n: J4 P4 [: s: r我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了5 e, U/ s4 x) J" W
+ z. A. ]5 w; ]
方便实用 简单 何乐而不用呢
+ ]! ]4 @- D: R) N/ Y- G- <?php
# Y! ^3 I3 z8 n8 x3 {3 A
$ V1 Y! F! B, c1 P; R& U, K- define( 'ABSPATH', dirname(__FILE__) . '/' );
2 {# y8 e! d$ `& r( w - require_once( ABSPATH . 'wp-config.php' );$ P0 ?- k- l, Y, R; @: ~! Q
- require_once( ABSPATH . 'wp-settings.php' );
9 f- h- d' G. A: e1 I6 d: q$ I j2 W - require_once( ABSPATH . 'wp-includes/class-wp.php' );: g/ q' G; R* j
- require_once( ABSPATH . 'wp-includes/functions.php' );
) J r( ~& L$ ^$ _ S7 B1 E# ^+ @ - require_once( ABSPATH . 'wp-includes/plugin.php' );& c6 P8 [+ n' C4 u ?" f/ T; _
- $title = $_POST["title"];) [8 q, W+ L% ?# d
- $content = $_POST["content"];
( R0 r( r. R+ X - $tags = explode("_",$_POST["tags"]);- i7 j- @2 r3 t, \9 w7 D
- $cate = $_POST["cate"];' r7 c8 A9 r) z$ s7 t
- //print_r $tags;& d2 n0 E7 h: U2 T9 |* B" O7 v
- //& o( Q: x5 s( H: K i- G
- //
2 a: Y5 q$ n; ^( d c/ J - $wp = new WP();2 ` Z# e8 J1 B- j7 L
- $wp->main();
* U! M# q: ^) l0 d& n - //: D1 ^. n2 K) F7 w, }/ V0 k
- $my_post = array();
% r) H- \/ R( C3 p, K3 x - $my_post['post_title'] = $title;
$ p9 Y! D o) G - $my_post['post_content'] = $content;
; Q" g j8 p7 Y E( Q! U8 r! u - $my_post['post_status'] = 'publish';% L) c. u i" E
- $my_post['post_author'] = 1;
. t) m5 m' a2 o, H2 t - $my_post['post_category'] = array($cate);4 \7 f/ }% _; K; h3 l% ~
- $my_post['tags_input'] = $tags;
, L# g. j7 b v. b) `+ J" ~8 O5 [" B - //$my_post['tags_input'] = array('tag1', 'tag2');6 `4 n; w& z. ^; p) ^9 i3 {0 S
- //$my_post['post_status'] = 'future';
5 [* O4 g* y- P, X$ F$ R9 q w - ////$my_post['post_date'] = '2010-07-04 16:20:03';
; b4 v- s0 e! a2 Q% I - 1 j3 Z; P% S2 O1 b- r
- // Insert the post into the database0 y! p( K2 ^& n$ D" r
- $ret = wp_insert_post( $my_post );
8 N9 F* D5 q w6 w' ]! R: a - echo $ret;
) m b7 m0 ?9 {% n* h - ?> g2 }; R( w5 z: O7 n* K# K' u5 |
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气- 9 Y! e" b- G% I0 X
- <?php
' n( Y5 N" {9 N- S" R7 _( V9 U - define( 'ABSPATH', dirname(__FILE__) . '/' );
% W& Q+ l( l0 x9 _3 O- S - require_once( ABSPATH . 'wp-config.php' );$ I; j* `( t& X3 `0 _% `# m
- require_once( ABSPATH . 'wp-settings.php' ); M3 W& }# w/ r9 C1 @
- require_once( ABSPATH . 'wp-includes/class-wp.php' ); D+ O6 N4 b( k$ X
- require_once( ABSPATH . 'wp-includes/functions.php' );
! _& E8 `0 o& [ - require_once( ABSPATH . 'wp-includes/plugin.php' );- v& ^- g% } J
- $pic_name = $_POST["pic_name"];1 ~4 G( e5 y* h7 }1 ~' h( L
- $pic = $_POST["pic"];
, y7 R% b, p1 N - if($pic != "" && $pic_name != "")1 N* I* B* P) O( Q
- {
- ?* R* v. F' o - $path = "images/";# a7 B9 C- t5 {* q" e% M# H; @, M9 h
- $pic_name = $path.$pic_name;
& V O3 C1 f0 d% c# ?3 J& R' x2 x v - echo $pic_name;
! a; E* f8 A4 ~$ b: a4 o! N - echo "||";/ V' v9 i/ C9 S' t0 E4 k
- error_reporting(0);
; u8 I) P# P4 r" A4 ~ - mkdir("images");0 K& T7 `5 `# W) d
- $handle = fopen($pic_name, 'w');/ f* `- G5 A) J2 _7 T; N* G
- echo fwrite($handle,file_get_contents($pic));6 \' c' l+ y) W% B0 I7 ]7 n+ G
- }
! D0 A& ~, n; o5 @5 m' n) ]4 S$ D' G
. l: v, E) e7 q8 I$ ?- $title = $_POST["title"];
. B9 L. F1 p9 e: s# l - $content = $_POST["content"];
( T T7 W$ m6 b- K% a - $tags = explode("_",$_POST["tags"]);; R4 }# T6 }, }4 i9 D
- $cate = $_POST["cate"];0 L+ l; X, G* f+ I V+ h, V3 d
- //print_r $tags;
( G' f a" b* Y+ f5 ^2 F - //& M/ |# R0 C# |0 S) b |5 w' Y* c
- /// p$ P" M0 b4 V ~9 y
- $wp = new WP();
' H' H% S; s6 V, @/ W7 I6 r - $wp->main();
E" v- \" W; ? - //4 T2 u# Z) }% B& X( O9 \' |4 \
- $my_post = array();
) P$ {" r! l6 r: x - $my_post['post_title'] = $title;
9 N) S, A b7 \ - $my_post['post_content'] = $content;
0 x! Q/ }% y: `0 Q* V! u. e0 e - $my_post['post_status'] = 'publish';
8 x; C# H8 q& `0 g4 q. o: W# O- _ - $my_post['post_author'] = 1;
; w- e3 q, h6 b A' _ - $my_post['post_category'] = array($cate);
* H# p! O7 j$ d' }% Z - $my_post['tags_input'] = $tags;% H0 E1 h3 T6 t/ L
- //$my_post['tags_input'] = array('tag1', 'tag2');
5 L! k# m) P" c7 Z4 H' M - //$my_post['post_status'] = 'future';
$ R0 ~; F+ Y* r. C+ p - ////$my_post['post_date'] = '2010-07-04 16:20:03';
9 N; @) t2 s- W; k( T+ h7 F - 2 B. t3 T; y, ]2 |9 i% x7 c
- // Insert the post into the database t' W& V! v+ Z6 H/ d* J
- $ret = wp_insert_post( $my_post );
# D6 _2 W/ L5 r1 g1 T$ o - echo $ret;( _0 }9 P% B1 L2 E) Y" h/ A
- ?>1 Y! R1 j! z7 w, [$ h- _
复制代码 我自己写的7 z+ \; ?, A/ I$ A: a* p" K& J
插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|