|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑 : J* S$ V1 ]7 ?2 v" K D( T. B% s
$ E8 {; ], [% t' h e9 E& Q
直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持5 C8 |$ Y8 J7 O* N8 o
" u* }2 b) I: e/ R4 m) W我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了
; U. x' M" r7 W/ x$ I
- M1 v8 X" O2 Z' f" k方便实用 简单 何乐而不用呢
0 N9 G' K: D: o1 U4 j4 N" h- <?php
3 b4 M$ y- k6 g- R: _0 O6 g - - H/ g2 ^( Q# X
- define( 'ABSPATH', dirname(__FILE__) . '/' );
$ M7 a5 u$ A& s P; l - require_once( ABSPATH . 'wp-config.php' );
+ T s! `9 D7 a6 {% h - require_once( ABSPATH . 'wp-settings.php' );
6 y! C3 S1 h- Y# R2 Z" g2 s - require_once( ABSPATH . 'wp-includes/class-wp.php' );
8 l% A, m% S1 l- u! U, j1 a - require_once( ABSPATH . 'wp-includes/functions.php' );
8 | \0 T5 D/ F+ t* g - require_once( ABSPATH . 'wp-includes/plugin.php' );- k; [# K9 g( G* u9 g! e+ H6 c
- $title = $_POST["title"];* p5 k# s5 g# }7 S
- $content = $_POST["content"];
7 Y( x2 T0 V) l% t3 V. Q% P6 Q - $tags = explode("_",$_POST["tags"]);
4 s7 x1 F( c4 X7 o& [! i A! @7 u - $cate = $_POST["cate"];" j m5 ^$ e( N
- //print_r $tags;
3 m% W2 C7 f( N- c' s( j1 x - //
; f0 I6 J3 S, x! ] - //
( X5 U4 f8 {" {% d. B6 ]. D - $wp = new WP();3 n, P% B/ c. [, e% p3 @; O
- $wp->main();4 M' f6 J( t6 Z
- //3 {- `& N! i- @: a
- $my_post = array();
* y/ m3 T7 G8 J b& F - $my_post['post_title'] = $title;
3 y' l, O) w# y5 R$ w' Y( M - $my_post['post_content'] = $content;+ K H. V2 U. ?+ B
- $my_post['post_status'] = 'publish';9 P- b q0 M" c2 ^' i q4 x. v
- $my_post['post_author'] = 1;; g( x7 Y! a: U1 s
- $my_post['post_category'] = array($cate);" j" B( `1 `7 Q: O
- $my_post['tags_input'] = $tags;
: |' }6 e: m$ v' y# n4 d: k9 o8 O - //$my_post['tags_input'] = array('tag1', 'tag2');$ f2 x j4 U4 q/ k: Z4 c
- //$my_post['post_status'] = 'future';
; D$ x7 M; E7 O( b- i - ////$my_post['post_date'] = '2010-07-04 16:20:03';
( q' A6 S2 y9 u - + ?( Q( G' F0 l2 U6 f
- // Insert the post into the database) m, L5 m# ]/ l2 p- |
- $ret = wp_insert_post( $my_post );: U& o' ?1 [: W- a
- echo $ret;- v9 @, w2 m; w# c6 U3 T
- ?>
) V+ e+ _* E# e
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气
5 C1 w, R/ P. P' w2 A& o. s- <?php# n1 p0 X/ P# s" p2 e1 T) v6 R
- define( 'ABSPATH', dirname(__FILE__) . '/' );7 p5 t1 O& T9 e0 j: m
- require_once( ABSPATH . 'wp-config.php' );% o7 H2 P/ E( B( W' A
- require_once( ABSPATH . 'wp-settings.php' );7 a) i) l. W. h5 o; {
- require_once( ABSPATH . 'wp-includes/class-wp.php' );# \! n# ?! e- Q* f; d Q0 t" j
- require_once( ABSPATH . 'wp-includes/functions.php' );
) p0 w9 a/ B' y - require_once( ABSPATH . 'wp-includes/plugin.php' );1 _, O6 Y+ l( d, u0 b" a
- $pic_name = $_POST["pic_name"];& T6 x6 j$ B- F6 L
- $pic = $_POST["pic"];( O; r) u" a& T( X" W
- if($pic != "" && $pic_name != "")8 P* Z4 P3 G. ~
- {* P7 U: k! V7 X. g6 p
- $path = "images/";
/ g) D8 u, f' d; v5 g* C - $pic_name = $path.$pic_name;, i5 i, [! j) y4 w) }
- echo $pic_name;9 R, Y1 v! h! N1 L& f& Q
- echo "||";
7 P# U! x; Z6 J u - error_reporting(0);
: x, h9 f. V+ c5 S& u - mkdir("images");
6 _" C. F) l( {* w& { - $handle = fopen($pic_name, 'w');
: v' }0 b s# S; A - echo fwrite($handle,file_get_contents($pic));
4 O6 H: T J/ c+ x& x9 x$ k1 _ - }; [; X1 h- r+ G8 r
- % n0 q; s, l) T5 i. x! F
- $title = $_POST["title"];: Q9 _7 `1 U" v7 ~
- $content = $_POST["content"];& Z L& ]8 w" b4 k- W o1 Z5 `
- $tags = explode("_",$_POST["tags"]);0 ?3 G% k. x# A0 k7 s
- $cate = $_POST["cate"];5 Q) x8 A" d& o# m% l# H
- //print_r $tags;
( Q: H) D/ ?9 n( n6 G - //
% c- S- u" \0 c - /// K# {/ d: b1 v8 [& b# D( F
- $wp = new WP();
5 |0 a0 G' E* P0 U2 F" O( j - $wp->main();
# R8 x3 x3 `9 t - //) C1 n" U O- i
- $my_post = array();3 a$ J. u8 Y0 M/ m) {7 D
- $my_post['post_title'] = $title;
* `1 C; k* X. m, a; u$ { - $my_post['post_content'] = $content;
: |$ r+ Q, u; k - $my_post['post_status'] = 'publish';
* \1 U* c3 y% p% l& j3 }2 u) W1 ~ - $my_post['post_author'] = 1;7 }. t! q3 L0 R
- $my_post['post_category'] = array($cate);- F8 K1 B1 q& ]/ X3 }
- $my_post['tags_input'] = $tags;
* b; t4 |! Y% T - //$my_post['tags_input'] = array('tag1', 'tag2');
. o- d+ n9 X* F( Z2 R% b - //$my_post['post_status'] = 'future';7 A1 q! S/ E" C8 r2 P
- ////$my_post['post_date'] = '2010-07-04 16:20:03';
$ {# _! r; `8 r. S% F3 s1 L/ a
. y8 N; D! ^ u9 d. ~- // Insert the post into the database X0 ?5 L0 P: C
- $ret = wp_insert_post( $my_post );* q }7 c3 K9 H7 ?
- echo $ret;
3 l' f* X0 C( v5 _. ~/ w z! I - ?>, b) G+ B1 z* s% J
复制代码 我自己写的
" l+ x" w9 G+ K2 V% x插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|