|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑
# p5 {* k* l! ^ Y
- n' h/ a: s# ^! ^4 ~直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持* w7 Q! _" V" g% V7 J @, }
# d8 x. h( D8 T$ m2 T
我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了, c* ]/ _& B F$ S+ t+ P$ S9 |3 o- M
" }- {" Z& F7 D9 C5 V方便实用 简单 何乐而不用呢
; k* S- @3 D a: f& d- <?php
& v: f1 M; C* R5 i; p) J9 Q0 W% D. r
0 p& t3 }2 o5 u# H2 B! J- define( 'ABSPATH', dirname(__FILE__) . '/' );
% e F1 E4 h' k0 D0 Z0 } - require_once( ABSPATH . 'wp-config.php' );
2 @* ]1 J; l( b - require_once( ABSPATH . 'wp-settings.php' );
+ M) g' d+ P( p( X4 W; j - require_once( ABSPATH . 'wp-includes/class-wp.php' );' `7 m0 Y$ A) r% a' t* z
- require_once( ABSPATH . 'wp-includes/functions.php' );& c/ Y0 C& Q& Q) ^
- require_once( ABSPATH . 'wp-includes/plugin.php' );3 L+ H5 D8 Y! [+ }% D$ D# f
- $title = $_POST["title"];
( c9 t) g; N3 o8 f - $content = $_POST["content"];9 y5 _1 C! J5 t6 O/ y- p6 z% ?
- $tags = explode("_",$_POST["tags"]);
2 g# d) o2 ?( b9 f - $cate = $_POST["cate"];* p3 h" U- I D4 {
- //print_r $tags;% P8 ^7 ?% e1 P+ M
- //
1 J) X- E( r j |4 K, e - //( {/ _! S& ~* w4 U% b' C
- $wp = new WP();
* G/ U6 K4 o% a7 ? - $wp->main();
: b/ e) ]; a, K7 @ - //! ~" [8 E# q/ C
- $my_post = array();* K) b$ \/ g0 {2 s. }
- $my_post['post_title'] = $title;) ~. S; [: ^) Y/ @+ L2 j
- $my_post['post_content'] = $content;! ^5 _# M2 f/ M5 L
- $my_post['post_status'] = 'publish';" c1 k4 T4 e2 C J. L& E# d
- $my_post['post_author'] = 1;
$ p) i- g0 U" C6 \3 I7 n! |: F) V - $my_post['post_category'] = array($cate);
4 V7 X% l: C% s. F3 g/ G U7 N( H" ?. U - $my_post['tags_input'] = $tags;
" L' K' o* w+ }$ i: d- f) E, V1 A - //$my_post['tags_input'] = array('tag1', 'tag2');
4 L3 F/ B! w" t6 }5 r+ d( P* a - //$my_post['post_status'] = 'future';
3 ?" R& s1 X( ] - ////$my_post['post_date'] = '2010-07-04 16:20:03';8 q5 L) n- i/ G
- ' _* W% P8 Q, ~- d* g: P3 J
- // Insert the post into the database
w8 G/ D2 p5 {" x0 v Z! _+ V+ \" S - $ret = wp_insert_post( $my_post );) B/ ` W, w: V R7 I( ~( X$ Z
- echo $ret;8 O% e4 D/ s; T+ ^+ h5 z& [
- ?>
9 Z$ T9 J: j0 \ }! X9 o7 y
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气- ) v* ]1 F( g3 F& Q
- <?php8 G8 G7 {% n4 i6 w/ T
- define( 'ABSPATH', dirname(__FILE__) . '/' );
! X, f( ?2 Y9 E - require_once( ABSPATH . 'wp-config.php' );; o" D1 G+ {, k$ @& c/ k
- require_once( ABSPATH . 'wp-settings.php' );
) i1 o' ~3 f/ r" M - require_once( ABSPATH . 'wp-includes/class-wp.php' );9 n/ z% v3 R. A9 D1 v7 c
- require_once( ABSPATH . 'wp-includes/functions.php' );$ `6 b0 Q& c, U; Y) Z9 \$ `! e
- require_once( ABSPATH . 'wp-includes/plugin.php' );$ U, o& T' a& H4 w7 E
- $pic_name = $_POST["pic_name"];
6 I# |. W* ?$ U, R/ n - $pic = $_POST["pic"];9 D# c; z' D2 c' e+ k4 G
- if($pic != "" && $pic_name != "")
- w6 g2 M i+ H8 Q - {
; { O& ?1 H5 z# C9 k - $path = "images/";7 W3 c9 l3 t0 i3 `/ c8 |
- $pic_name = $path.$pic_name;
, q! r# F) k4 b8 G - echo $pic_name;+ I! }: r+ S" s3 y( `
- echo "||";& m3 h1 T) Y) W% t
- error_reporting(0);+ s% L. r1 |5 x& _' `
- mkdir("images");
$ ?( r) q! m" R W1 | - $handle = fopen($pic_name, 'w');2 R- z$ i6 o; \. f; K0 `
- echo fwrite($handle,file_get_contents($pic));& d- W+ Y) Y: ]
- }
( p, R% J! X3 H: W
* P: H. Z% a2 K3 F- $title = $_POST["title"];
; i o: S) L' \% L3 D. j - $content = $_POST["content"];
4 l% ]4 N k% Y: P8 ]) T* _ - $tags = explode("_",$_POST["tags"]);
3 S' E) x5 y3 B( ~2 t - $cate = $_POST["cate"];
& Z0 _0 X' r3 U1 X) i7 B - //print_r $tags;: v) U1 B) O& `" {5 \2 v
- //
6 _1 r; d/ r% Q' Y9 r$ ~$ R - //6 X' R, X1 h$ P2 C: `3 U3 E1 L
- $wp = new WP();1 X) H6 u" N% x: E: {+ Z) L
- $wp->main();
) B* u. T# P# o - //' ]+ o) Z+ O8 Q: O/ Q- C
- $my_post = array();5 K- o. o# z5 o* K; n
- $my_post['post_title'] = $title;
2 D3 S% [/ p) P - $my_post['post_content'] = $content;
! x- J& S, V" }- N; @ - $my_post['post_status'] = 'publish';+ n; M3 r$ T7 M. L6 o- d3 k' N
- $my_post['post_author'] = 1;7 ?+ f0 d! p. X6 E6 Y% V0 x0 l, F
- $my_post['post_category'] = array($cate);/ a. O5 }- i8 F0 S" \, o& }
- $my_post['tags_input'] = $tags;
; b7 I0 K+ M; }6 D& ] - //$my_post['tags_input'] = array('tag1', 'tag2');
. R6 g1 O6 E/ J% x* N - //$my_post['post_status'] = 'future';
8 I+ G; M o, b - ////$my_post['post_date'] = '2010-07-04 16:20:03';; o" }# P" y2 c9 R2 ^
8 n& [( ?7 u/ g' p9 X( ?- // Insert the post into the database5 x7 i% J7 q2 Z
- $ret = wp_insert_post( $my_post );
( i, l4 ]" C; ` - echo $ret;
# X- n5 ]* _0 I. V - ?>
- Q0 i# p; D3 {$ @& y4 y
复制代码 我自己写的
0 r+ b5 p1 {- x* v: U插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|