|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑 ; ?2 t- ?3 b( ^4 A
$ f7 ` j6 E1 j+ w5 ^
直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持
0 z9 ]/ ^" R* G u2 Q
$ V7 O Z3 J. k2 w- X- N4 F0 f我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了
* T5 ]% ^# D7 m# G! q( j$ _% y4 c$ s8 J8 A1 z/ f7 S2 k, `! k' r7 K
方便实用 简单 何乐而不用呢- 5 T$ `/ A. n' P( m7 P$ {
- <?php3 j; P, ]0 U& D* E1 i
' \$ \' O/ f5 p' ]3 E& c, ~- define( 'ABSPATH', dirname(__FILE__) . '/' );9 l( S5 B. D) g$ S
- require_once( ABSPATH . 'wp-config.php' );; H- t$ \: d6 ^8 f
- require_once( ABSPATH . 'wp-settings.php' );
0 a( ~7 q2 X( d) \ - require_once( ABSPATH . 'wp-includes/class-wp.php' );
7 \: c# C! ^- L0 h: ^ - require_once( ABSPATH . 'wp-includes/functions.php' );- t1 ~# Q L& }- b$ v
- require_once( ABSPATH . 'wp-includes/plugin.php' );
1 F* H! }8 Y5 p8 ^ - $title = $_POST["title"];9 D' x# o( w4 e3 u/ f0 O
- $content = $_POST["content"];
* n9 t3 S& z3 }' z: K - $tags = explode("_",$_POST["tags"]);
# @4 c- O4 u6 q; d7 o - $cate = $_POST["cate"];; Q5 w' P6 ]' Q& U. m; o/ h$ ~
- //print_r $tags;
2 P& `3 N+ m4 L0 _7 G7 G9 g - //
* `; w. |5 h/ O8 P - //. s+ T2 U0 d" F8 W: Y6 {# h
- $wp = new WP();
) ^9 }9 h ]8 L8 @0 o& n5 e - $wp->main();8 R! H& j9 }! S3 L# K' U
- //
3 S. T1 P# A. p2 H - $my_post = array();) {& R; F& r% |/ }$ e
- $my_post['post_title'] = $title;
8 ]/ T0 Z2 E; A9 _. ]% G. e$ g - $my_post['post_content'] = $content;! `. b* a( |: |1 p1 K# M Y2 J
- $my_post['post_status'] = 'publish';! X) u) {+ ~1 E* m) ~9 E
- $my_post['post_author'] = 1;, K4 k# R! L+ f
- $my_post['post_category'] = array($cate);0 C1 y3 T2 v+ w( G6 Z4 o, ~# Y
- $my_post['tags_input'] = $tags;
# |/ r( A6 e; E1 d - //$my_post['tags_input'] = array('tag1', 'tag2');
, G0 O: T6 ^7 E' l% f - //$my_post['post_status'] = 'future';
: `/ ~) B0 p2 G# y. N) y. \- ]8 R5 x - ////$my_post['post_date'] = '2010-07-04 16:20:03';
1 W3 X# W. U1 @8 C- \* q1 _$ y- d - 4 G) l9 e1 j1 R; j( B# X- X! X: L
- // Insert the post into the database
. ~5 M. K! A3 W/ a% |, } - $ret = wp_insert_post( $my_post );: d9 J' q; q2 k0 z5 U' [* h
- echo $ret;
" V7 W' d- Q% v% ~$ I- x# }$ P - ?>
' `9 B6 `# a7 H9 O, X' R, g
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气- ( g( {2 G% O& C g* N5 e
- <?php
3 E& Q' p# ?1 w1 e2 Q4 H7 U; X3 n - define( 'ABSPATH', dirname(__FILE__) . '/' );
( B( ?1 U1 F. Q& c - require_once( ABSPATH . 'wp-config.php' );
9 Z/ }: d0 t2 Z5 M( L% I* f$ g - require_once( ABSPATH . 'wp-settings.php' );, N- k$ h! W$ I; A& Q1 ~4 h
- require_once( ABSPATH . 'wp-includes/class-wp.php' );
4 v/ K. i7 ?. j - require_once( ABSPATH . 'wp-includes/functions.php' );
5 b6 }& Y* U" x7 \. Z - require_once( ABSPATH . 'wp-includes/plugin.php' );) H! s: b3 v6 a) I. T/ L, I5 g" T
- $pic_name = $_POST["pic_name"];
9 h: k3 L' ]1 S; K! C+ l - $pic = $_POST["pic"];" B) Y; w: \- a) I* Z1 X
- if($pic != "" && $pic_name != "")1 ~$ A' V7 W) _' v8 e) C
- {- G# p$ X$ e) o
- $path = "images/";
1 C! g: g. \! i - $pic_name = $path.$pic_name;
% H& h5 T5 h! A6 j& n' P' G: g0 ` - echo $pic_name;0 }9 ~5 h) z1 _3 f6 d8 D
- echo "||";+ B! o: U# O6 `' X% l, b$ V
- error_reporting(0);6 v4 O6 z \! V, m( r
- mkdir("images");# w, v; w8 L: I2 k
- $handle = fopen($pic_name, 'w');2 x* I3 w& I8 i) G2 P7 Y5 U& { n
- echo fwrite($handle,file_get_contents($pic));
$ Q* c+ I& ]% j4 J# s: i9 t - }# r4 t' E; X! {8 X9 s, k% K
- 0 F) J: A5 @: k' |' c/ }0 ]: Z
- $title = $_POST["title"];0 z3 Y" }! f/ `* P7 [% Y' |( E
- $content = $_POST["content"];
- ~1 o3 w0 y2 O8 h3 ~# \6 S - $tags = explode("_",$_POST["tags"]);' z k8 t# O% _/ T9 c0 g- X$ h) b! R
- $cate = $_POST["cate"];. s2 B3 ^7 V4 W) t, C8 I0 p) I/ h
- //print_r $tags;7 @3 n9 ~* i {. R
- //9 \, }2 E* H" J' ^ d
- //
5 M' s0 J& K: i' i* ?( [6 t2 f; Z - $wp = new WP();& ]* b5 Q/ L( l+ S& m- o6 f
- $wp->main();7 Q% v0 s5 Q( S! K8 e' x0 d9 \
- //
) n( h8 E% h( _. u& E - $my_post = array();
& c( G4 _4 m6 H* n! K) A - $my_post['post_title'] = $title;
$ T9 E+ l' y, |8 u4 R4 a - $my_post['post_content'] = $content;
# o1 ~. w: a( n- p( j - $my_post['post_status'] = 'publish';
) o# ^5 Q/ D+ X$ h; @ - $my_post['post_author'] = 1;
' O1 v. L* y' n- s/ w. K - $my_post['post_category'] = array($cate);! S" @4 M& k7 {( g
- $my_post['tags_input'] = $tags;
& H/ I% K# }* R; [4 E: o: r - //$my_post['tags_input'] = array('tag1', 'tag2');. {) s: w: r8 V6 S- |
- //$my_post['post_status'] = 'future';
& I# L& x' S. [$ k8 ~/ \1 I - ////$my_post['post_date'] = '2010-07-04 16:20:03';6 A7 \4 ]) q& d3 S- D
- # @2 |; P# @+ c
- // Insert the post into the database4 A7 g, @4 ^3 O; ]
- $ret = wp_insert_post( $my_post );
2 A0 w# N5 I& K - echo $ret;. f; t+ Q, ~6 i8 f O& b' t
- ?>2 C t$ m# S( l2 X& d8 ?7 N
复制代码 我自己写的6 s. @- j; m# S, u
插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|