|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑
$ o4 X7 B' \1 W) r3 S* M9 N+ D
2 R! A' z& S# G* H直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持
# d- e9 r7 a0 W: |- r9 S1 \+ u3 C3 C$ s" S ^$ W
我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了
9 i5 P0 A9 m0 p4 P9 l% m. I4 ]4 m# I9 Z
方便实用 简单 何乐而不用呢
8 e: F2 \% y& M0 a- <?php9 `6 e4 y* E o f1 D8 e1 B, B* }. l
- - s' T5 n; v# O0 n
- define( 'ABSPATH', dirname(__FILE__) . '/' );
. `6 p: Y4 Y$ Z. B8 F - require_once( ABSPATH . 'wp-config.php' );
! Y& `# \3 g9 G - require_once( ABSPATH . 'wp-settings.php' );7 U9 Z% W1 `5 g# Z
- require_once( ABSPATH . 'wp-includes/class-wp.php' );
3 U* X; J5 ^: r7 d$ t7 s4 f* E - require_once( ABSPATH . 'wp-includes/functions.php' );1 C8 y+ B, h2 x# m3 _- P
- require_once( ABSPATH . 'wp-includes/plugin.php' );
" Z' d; n" R* ?$ Y) j - $title = $_POST["title"];7 w( d/ }: y( B! ^- f( J5 }
- $content = $_POST["content"];* N# @7 p* G. M" G& B
- $tags = explode("_",$_POST["tags"]);' n$ u; K; Z/ e+ ?3 a* e- Q, Y
- $cate = $_POST["cate"];
8 w1 m0 d& \2 g6 @: X/ o - //print_r $tags;
# r1 m* F" j4 u - //1 e$ x; x: g) I S
- // \ ?% I6 _. F ^ u% c8 W
- $wp = new WP();0 t' G* A# o8 B: B$ O! ^; h4 m' D
- $wp->main();- E9 R+ |4 \, {- I5 x
- //
$ P" L! _- a& `/ c4 `3 a2 m& t - $my_post = array();2 F& f; _$ _) v) w) s
- $my_post['post_title'] = $title;
9 Z8 a4 {) H1 _ l - $my_post['post_content'] = $content;
* L: I7 n& p8 G! G - $my_post['post_status'] = 'publish';7 U- `5 r8 A2 Q' t
- $my_post['post_author'] = 1;
" p6 a. F4 T+ @& b! W' t - $my_post['post_category'] = array($cate);
3 n! Z- z1 J, o; H - $my_post['tags_input'] = $tags;
+ {' I! H4 F1 _; I3 _! z P0 R - //$my_post['tags_input'] = array('tag1', 'tag2');$ o' o! J& t% E( i7 {+ g
- //$my_post['post_status'] = 'future';
) M0 D/ w0 c% o/ w; a7 v4 Y - ////$my_post['post_date'] = '2010-07-04 16:20:03';* e9 T, |* m8 u8 r5 `8 R7 R' T
- ; p$ B, `1 S4 a& Q
- // Insert the post into the database
& Q5 E0 J$ ?* c7 i! f - $ret = wp_insert_post( $my_post );
1 i- Z% v$ y1 k5 j4 q - echo $ret;
+ f" U! j% M6 U9 ]5 F/ ~ - ?>/ R/ g9 M: [% W0 U+ o, M
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气
% e. ?; ], A$ ~! r3 P' y9 k- <?php( {+ c4 R. Y+ \6 D2 U
- define( 'ABSPATH', dirname(__FILE__) . '/' );
1 \) m6 B4 ~0 c& ~" q# j - require_once( ABSPATH . 'wp-config.php' );
8 e' g- b7 p$ d' h0 x - require_once( ABSPATH . 'wp-settings.php' );
" s: d0 B- o' `* n - require_once( ABSPATH . 'wp-includes/class-wp.php' );: L! D! K e* M5 l* |( B
- require_once( ABSPATH . 'wp-includes/functions.php' );
' g1 w W f9 q- [/ e! I% o - require_once( ABSPATH . 'wp-includes/plugin.php' );
: {7 s: |$ f9 U - $pic_name = $_POST["pic_name"];7 o6 W2 M) ?5 r& M6 ^5 C
- $pic = $_POST["pic"];9 X' G: u. \3 \2 H4 J1 q
- if($pic != "" && $pic_name != "")% H& y7 t! ^, S$ [8 w( l3 ]& A
- {: h' U( T! E; A" q4 D3 |# z
- $path = "images/";. X# E8 |, K9 y! t% @* Q1 R2 K0 w
- $pic_name = $path.$pic_name;
4 L7 S8 Y9 y+ r0 f - echo $pic_name;
0 s+ L2 w1 a$ X3 ]! c - echo "||";. D8 m4 Z/ s4 s5 c8 S. C
- error_reporting(0);& \# \/ g3 K* p1 }1 y, R
- mkdir("images");& O) g9 j1 w }- C K
- $handle = fopen($pic_name, 'w'); \6 W, M/ R% q) [1 M
- echo fwrite($handle,file_get_contents($pic));
1 _/ ~8 n8 W8 L9 F - }
9 I" O; o$ Q" Z* I- P" `
- v$ b# `0 t5 p" r- $title = $_POST["title"];: l3 w2 k# L1 I, I1 P y1 }
- $content = $_POST["content"];1 J9 k9 p) X3 ~" q
- $tags = explode("_",$_POST["tags"]);1 k- p0 r9 Y4 m/ R
- $cate = $_POST["cate"];' R) @. i& M# N4 Z: ^7 y% _" r
- //print_r $tags;
) {7 x# ?+ R5 y* o5 q# y+ g' ] - //
+ x1 h# l8 X. m: X |, h - //8 n$ R1 Y$ I( M6 h4 l, ~ L
- $wp = new WP();
0 w- ~7 d. o. w( l$ b, H7 Y4 [ - $wp->main();; P `* Q6 W% c! ?, \8 X
- //
" `9 ~" z5 x5 ^ - $my_post = array();
; e, A+ p) V. i4 h8 j8 [ - $my_post['post_title'] = $title;/ h/ g* e9 x# _$ a
- $my_post['post_content'] = $content;
1 {, R: {! d# r3 f - $my_post['post_status'] = 'publish';
/ B; F* r6 P# P w+ F* w: c% o8 o - $my_post['post_author'] = 1;
# o. J+ H' p( o' H- o5 U - $my_post['post_category'] = array($cate);) U$ b. l, i- h- t. {
- $my_post['tags_input'] = $tags;/ M) T5 Z* F- i5 I6 q% G
- //$my_post['tags_input'] = array('tag1', 'tag2');
3 {' v+ T4 J( m5 u" f* `& ] - //$my_post['post_status'] = 'future'; j, n: ^) ?1 O
- ////$my_post['post_date'] = '2010-07-04 16:20:03';. J0 ~2 U2 M- K: b3 [4 l
# ^7 m! ^3 A H9 W- // Insert the post into the database
; Z% B3 U. m: ?% Q A - $ret = wp_insert_post( $my_post );
! x& c5 X F \: ~8 T/ s - echo $ret;
3 ~9 O3 `0 b+ O/ A" {; W, L - ?>
% @2 n, R7 \3 D! R
复制代码 我自己写的
! F/ d, n- g _0 \: d) {" ?插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|