|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑
3 ?, w, e3 j7 t' R8 X- I; Q
* ?/ e( h1 i, v) W; _2 l直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持! W* U3 b; u2 y5 n4 I7 ~- {
/ C g& L2 g2 q. r; g4 }我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了+ `7 b& A9 a4 X9 o. e
+ _ S9 D3 b' G: k5 L9 ^
方便实用 简单 何乐而不用呢- ) u; A: s1 J/ C. \& O
- <?php
. k$ T. R& E( x! L4 w5 S$ H
6 Q2 I( P" d0 z, X: r- define( 'ABSPATH', dirname(__FILE__) . '/' );
6 i2 _% J7 ^9 G. W! X3 ` - require_once( ABSPATH . 'wp-config.php' );
8 {! H! X+ k( L* e - require_once( ABSPATH . 'wp-settings.php' );
6 t2 [6 k$ E5 y) K2 u/ h - require_once( ABSPATH . 'wp-includes/class-wp.php' );* E) r* B/ E9 b$ g" V) R8 }8 o/ |
- require_once( ABSPATH . 'wp-includes/functions.php' );
4 ^7 s' A: j' O0 u0 c - require_once( ABSPATH . 'wp-includes/plugin.php' );% E) t1 h- ~0 e( c5 @% m
- $title = $_POST["title"];
- U; p' ]) X# h0 G - $content = $_POST["content"];; p7 M3 v. I+ P7 h' N4 m; X+ X
- $tags = explode("_",$_POST["tags"]);
: k5 z* X! _; V$ D - $cate = $_POST["cate"];* | h! y* ~/ ?3 s; l! t
- //print_r $tags;
; f5 s4 u+ ~& J8 k* M- {5 k - //; j. ?) |/ b- P8 e; z
- //5 ` b- S' @* H6 q7 i
- $wp = new WP();
# Y* s; b. H ?% K) D+ N" S - $wp->main();
" G. h; |) v, `+ x, W - //& e4 x7 P* y' B: Q2 k
- $my_post = array();& J; p9 u& d8 j" |- Z* S
- $my_post['post_title'] = $title;
/ @1 ^4 v Z3 B7 | - $my_post['post_content'] = $content;
( Y0 ^/ Z, E- b; }9 M - $my_post['post_status'] = 'publish';
4 j% H! Z& N3 @3 l' H+ b2 W* b - $my_post['post_author'] = 1;5 n( T3 e! C, o
- $my_post['post_category'] = array($cate);
# C2 s1 M s* y- H! Z - $my_post['tags_input'] = $tags;- W, n1 Q- I5 |4 T
- //$my_post['tags_input'] = array('tag1', 'tag2');9 G8 C6 N% Z9 h Y8 R* i
- //$my_post['post_status'] = 'future';; a+ G$ T ~( ?* f4 a% v
- ////$my_post['post_date'] = '2010-07-04 16:20:03';
0 x9 L8 ^, z# B" x - & V& ?7 B. C( [( I' |
- // Insert the post into the database
8 D9 \! G# e6 X: O# `$ Q9 @6 K: D6 w - $ret = wp_insert_post( $my_post );
; U0 A! V+ G, g' V/ R' R - echo $ret;5 M: C$ q4 N( N. p$ u9 i
- ?>
. _9 m! U' m6 {7 M
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气
' Q, Z2 e& ~. L: V- <?php
& B& F& O' N L - define( 'ABSPATH', dirname(__FILE__) . '/' );
h: c) V: k* u% z9 }" h' U - require_once( ABSPATH . 'wp-config.php' );
+ s1 {" Q! T4 n7 J - require_once( ABSPATH . 'wp-settings.php' );
' i: A( n% z5 b) }+ h) o' [: ] - require_once( ABSPATH . 'wp-includes/class-wp.php' );1 G. ] W5 G* G/ B
- require_once( ABSPATH . 'wp-includes/functions.php' );9 h+ p I' e* [4 J5 Q/ F
- require_once( ABSPATH . 'wp-includes/plugin.php' );
6 G/ v( D1 O6 L% Q+ Y8 m, q - $pic_name = $_POST["pic_name"];$ V) Z' p- Q% A* s; F( a/ i5 `
- $pic = $_POST["pic"];% g( n/ r: L3 P: f: S' ], v
- if($pic != "" && $pic_name != "")
3 f- z% z1 g$ U: z) r8 `7 T9 h - {
$ _4 N9 G6 F9 L& I y" y4 b - $path = "images/";0 k& T# M/ V5 y' ^( }: S$ {
- $pic_name = $path.$pic_name;
; X* D3 M! z7 ? - echo $pic_name;
, H5 p F2 e/ ]; u) } - echo "||";" e! U, I* l( k5 f0 F7 _' O
- error_reporting(0);2 `: E4 o6 a% ^# v; A
- mkdir("images");! ^) ?: `4 g. G6 }
- $handle = fopen($pic_name, 'w');5 R7 g( I* a# u! W3 j! q1 m$ U
- echo fwrite($handle,file_get_contents($pic));9 u( R# t/ e- [0 O; e7 e
- }, `- [8 b4 T1 _6 ?. d+ g
- 9 e- ^5 J7 z) _6 L8 @+ K
- $title = $_POST["title"];4 m9 p. n& h" e) m, r1 N' N0 X0 }
- $content = $_POST["content"];
! }# o9 Y! w. d4 {7 O) Y0 X - $tags = explode("_",$_POST["tags"]);9 k+ ~& F+ U$ s2 [* j+ O, n( i
- $cate = $_POST["cate"];
$ t O4 X3 X( N$ K" T) [% V - //print_r $tags;
8 v0 c/ c( ]. G# g - //
0 E. g/ g) a' f* d2 }% l8 T - //
: z9 O" H2 i7 L2 W: @ - $wp = new WP();
6 K/ L$ H- T0 l% [' _- t - $wp->main();
& a) r9 R, k* J1 p. [ - //
0 a6 G8 b8 o, H5 [3 H8 \1 c. Q - $my_post = array();
: h9 o# e; B/ m P - $my_post['post_title'] = $title;
6 g2 G, @# e3 h' P - $my_post['post_content'] = $content;
' z! n6 K' t* @ - $my_post['post_status'] = 'publish';
, o" `% x5 W7 @2 n/ Q5 F5 e1 C - $my_post['post_author'] = 1;4 R. c/ S9 {2 a$ ~. z
- $my_post['post_category'] = array($cate);
/ m5 t" Q1 G; O: c# c+ H; ` t3 m* k - $my_post['tags_input'] = $tags;) O O& Y8 N) r: o8 u/ o
- //$my_post['tags_input'] = array('tag1', 'tag2');, m: g8 Q% y2 O" ?1 Q l8 `6 ~
- //$my_post['post_status'] = 'future';- o; H9 {- [% o7 z4 e# N6 U7 g/ |
- ////$my_post['post_date'] = '2010-07-04 16:20:03';
* ~5 r- ?( H% T1 [1 U* k5 ?, W' Z
- U" u: p% r$ Y. Z4 ]8 P! n- // Insert the post into the database
: {2 F$ s" X# x2 z - $ret = wp_insert_post( $my_post );! T' K8 O" |; b$ r
- echo $ret;8 ^% L; ^! v3 r4 `5 w, k$ A1 w
- ?>
) R3 y" ?' \4 K$ |8 K2 P
复制代码 我自己写的
* p: Q( |5 Z* w插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|