|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑 ) D1 W y8 t! M7 }5 S
8 A6 e6 v2 W/ a D q3 L直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持
! D7 q6 ~! M5 h" X8 L4 _& G3 p X( L) |/ V4 Y, ]
我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了
; S# k7 J* B; W2 R$ X4 T3 F2 s2 c& Y. P
方便实用 简单 何乐而不用呢- $ g) i; X, q/ s( H1 U: b9 D1 a; @
- <?php0 Q) o! x/ M, Z% \
, N+ G3 H6 }5 _: l- define( 'ABSPATH', dirname(__FILE__) . '/' );
( S1 ^, {9 y, j4 o8 w/ j0 p+ x5 ` - require_once( ABSPATH . 'wp-config.php' );- }6 D6 x* D( C4 G" Z
- require_once( ABSPATH . 'wp-settings.php' );; Z9 F: q) i6 P% i: C; I2 \7 ]. Z
- require_once( ABSPATH . 'wp-includes/class-wp.php' );
! g4 o* O" V0 @( b' f. D# d7 x - require_once( ABSPATH . 'wp-includes/functions.php' );. S; q; r* ]' ~) {9 v6 q
- require_once( ABSPATH . 'wp-includes/plugin.php' );) f' a% i6 L; A' J c1 `
- $title = $_POST["title"];- V4 ?7 S4 D9 p! l+ z2 V8 n# h8 k
- $content = $_POST["content"];+ s0 d$ T# @# u. N
- $tags = explode("_",$_POST["tags"]);. y8 S1 J# j8 X' P! I+ r
- $cate = $_POST["cate"];% _! M( Q* o; L
- //print_r $tags;
7 A" E d, U3 u) b' w2 a. i7 {. D - //# o$ U% e. n4 m1 y- w1 _
- //. T8 s& \/ N, z' \: u6 O
- $wp = new WP();
; w2 K$ q: B; k) Y9 z - $wp->main();' r# n# ^8 [0 t. E! c- s
- //
( c+ f: x5 D$ [, y. P - $my_post = array();7 _3 [/ ]$ }* @& f" D
- $my_post['post_title'] = $title;
/ E7 r: R2 w e& v9 H9 F4 s - $my_post['post_content'] = $content;
8 ]/ \; F2 P+ x { C) j; O3 @& B - $my_post['post_status'] = 'publish';
9 `: a6 r3 G4 W, F - $my_post['post_author'] = 1;2 E0 u; H$ T/ I$ a, U
- $my_post['post_category'] = array($cate);
5 X/ A- |' C1 F- u; F - $my_post['tags_input'] = $tags;
7 ~" x& E5 f; c3 j6 t0 \! Z' S - //$my_post['tags_input'] = array('tag1', 'tag2');
9 f8 R4 k; D9 ]+ s9 i7 D9 p - //$my_post['post_status'] = 'future';
/ e% e3 y2 F' `9 L% J' ?1 o/ L+ Q - ////$my_post['post_date'] = '2010-07-04 16:20:03';% }4 r; X% t) ]! ^9 @- ]
- ( ]/ P1 W3 @) r2 a& Y9 m" i
- // Insert the post into the database h% m& y2 h8 [3 B
- $ret = wp_insert_post( $my_post );
2 o8 Y+ _9 P/ C" Q" `# Y5 f; y9 Y - echo $ret;
1 M# p/ Z' b0 ? - ?>6 c+ Q0 c q+ ?8 ?8 v4 Q+ r
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气- ' K2 s5 [" x! ~4 m9 F
- <?php) N6 Q: b& H. C
- define( 'ABSPATH', dirname(__FILE__) . '/' );
& i7 q0 Y- d; P( L - require_once( ABSPATH . 'wp-config.php' );8 I; O& J: U5 g% ^" N+ q
- require_once( ABSPATH . 'wp-settings.php' );
" _8 t: [4 e9 N' r* z) x; Y% K - require_once( ABSPATH . 'wp-includes/class-wp.php' );0 U; i9 s# y, k: v* r4 H1 n
- require_once( ABSPATH . 'wp-includes/functions.php' );
! \8 w$ N/ T1 B; P; o# g. h - require_once( ABSPATH . 'wp-includes/plugin.php' );
! T; |8 A/ q1 b2 ~! `0 B4 B - $pic_name = $_POST["pic_name"];
9 N/ m8 Y8 M& o2 D9 o: \( i, e - $pic = $_POST["pic"];
' Q1 r0 M% b* n9 |" L% S { - if($pic != "" && $pic_name != "")8 X, R' |5 ^+ r$ A
- { [! N1 U# y8 T c) s3 _2 e' D+ z
- $path = "images/";$ C8 K; H: q6 L5 O
- $pic_name = $path.$pic_name;7 b1 Q* X; w, O4 L& U
- echo $pic_name;
7 ~1 K i! I% I$ [$ s/ C - echo "||";8 j* R1 g/ Z/ b6 g3 }
- error_reporting(0);
9 R* p) ^* v, H* F# B8 A - mkdir("images");3 q3 l) i! c2 R7 ]: Y! q: E
- $handle = fopen($pic_name, 'w');0 M% X, r, n4 j7 @0 ]- i
- echo fwrite($handle,file_get_contents($pic));. o% S7 r# R6 S3 }; h V% ~
- } f' E. B: ?$ _# T
7 F2 y2 P9 S9 u+ I9 z" {- $title = $_POST["title"];4 `+ s# a; l3 {* E0 ?1 Z) `
- $content = $_POST["content"];; v- e2 E% `/ G7 v7 S$ @. T# E
- $tags = explode("_",$_POST["tags"]);# K. X7 g& i1 ^3 q8 R
- $cate = $_POST["cate"];8 O$ n( E4 r3 ^- p
- //print_r $tags;% a& e9 {& O; G) \) A
- //
l& \" |2 ?: @: I' z* C - //
3 x! n3 a- V, @6 d - $wp = new WP();9 V' x: a! w, d$ X
- $wp->main();
/ Z# t) C$ Q7 \' E/ D5 a5 q) s( r - //7 ~' V1 Y! ^2 V3 V- S
- $my_post = array();
2 z/ l% L) I. _/ m6 i s - $my_post['post_title'] = $title;
; }: U7 t+ Y) c7 N - $my_post['post_content'] = $content; h3 N. Z/ @! w( C Y! R! \
- $my_post['post_status'] = 'publish';
2 o! H( y# D& L- b7 H - $my_post['post_author'] = 1;
' \0 v$ {9 C _ ? - $my_post['post_category'] = array($cate);
' P5 C6 P0 r3 S - $my_post['tags_input'] = $tags;- `) ~/ d& x' s% z, l; I& q
- //$my_post['tags_input'] = array('tag1', 'tag2');
! B7 |4 ~. u4 ~5 [- T" [; Q( X - //$my_post['post_status'] = 'future';5 p8 X; o# F. i4 ?4 m
- ////$my_post['post_date'] = '2010-07-04 16:20:03'; D1 Z. y) h6 t
: n1 R, Q& @3 `' W- // Insert the post into the database
" G0 Y& \2 ~5 m - $ret = wp_insert_post( $my_post );
0 n5 c; G2 L! k) z - echo $ret;
6 k; ^# ?1 u& U - ?>
" l, A- z' g' N4 M4 h
复制代码 我自己写的/ v; y7 t/ ~- o% m# f' u
插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|