|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑 1 Y# |9 K. X) l0 j" |0 B0 P! @
1 ^; v1 c. Q& F, t0 m' w; n
直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持' |" X( E# g) c4 x3 G
. u& t) p# N3 ?, g5 Z我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了4 z+ R) P# N2 l+ y* }
$ ?: S9 i( U! F8 E$ o
方便实用 简单 何乐而不用呢- 5 C% ~. U6 e# p
- <?php- _ ~# O5 Z+ ?( ]7 j
- + J* I3 S; S8 s0 d1 z) x/ L
- define( 'ABSPATH', dirname(__FILE__) . '/' );
& n, o7 s. R4 K2 a8 U' l - require_once( ABSPATH . 'wp-config.php' );
1 x, M1 t) D! Z/ f. \0 y9 m - require_once( ABSPATH . 'wp-settings.php' );
) d; \, \9 H/ s# i. }5 l7 B - require_once( ABSPATH . 'wp-includes/class-wp.php' );. O; }/ q# Z0 L; I# r% \$ |
- require_once( ABSPATH . 'wp-includes/functions.php' );0 O; q# g1 w) E1 @) x
- require_once( ABSPATH . 'wp-includes/plugin.php' );( l' p: s5 Z, t" x
- $title = $_POST["title"];1 G/ s) X" t/ M6 ? h( |9 [
- $content = $_POST["content"];' K- @ ]" d6 `$ A" f
- $tags = explode("_",$_POST["tags"]);6 }, N; @/ L0 e4 _- m& N6 Y4 m
- $cate = $_POST["cate"];* t4 b- l8 u0 I4 O$ G P
- //print_r $tags;+ B, E6 W+ R! \& \5 S. A5 U8 P
- //1 a2 H# @* Y) s0 M0 G
- // R( r% j) l x! B8 K
- $wp = new WP();
( e4 G9 R& b" q: o - $wp->main();4 a) e/ ~; A, q* X
- //
/ G) }) {# t: X9 J7 ~2 R$ R. K) Q; ?7 u - $my_post = array();8 j# Q( J& c4 u, R
- $my_post['post_title'] = $title;
) k& {1 r$ R& L0 u ^ - $my_post['post_content'] = $content;, Y. k7 z' @- |4 K: h* r
- $my_post['post_status'] = 'publish'; m t) U; G" K" p) G
- $my_post['post_author'] = 1;
2 y! Z) B- X$ v- Y0 q; f - $my_post['post_category'] = array($cate);
4 x) y* ?3 P3 |: g8 e - $my_post['tags_input'] = $tags;5 Q' M9 d6 X5 ^3 F* S4 l5 Y- D$ Q
- //$my_post['tags_input'] = array('tag1', 'tag2');* B3 `5 y0 `6 h# s5 k
- //$my_post['post_status'] = 'future';9 R% c/ v) ]6 ^
- ////$my_post['post_date'] = '2010-07-04 16:20:03';
' E* C' S- D) d5 {" o
; j1 C' ~9 ?( B# P. t+ X- ^- // Insert the post into the database
) B$ q) z0 @' |9 X! N - $ret = wp_insert_post( $my_post );- D" e" P' b8 J, i2 \! h$ A
- echo $ret;
, j9 R& x# E3 `* g: R! B9 b9 M - ?>
' g4 M- h/ `/ ^ x
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气
- y! i1 U% e3 J! H- <?php4 @( K. T5 _; h1 a) c
- define( 'ABSPATH', dirname(__FILE__) . '/' );
6 ]& {6 Q( ~5 `5 y1 d( l/ P - require_once( ABSPATH . 'wp-config.php' );
. c5 I: j8 S5 B7 ?( ^' ?$ a" k - require_once( ABSPATH . 'wp-settings.php' );4 k) s$ w# ?! s% H+ P8 [0 h3 X3 a
- require_once( ABSPATH . 'wp-includes/class-wp.php' );2 g3 L4 p0 Y- g* Q9 z8 r
- require_once( ABSPATH . 'wp-includes/functions.php' ); A1 B5 w `8 W
- require_once( ABSPATH . 'wp-includes/plugin.php' );
# P* ^5 B* b2 b& q! ?5 ^3 k4 ] - $pic_name = $_POST["pic_name"];
& O+ Q3 w' ~8 r" D5 ?0 Q$ J - $pic = $_POST["pic"];' M: S! @4 X( [) R1 A. e
- if($pic != "" && $pic_name != "")( l o8 l3 w3 A" W4 v+ T1 |, q
- {
- C$ e: q+ p; p0 t8 K - $path = "images/";' A( Y6 o% o+ E/ X) N
- $pic_name = $path.$pic_name;
1 }6 E0 ?3 g$ V$ v - echo $pic_name; z( @2 y% z0 O0 c2 ^0 X; ~
- echo "||";, z! x3 R7 S8 ^7 ?5 p
- error_reporting(0);, M. h* k& P' F# |! B
- mkdir("images");9 M/ S' h" r9 ~' v8 ~9 \' \
- $handle = fopen($pic_name, 'w');% ]0 g' h9 h8 r4 H0 `& G
- echo fwrite($handle,file_get_contents($pic));
( ?) h7 S$ y3 f. b+ N% ^ - }
; b9 p* r6 h( b0 ~
& H7 E2 G; r3 }& c- $title = $_POST["title"];; ]- Y! t. K2 {6 z" }1 s% o
- $content = $_POST["content"];
) j4 {* x% ?8 G* P7 ~ - $tags = explode("_",$_POST["tags"]);
3 E( [ J7 n5 v. P1 } - $cate = $_POST["cate"];+ P* U: e3 c2 i9 ?+ o% V
- //print_r $tags;
1 [& V% d* N' _ - //( e$ G! G: ^. D% V
- //: {1 L9 A: `( q4 `! q) `% L+ L
- $wp = new WP();- L. A7 F- G- {" q8 g! M" r, M$ f8 d
- $wp->main();
) S' M* r! H) g% D6 g$ n9 ^ - //
5 @! D% Q7 H6 \) J - $my_post = array();
7 u, Z5 a3 V! Z+ N6 V - $my_post['post_title'] = $title; ]+ `1 \8 U& b3 O \1 S% R
- $my_post['post_content'] = $content;6 H. ^5 m$ n6 U0 N6 q3 X6 b8 d2 s
- $my_post['post_status'] = 'publish';4 n. {, [& r4 [0 o
- $my_post['post_author'] = 1;
+ a. b5 p8 ~' g - $my_post['post_category'] = array($cate);5 N& {) T# a9 C1 x t8 C
- $my_post['tags_input'] = $tags;. g: I! ?+ e' r
- //$my_post['tags_input'] = array('tag1', 'tag2');( ~% H' [& r2 i5 {" |' F
- //$my_post['post_status'] = 'future';
( c/ @7 m7 F* F3 S0 ~4 {% q, q - ////$my_post['post_date'] = '2010-07-04 16:20:03';, N0 `9 E! N9 r% E2 F& Y/ V8 R
- ' B, s# E( `& N T4 \8 d' q
- // Insert the post into the database
) }/ @: I0 K t - $ret = wp_insert_post( $my_post );
- B v& B7 f) l: d2 q - echo $ret;
) q u/ p* o, v% s ~% o" i& R$ o - ?>
# H2 B: ?& n* [% d/ A
复制代码 我自己写的
2 f% I" [3 l) |# ]* n: t插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|