|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑
* Z) W! N9 N$ C' y2 t/ [$ I- U
. b2 C) g- x7 {" p1 ]6 k直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持2 t) P) v( Q5 t
) F8 H2 _/ ]$ z* h
我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了
* B$ I/ i/ C" u& r! R* o+ Q: \2 S' O$ o2 u+ q; y7 Z# }9 S" h
方便实用 简单 何乐而不用呢- " M6 o5 u" N/ j; M! F4 |$ D; j
- <?php
. `% ?( f' ^4 K2 J
4 C( a4 D: k$ ?7 z. \! @. }) y- define( 'ABSPATH', dirname(__FILE__) . '/' );6 K6 Q* a) o' V5 T
- require_once( ABSPATH . 'wp-config.php' );
$ l/ b( S1 T. n& [+ n+ \! u - require_once( ABSPATH . 'wp-settings.php' );
. U# z* c6 T3 s - require_once( ABSPATH . 'wp-includes/class-wp.php' );0 ]: z1 b- D. a: s
- require_once( ABSPATH . 'wp-includes/functions.php' );6 t4 \0 S+ R3 d& x$ Y
- require_once( ABSPATH . 'wp-includes/plugin.php' );' v7 O" _8 |9 n" d
- $title = $_POST["title"];) M5 o0 h" l, i+ B+ Z( Y
- $content = $_POST["content"];2 i5 f$ i7 t3 k6 j* u( Z
- $tags = explode("_",$_POST["tags"]);
! Y h0 |, J2 N* Z8 Y( @3 f; U - $cate = $_POST["cate"];( N9 Z% N9 o5 k" K5 ~+ ~/ m
- //print_r $tags;2 A7 Z9 @3 e; i8 N8 f
- //4 W/ h. C3 l! f) t* b
- //
; {1 E7 c1 t2 w1 p$ u% M - $wp = new WP();
4 _8 |; J& o5 j% l& i( w# y3 k - $wp->main();
# Y' I+ k/ Y& m3 q; W8 k - //
/ w: i/ v- U4 }6 u8 k1 \- Y - $my_post = array();1 p- B( l+ `& ?& b- w! \# \
- $my_post['post_title'] = $title;4 M- @" x+ M9 |- j* F( @, |6 X
- $my_post['post_content'] = $content;
. t5 L& n2 p5 @( U% n - $my_post['post_status'] = 'publish';, R. |0 x+ u* g7 w$ O7 ?
- $my_post['post_author'] = 1;
+ X- i& x5 j/ E0 z* }: @ - $my_post['post_category'] = array($cate);
* _0 W( |& [3 p$ n - $my_post['tags_input'] = $tags;
& w, h' u& `4 w - //$my_post['tags_input'] = array('tag1', 'tag2');
# u& d4 N4 E. [3 l8 W - //$my_post['post_status'] = 'future';
, |7 v5 U8 j; \, l, \ - ////$my_post['post_date'] = '2010-07-04 16:20:03';
' L! r0 u) `" n1 g6 a6 J - 7 ]0 b1 y* M: M% b# Y9 y) [
- // Insert the post into the database
) [4 E) Z" X' N+ o - $ret = wp_insert_post( $my_post );
6 k. ]7 V& u' F5 q - echo $ret;
6 k$ D- a1 \, m, B9 _6 w2 x - ?>
+ T# F1 q' X7 \0 g
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气- 1 k. k7 H4 t3 C5 e- _2 a5 A3 S" w3 F% N
- <?php: s( `/ R s9 Z2 j$ w
- define( 'ABSPATH', dirname(__FILE__) . '/' );
' t" R& L |2 y; D* t, J - require_once( ABSPATH . 'wp-config.php' );( z: |# |$ D7 \: v( {+ K/ i
- require_once( ABSPATH . 'wp-settings.php' );
( K) m5 S0 g8 c* E" C - require_once( ABSPATH . 'wp-includes/class-wp.php' );
- x) Z& h) k9 s* y" U5 { - require_once( ABSPATH . 'wp-includes/functions.php' );3 ^) ]& S. x& z: q: V0 M# f, {
- require_once( ABSPATH . 'wp-includes/plugin.php' );
8 I, O* v6 _3 t - $pic_name = $_POST["pic_name"];
1 x( D/ Q2 T B- P+ U - $pic = $_POST["pic"];
$ q/ W$ \" C) c* o% Z - if($pic != "" && $pic_name != "")
1 |1 G/ V+ w$ t! } - {
g. G- A7 Y0 p% ~% p% F3 y0 { - $path = "images/";8 {: M2 c* C3 z% @" Z; q& W+ j; ?
- $pic_name = $path.$pic_name;" K4 v' Z" a& b) X! s3 U% I
- echo $pic_name;/ A4 z- a$ F! O0 ~0 S: ~
- echo "||";% p+ i0 M5 c4 e% ?# n
- error_reporting(0);
2 |8 b( a6 U/ D7 T: M* w - mkdir("images");
* C( N( X7 K( |7 R, j9 M& k0 S - $handle = fopen($pic_name, 'w');$ Z2 v3 Z; }+ c8 C9 V
- echo fwrite($handle,file_get_contents($pic));
# }# r, l0 a( X* z; l" u - }
* ~! h8 S# m* c" Q: b& d m
W* n9 d$ t B" _5 T- $title = $_POST["title"];
- r* I: ?4 e6 o) n5 P, h - $content = $_POST["content"];- a' ~9 `- v# A& M7 T' m
- $tags = explode("_",$_POST["tags"]);0 u6 i3 v* \% m7 C% z6 r
- $cate = $_POST["cate"];
/ ^2 W6 ~9 Z& {# h) _1 j - //print_r $tags;6 J/ P1 m$ I) E+ ? g8 J
- //8 U' Z5 n4 M" m0 [3 c
- //' z# E3 f! x0 g$ F, g
- $wp = new WP();
. L N. @3 `* Y' ]) e - $wp->main();
, s( {, O* h( I9 s6 _3 ? - //
5 E0 c$ n* P6 O K/ d - $my_post = array();0 _+ K4 E1 A0 i* R$ b7 u! k8 u" Z
- $my_post['post_title'] = $title;+ e/ ?' H' O- C+ h; H( c
- $my_post['post_content'] = $content;
f5 |4 W3 r6 M* ` - $my_post['post_status'] = 'publish';
7 K3 n0 U9 p# q2 ~, A7 D5 ~/ e6 [ - $my_post['post_author'] = 1;
* c: D- t# e% {2 L$ q5 N$ {8 H - $my_post['post_category'] = array($cate);
/ h5 A) g- t% T9 f; x5 W5 r - $my_post['tags_input'] = $tags;
8 a, ?9 ]/ \& ^6 z$ U6 n - //$my_post['tags_input'] = array('tag1', 'tag2');
9 c m: f1 } a: Y - //$my_post['post_status'] = 'future';
; d( n6 R. {4 ~8 W' L x o0 w' n - ////$my_post['post_date'] = '2010-07-04 16:20:03';) P1 q% j% l0 g/ ^+ b. _5 E
4 \3 U$ m+ Z% P9 f- // Insert the post into the database6 ?8 T3 F! M/ a0 o( Y) H
- $ret = wp_insert_post( $my_post );
* S0 o. Y; q% p$ F - echo $ret;) i: O- a8 t9 ?1 a9 u0 s) n0 K: |
- ?>
( Y1 N3 }! K U$ W" r+ r% b
复制代码 我自己写的' w/ l$ o8 X8 L
插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|