|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑 5 r# s+ s) O3 }. ]+ X
* x+ l9 k* T Z" O8 [: R直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持
4 |' G1 r8 x- _. y
9 f- R x: p, ^- V我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了
# J g! B$ ?4 T3 f# _; W, P5 d
) y/ k6 o; @, c' D5 v7 x# v方便实用 简单 何乐而不用呢
, ]/ U- q; g' F' R- <?php4 C; {+ q# i8 ~0 _
9 {: }: C Z4 v4 Y- define( 'ABSPATH', dirname(__FILE__) . '/' );! o w! X, f9 ]/ V
- require_once( ABSPATH . 'wp-config.php' );6 [' S9 S4 {3 `% k' K. j
- require_once( ABSPATH . 'wp-settings.php' );
; h+ ^$ E6 E8 z9 |; H - require_once( ABSPATH . 'wp-includes/class-wp.php' );7 W: X% y9 a" o+ k$ {& c |) R, K
- require_once( ABSPATH . 'wp-includes/functions.php' );
" U5 _ M! g) b, {. \ - require_once( ABSPATH . 'wp-includes/plugin.php' );1 M+ A# D4 S4 Y2 j
- $title = $_POST["title"];" c& ]3 h3 }' T) L7 [' ~8 n
- $content = $_POST["content"];9 {2 h5 O A: c4 A& L5 D
- $tags = explode("_",$_POST["tags"]);: O9 t% B/ n( y( D
- $cate = $_POST["cate"];/ |( K2 w) K6 `$ v+ ~
- //print_r $tags;
- x6 L8 u# `; C- r - //
5 r3 l7 S! [. {" j& y: `1 u @ - //
# D z$ d0 k9 {2 l4 H4 j2 [: q - $wp = new WP();" g6 y8 q3 ~# R ]( J. j d/ K
- $wp->main();1 P8 i1 C ]) J! R" C
- //
$ ]8 t$ U9 L9 f3 N0 ]0 a - $my_post = array();
$ s. Y, Z% f! x - $my_post['post_title'] = $title;" @4 h6 @) N3 |1 I1 m& M ?
- $my_post['post_content'] = $content;
" \/ r4 ^. _7 x. w - $my_post['post_status'] = 'publish';% R2 K' w! @" C3 Z* K4 U
- $my_post['post_author'] = 1;
2 s; d; ?4 E; m: i. [ - $my_post['post_category'] = array($cate);
; v% n4 Q- R/ |) w - $my_post['tags_input'] = $tags;
4 S) Y9 T; O2 }: \0 M7 @ - //$my_post['tags_input'] = array('tag1', 'tag2');
% O% X& E! E" a - //$my_post['post_status'] = 'future';9 i2 m# z- }; q+ b
- ////$my_post['post_date'] = '2010-07-04 16:20:03';
7 `! y3 c2 T) s' S( y5 b, m4 K
! y% N% D6 d3 a3 W0 j1 j- // Insert the post into the database
, P9 T# m9 Q ~6 M! j( `' } - $ret = wp_insert_post( $my_post );4 S. M- [) J# x/ j9 s/ b; Q% b% I
- echo $ret;
% D% ~, f ?* D& r8 T& u# f! e0 a - ?>
, d$ D& ]2 C: o0 Y0 U
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气- - A, s- A( n: _$ U9 f5 F$ v/ @
- <?php) W; F" ]) c9 f9 U# ^1 \+ p
- define( 'ABSPATH', dirname(__FILE__) . '/' );) z- {: W- M: B/ c! R* L
- require_once( ABSPATH . 'wp-config.php' );& K2 r F R+ x w2 e7 N/ z
- require_once( ABSPATH . 'wp-settings.php' );
3 F& F( N& r. t6 f+ ] - require_once( ABSPATH . 'wp-includes/class-wp.php' );, `+ I' e( C+ ~" r! E/ I$ l# x
- require_once( ABSPATH . 'wp-includes/functions.php' );
' S0 D# q$ m" J+ |8 a) F9 ^$ M7 ` - require_once( ABSPATH . 'wp-includes/plugin.php' );
' Z7 H {! J' Y$ J+ k - $pic_name = $_POST["pic_name"];
6 g! s. B+ z" h& `" O$ v( _ - $pic = $_POST["pic"];' k; `: X3 m3 L: B4 _
- if($pic != "" && $pic_name != "")
~6 b# W" Z; f2 O! ]% L. G1 i - {
7 y0 r- o1 A4 D" A% a - $path = "images/";
8 r, J/ f1 ~9 K - $pic_name = $path.$pic_name;4 j8 x& o F" s- _) L0 Z2 ?
- echo $pic_name;: ^( h/ G( Y3 | l* s. P* ?# O
- echo "||";. G2 }5 {/ |# n" ^
- error_reporting(0);
& Y+ T1 u7 e/ ?$ @/ Z+ V/ V1 ] - mkdir("images");- R0 S5 i& P' l. o! L# v. N
- $handle = fopen($pic_name, 'w');
G7 N. v3 [, K/ H( n - echo fwrite($handle,file_get_contents($pic));; R/ O7 v7 b+ {& k
- }( _1 Z* x+ [" n0 w! A$ A) j4 g3 Y
2 E9 ~2 k6 a: C" S1 g; I: z- $title = $_POST["title"];
. n% v* G; W2 [ - $content = $_POST["content"];; o) A- N2 @! {
- $tags = explode("_",$_POST["tags"]);8 Q- K" a2 {. D* w$ j3 K
- $cate = $_POST["cate"];( i3 G+ H" I# G
- //print_r $tags;
. G1 j4 w/ s& j1 b - //4 H) r$ s P( O; n9 ]! v* V5 W
- //. W& ?: P/ n! C9 V
- $wp = new WP();
& t7 r- R* x/ }. j8 X$ U - $wp->main();
1 E0 H7 i4 R4 \- U9 w3 j+ x- C - //& Y2 X) y* L, { F* _
- $my_post = array();% l9 U+ _: w# S8 D, u
- $my_post['post_title'] = $title;
, I; m- J+ g2 b, g z - $my_post['post_content'] = $content;
" T& L2 V& C/ o* D - $my_post['post_status'] = 'publish';
+ }7 [' u* J9 W$ S( l( b& ^- ? - $my_post['post_author'] = 1;
; k S- q: b8 l p7 |% n# @$ H; D - $my_post['post_category'] = array($cate);
5 {( B0 _9 M" m; p% g - $my_post['tags_input'] = $tags;$ D% b( J& P3 k% X6 l( Q5 p8 Z
- //$my_post['tags_input'] = array('tag1', 'tag2');
; t" q- E, e, D4 ^. q e - //$my_post['post_status'] = 'future';- w* B* [1 [) N6 u8 j
- ////$my_post['post_date'] = '2010-07-04 16:20:03';. K2 N: H3 Y% \
- ~+ p- ~9 q3 D" J3 E- // Insert the post into the database
9 v& o! @& c; v - $ret = wp_insert_post( $my_post );
5 ?% ?2 n7 f+ N) u* d - echo $ret;
. O" b' b; W+ l - ?># Q& w$ a8 P o
复制代码 我自己写的, [: l+ s& Q2 T+ M9 G& A0 f8 |
插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|