|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑 . r2 |$ e; y- t4 G
6 R, e8 d+ P3 u L/ ?直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持& G+ Q7 J8 T) c- J! V
2 @/ `: `. X/ c) [+ f9 k我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了 f, f% o4 S' I3 a0 W
6 Y0 S: P9 A; d; Q! a) ]; c$ ]
方便实用 简单 何乐而不用呢
$ U( H6 _) Z+ r+ J1 K- x4 y5 b& n- <?php4 l1 `/ W6 ^, S2 q+ \' Y3 Y
- + q6 J+ f, m; P) J1 F2 O/ H! T
- define( 'ABSPATH', dirname(__FILE__) . '/' );
2 W/ ^6 I6 ^2 B! [- r8 j9 J- h- D# Q - require_once( ABSPATH . 'wp-config.php' );* j/ q) d3 X! `/ N
- require_once( ABSPATH . 'wp-settings.php' );6 I* Q9 x0 V4 L4 _2 j
- require_once( ABSPATH . 'wp-includes/class-wp.php' );
- V* _2 G$ y* Y! m+ d$ s7 ~ - require_once( ABSPATH . 'wp-includes/functions.php' );" R) T$ p0 O% b, h* n4 K! H* c
- require_once( ABSPATH . 'wp-includes/plugin.php' );; G0 G, u$ U9 q* Q4 n, {7 N
- $title = $_POST["title"];% Y, _/ B6 |' a; R
- $content = $_POST["content"];
# N7 [1 m5 c$ c. j3 a1 V+ P2 _ - $tags = explode("_",$_POST["tags"]);% U6 H2 c0 s: f
- $cate = $_POST["cate"];
8 v3 [, Z% v; r8 n8 I; d - //print_r $tags;
5 ~4 X: F0 |( r& h! \" ^ - //
( K/ s# G. D% w$ [5 ^* n - //
# L [* y4 m' c* c. P - $wp = new WP();* p4 K: ?1 C. ?% i9 v3 C9 D% h
- $wp->main();' [4 v8 q7 _ n& k* e. N& w. \4 D
- //- Z" Y5 s, T4 l
- $my_post = array();
: H8 X, s f9 F - $my_post['post_title'] = $title;) z) u6 X1 `$ p3 x
- $my_post['post_content'] = $content;4 {0 r7 G7 S: [
- $my_post['post_status'] = 'publish';0 a8 Y$ b }4 l. y, q* B( |
- $my_post['post_author'] = 1;, m3 {- f0 y, {% d/ F4 \
- $my_post['post_category'] = array($cate);+ C5 C# k- w" T- {( z2 q
- $my_post['tags_input'] = $tags;1 d& J7 T- F/ N7 X" a0 R
- //$my_post['tags_input'] = array('tag1', 'tag2');
7 J' |5 V. Y2 F% {) z; @0 _( F - //$my_post['post_status'] = 'future';
l& T% s7 R' ?* S - ////$my_post['post_date'] = '2010-07-04 16:20:03';$ i/ q* J9 ]8 ^5 V! E
- % R. f* f: K# Q5 c- P
- // Insert the post into the database
8 Y' M, W4 f1 C" q+ A. H _- v$ j - $ret = wp_insert_post( $my_post );) B* L* N* u" r, P! D' _
- echo $ret;
3 j. W4 T$ z6 i; L. \& ~! i - ?>/ G- c2 K; w2 M6 i4 d
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气
, X8 J0 w3 W" M$ C- _# T. j5 {- <?php) _0 s4 B9 X. ~
- define( 'ABSPATH', dirname(__FILE__) . '/' );
6 p/ @' G9 f D - require_once( ABSPATH . 'wp-config.php' );& i8 e7 u' K2 t0 O. L/ o& }4 M1 G
- require_once( ABSPATH . 'wp-settings.php' );
/ Q( `& }# M7 ^; f0 k4 y - require_once( ABSPATH . 'wp-includes/class-wp.php' );
% X% O) ?7 Z( @9 T8 x% A& ^8 }2 b - require_once( ABSPATH . 'wp-includes/functions.php' );
: \! o& C2 T% b' R& @ - require_once( ABSPATH . 'wp-includes/plugin.php' );
0 a. k: }4 R% c- B4 j - $pic_name = $_POST["pic_name"];
8 N/ o1 y2 a( ?7 y: \ - $pic = $_POST["pic"];
/ J% m+ ]; O3 V9 d0 X r - if($pic != "" && $pic_name != "")
& u0 Y; {8 i8 d6 H8 I - {
% K# X" M7 X; [! | - $path = "images/";
4 |& E! a( ?' i' j- G' v - $pic_name = $path.$pic_name;
; K# j; l* t; S! P) E5 _0 Z( x - echo $pic_name;" L4 X9 f" D) d/ K4 T$ m1 O7 o
- echo "||";
/ j; r* c) w) e - error_reporting(0);
, R1 H- x: K; q, l2 a1 F - mkdir("images");
8 x0 ^* g6 w/ S' K - $handle = fopen($pic_name, 'w');& d- A! t% P1 X
- echo fwrite($handle,file_get_contents($pic));9 v" W- I% P0 I2 T
- }
- M9 u% l& T1 [% k9 s& v" V
0 }5 Y, m/ ^& G7 \- $title = $_POST["title"];
: C: }$ T/ f6 _: u& @5 A5 R - $content = $_POST["content"];& A' a9 G1 I3 \6 `9 J, m
- $tags = explode("_",$_POST["tags"]);, o4 }) @3 @: e/ ?7 f2 x; ^
- $cate = $_POST["cate"];
- S3 k! R3 t+ K - //print_r $tags;
! _5 g. Z- w& y2 {4 w - //
! j5 u% z/ S F( V& z - //- I- W% S: K" o! e3 M, @
- $wp = new WP();
4 E8 E( A" \: Z9 a3 I/ u - $wp->main();' w7 k0 V% A [, ]
- /// i T- I ]0 g
- $my_post = array();
" y {% Q" _* v& k - $my_post['post_title'] = $title;, `4 ~% y) K, X5 g- E; o& g1 B2 d5 }
- $my_post['post_content'] = $content;
( A3 c r) F N1 |' W: T' o1 h: k - $my_post['post_status'] = 'publish';; U4 u& ]+ N* `# _2 T, L* g: f& o
- $my_post['post_author'] = 1;- [8 s5 B# q6 k% C& f
- $my_post['post_category'] = array($cate);- p. L0 [9 t8 |* l( \5 u
- $my_post['tags_input'] = $tags;
1 b; }4 M+ Z: M" { - //$my_post['tags_input'] = array('tag1', 'tag2');7 y4 w- f: Z1 X) j
- //$my_post['post_status'] = 'future';
" c8 D C1 T6 b% b& d- ?2 q2 J. D - ////$my_post['post_date'] = '2010-07-04 16:20:03';7 ^, E `$ S% [, @# ^8 z* s7 h& g, m$ O
- 1 b# R, q. d6 P! b" |; O
- // Insert the post into the database
" U! b8 K5 ^. P6 j7 E - $ret = wp_insert_post( $my_post );9 }/ A N5 t" [5 [
- echo $ret;6 |# m2 S- E* \1 F) z* {: _% @
- ?>+ M" ?5 e) c# _8 |; H; j) [. I9 e0 U
复制代码 我自己写的, L2 u4 h9 U5 e5 ]. j( t( L
插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|