|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑 7 x4 Z$ Z0 D: N4 A
+ I5 H& S( T3 }& @$ H1 t7 ~; q
直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持
8 e; C/ [9 q( c, F* t, ?
( O1 L0 L& R+ T; t- n1 z我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了
: E% M2 u h0 V# ?4 n8 Z. H; D/ p' L! p& y' y* \5 H
方便实用 简单 何乐而不用呢
! G2 e `! ~* S# R, w$ q- <?php- v2 R" s; c2 n% x
- . e) R& h8 n+ S3 X0 o
- define( 'ABSPATH', dirname(__FILE__) . '/' );5 {3 ]5 X" b% o; H. {: v6 k! T7 W
- require_once( ABSPATH . 'wp-config.php' );6 Z+ C2 p) ~# _# R
- require_once( ABSPATH . 'wp-settings.php' );3 |) r" v8 `) |4 K T# e: H
- require_once( ABSPATH . 'wp-includes/class-wp.php' );
* f4 i/ G+ G9 H$ M! k - require_once( ABSPATH . 'wp-includes/functions.php' );
& D* b6 g& D. n - require_once( ABSPATH . 'wp-includes/plugin.php' );
) H8 v7 J& P0 A1 E7 U6 Q - $title = $_POST["title"];
' ^8 Z* v. H9 r - $content = $_POST["content"];
' V4 \2 n; f+ }# K - $tags = explode("_",$_POST["tags"]);! n( F' e8 s# o; ~
- $cate = $_POST["cate"];
0 r4 t0 P3 w0 K8 |+ c" t5 Q7 v; a - //print_r $tags;
5 e3 g! r5 \, i- Q! B+ P; n - //
9 P4 G2 z1 e4 Q- S+ j9 B# u+ X - //; t6 ?" u' f6 P
- $wp = new WP();# b/ P0 N' [1 {5 C4 R
- $wp->main();
8 @6 ] F& z! h; r" ~ - //
* a9 ~% }% l( @& w; f7 Y& C# d5 C - $my_post = array();9 |) s& y& b+ f
- $my_post['post_title'] = $title;. V* V* q- s* p2 B
- $my_post['post_content'] = $content;" H! W! F W- }9 q b. B
- $my_post['post_status'] = 'publish';* m1 k' {4 B$ c( i
- $my_post['post_author'] = 1;5 d, O" O/ ~$ c! C5 [1 x( Z2 k& H
- $my_post['post_category'] = array($cate);/ y( r' `* a7 D/ r' O& h4 }
- $my_post['tags_input'] = $tags;$ `8 j( f$ D7 r6 ^
- //$my_post['tags_input'] = array('tag1', 'tag2');
' I- I2 F, U5 W" k, F" k$ p - //$my_post['post_status'] = 'future';
0 v4 c$ S) G3 M0 p7 a - ////$my_post['post_date'] = '2010-07-04 16:20:03';
( M$ T! o3 D8 b1 C6 |" b
! O) `8 z; M+ g0 e' y+ y# ~- // Insert the post into the database, F; D$ i( ]* T3 S
- $ret = wp_insert_post( $my_post );
6 l6 ?# [' G1 p+ |2 w - echo $ret;
9 a3 a. p6 v% L, E - ?>
: O! ^* j0 e1 p
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气- . _ F! n! Y# L+ l" O/ V- _
- <?php
2 V+ E" \! M! ?7 f3 B - define( 'ABSPATH', dirname(__FILE__) . '/' );; [: m# M7 S# b0 K
- require_once( ABSPATH . 'wp-config.php' );
2 ^/ K, |8 f0 g+ J n$ x9 a3 W' W - require_once( ABSPATH . 'wp-settings.php' );
, z# ?; S. Z0 }% \ h - require_once( ABSPATH . 'wp-includes/class-wp.php' );
/ _' q+ S( d | - require_once( ABSPATH . 'wp-includes/functions.php' );" G3 j) C9 {, L0 T8 c" {
- require_once( ABSPATH . 'wp-includes/plugin.php' );' V; C: e8 ~$ ?5 ?8 c! P; `
- $pic_name = $_POST["pic_name"];* h- {4 e6 n$ c9 K; V( V" E
- $pic = $_POST["pic"];
% |, y, b1 z0 b2 O - if($pic != "" && $pic_name != "")
$ X* C/ V4 _$ S6 b - {
5 y0 r( Z0 E; r: f. b - $path = "images/";8 Z& b. T# O3 d. J. j: w4 A7 g
- $pic_name = $path.$pic_name;1 M" I. m) f* q0 d
- echo $pic_name;0 x+ t% Q- I! o7 u
- echo "||";$ |9 E% w( {, g! D7 ^7 y2 O) B& Q
- error_reporting(0);
" }" J, S# ?& g) {: _3 | - mkdir("images");
$ Y" v! T6 x8 Z6 ?7 l0 V- A8 L; D - $handle = fopen($pic_name, 'w');
+ ^8 W+ \, B: y - echo fwrite($handle,file_get_contents($pic));4 b0 ?' j, a- B/ w, S
- }
# p( X( n9 z4 }. r6 ]- t+ B: F! X
) u% E! U) _. ^9 F) I6 o- $title = $_POST["title"];
3 u6 x0 B( u1 D, ?' j5 W - $content = $_POST["content"];
2 B; J9 m# O4 F, X: e; C# e6 w - $tags = explode("_",$_POST["tags"]);/ y$ g" a: I9 ]5 Y+ F: p- ]
- $cate = $_POST["cate"];
" Q9 B5 V2 V' m# o' [ - //print_r $tags;' I0 b2 i" n# ?8 T; r- D
- //
& x7 T" ^! y, Y% Q; W, E2 ]& Z - //, `+ [6 a% @) t% w7 M# r
- $wp = new WP();
, C" ?* I4 V- p# ~* H' J* V - $wp->main();
x. r1 j G. v1 \; w7 y9 T) a$ Z - //( X1 T4 G0 W8 e4 n& y
- $my_post = array();
& _9 |) {9 A. y4 W/ u9 Q1 Z - $my_post['post_title'] = $title;/ w5 v9 Z X8 u5 p
- $my_post['post_content'] = $content;
5 e$ B( t3 x! J/ z$ T2 ?1 [ - $my_post['post_status'] = 'publish';, R" s( F% t% V$ a! |( ~
- $my_post['post_author'] = 1;
3 ?; A0 R1 n c) p% L - $my_post['post_category'] = array($cate);
% Z7 I1 N W: u: h/ P+ G8 G7 B" O$ X* M# T - $my_post['tags_input'] = $tags;
8 l1 S, W# ]) f/ M! V" s$ n - //$my_post['tags_input'] = array('tag1', 'tag2');. ?$ M q& ]1 S% ~% q+ _: C5 A
- //$my_post['post_status'] = 'future';
2 H- p2 ?7 j( q5 k - ////$my_post['post_date'] = '2010-07-04 16:20:03';: w2 L' q/ [+ \0 T! ~
+ e* U1 q! ^, p& C9 b- // Insert the post into the database: q! M3 Y8 s' F$ ~0 l: S( f% ^8 i: P
- $ret = wp_insert_post( $my_post );
+ p( P4 V! y$ S6 c( ?& K - echo $ret;
, I9 }2 @" z: n$ ~ - ?>
# A# m( v2 Q, s7 b% W" H' ~7 x
复制代码 我自己写的! ?( z. H9 }+ b
插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|