|
本帖最后由 finder 于 2014-12-28 15:13 编辑 * c( ~ d/ q9 a0 j. v( n
6 B: j9 O. |1 k/ u9 i7 ?& F直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持
0 G5 Y2 [) p& n
5 ?1 E5 `/ o* X) r我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了
, l6 k6 x+ X9 F' K9 ~$ X3 h* \$ d& |; X, B
方便实用 简单 何乐而不用呢
) F, I& \3 @6 }+ A7 }) H- D# t4 I- <?php) N6 Q- T$ J; Q3 G+ j+ s9 J7 c
- 2 v7 F! D& G5 \& z5 k3 T/ Z
- define( 'ABSPATH', dirname(__FILE__) . '/' );5 T& X$ J+ A: \5 C! |. g$ c
- require_once( ABSPATH . 'wp-config.php' );
: W5 C( J% g( w y8 W: D! N# u! C - require_once( ABSPATH . 'wp-settings.php' );
* x3 O5 _0 ]8 ]1 r - require_once( ABSPATH . 'wp-includes/class-wp.php' );: w: c* Q& Y5 q
- require_once( ABSPATH . 'wp-includes/functions.php' );
! p9 k- d. O N# l' r" f - require_once( ABSPATH . 'wp-includes/plugin.php' );
7 t) P& U' i* K o6 @+ @; \ - $title = $_POST["title"];! \! g# I' n3 D
- $content = $_POST["content"];0 O0 o8 a8 w- [+ S+ p) P; o% r
- $tags = explode("_",$_POST["tags"]);
: o3 h8 g2 F' p$ U/ @6 m1 f - $cate = $_POST["cate"];
+ {% }* B- W& ^ - //print_r $tags;
. n4 s+ J5 z: G - //* z/ o$ S: k& ^8 p
- //
5 n7 `) |1 Z; |4 W - $wp = new WP(); N2 r7 j. T( R+ {
- $wp->main();7 L, s: r7 J6 z. m4 R7 D
- //
& b* t) ]- T# [9 X# b - $my_post = array();
) @/ k. c8 b* ^, d B2 s: D - $my_post['post_title'] = $title;9 M) o# m- Z- x* H: w
- $my_post['post_content'] = $content;
4 q+ ]( f- V8 A- F. n0 S - $my_post['post_status'] = 'publish';
5 C) @* X) p- s' J- t" S7 P3 E - $my_post['post_author'] = 1;7 O4 X$ r# J: ~+ Y! t- }: g7 ]% R
- $my_post['post_category'] = array($cate);+ v: Q5 Y* r% b- `: x. v. j
- $my_post['tags_input'] = $tags;1 M) }4 t% E0 T- I
- //$my_post['tags_input'] = array('tag1', 'tag2');" f, K4 R, Y9 C1 B) U+ v4 V
- //$my_post['post_status'] = 'future';
- {6 M, f h% I4 e. Q& D+ c - ////$my_post['post_date'] = '2010-07-04 16:20:03';( D B$ m# U5 }: X2 G4 O' C
- 8 j# O8 K$ i" Q2 w9 N! `
- // Insert the post into the database
Z% e( s- u% E0 z! [7 o" ~ - $ret = wp_insert_post( $my_post );) e5 ]' l1 D' K
- echo $ret;
; x# [; f( v2 K6 Z - ?>! E* c0 S1 i8 e% w3 B$ |4 ~1 v2 a; d
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气
# l" Z! t% {* s- <?php3 `; k: b" T0 p7 y4 E
- define( 'ABSPATH', dirname(__FILE__) . '/' );& D: A' q1 c5 J) N9 `7 R7 W/ [
- require_once( ABSPATH . 'wp-config.php' );
; s6 H6 [$ u* d. p$ P" G7 L7 v - require_once( ABSPATH . 'wp-settings.php' );
. Y0 {* J% _ M/ W, { - require_once( ABSPATH . 'wp-includes/class-wp.php' );
# N. ], A( o9 b$ X6 g' K$ y - require_once( ABSPATH . 'wp-includes/functions.php' );
8 P2 @1 Y- ?- r" v |7 |3 V - require_once( ABSPATH . 'wp-includes/plugin.php' );9 T) e# p G& K- F1 ^+ C* p6 x
- $pic_name = $_POST["pic_name"];' A5 @9 X, i. y5 W/ N5 q6 U
- $pic = $_POST["pic"];
# ^1 w. X2 v+ ]! s - if($pic != "" && $pic_name != "")
6 |1 z) a- K0 Z4 x! f+ w6 S& x - {
0 x& |. o- K+ z o }/ J - $path = "images/";
$ I. E5 R& ^; k5 }8 [ - $pic_name = $path.$pic_name;
2 C9 s: t$ e0 t" H; \" C7 ~8 t - echo $pic_name;
" R" l7 [' f( M* z3 x% t - echo "||";
% F; j0 L- \9 Z7 J7 D6 Y( P! X) } - error_reporting(0);
7 K$ i r, c j5 z' q: u% z - mkdir("images");4 u9 B5 a, j" f) ^ e
- $handle = fopen($pic_name, 'w');
( X( D7 v1 N6 {7 A8 |: w4 ] - echo fwrite($handle,file_get_contents($pic));, I; T {5 y9 w- H- Q8 v$ s
- }4 G( V* B! ^ S! X3 }; t
* l" q, j6 H. E2 k2 o! W3 l- $title = $_POST["title"];4 c" ? J; M7 S' O* Y# q
- $content = $_POST["content"];* Z& J1 J I5 Z
- $tags = explode("_",$_POST["tags"]);5 d9 n' F) H# h8 `- }
- $cate = $_POST["cate"];
+ k2 j# t5 b1 p$ a - //print_r $tags;$ }- [; }" B3 S" d# u' ]8 d e' l" a
- //" v: D( x0 R. k: W- R7 _3 J$ N0 H
- //
U5 O. d% U% \ @6 M9 ?, r3 x - $wp = new WP();
, t+ `6 ?! m3 |4 z0 r5 l$ [ - $wp->main();) ]" ]; e; u* q/ ?
- //! C: _2 B& w' p9 a% [& V
- $my_post = array();* w. y7 h! r% L& U) l' C
- $my_post['post_title'] = $title;2 t' F: a9 s4 l5 |: O$ c: K
- $my_post['post_content'] = $content;
$ t- _" [. ^' \ a% m8 c' [0 I - $my_post['post_status'] = 'publish';
0 C/ y+ R$ [0 J* u - $my_post['post_author'] = 1;* j5 z! F5 C( ]. d" q1 t6 ?" V" D
- $my_post['post_category'] = array($cate);
7 C O2 N/ E3 c: m) U - $my_post['tags_input'] = $tags;* @* o. H& Z) T% L: ]# ]( _
- //$my_post['tags_input'] = array('tag1', 'tag2');' }" F+ b- }. s b% K2 ]
- //$my_post['post_status'] = 'future';1 D: Y1 O6 \* e
- ////$my_post['post_date'] = '2010-07-04 16:20:03';- l {( B+ V' N" b0 ^2 X# U& u
" C" H G0 C0 o Y5 u+ Q- L- // Insert the post into the database
8 s; W3 }$ M/ S& k* l - $ret = wp_insert_post( $my_post );! q6 r% J( B% Y
- echo $ret;
0 G, ~) G7 ]) u i: S! X - ?>0 h* ]& s: {5 C8 Q# B2 b( P* T$ B
复制代码 我自己写的: {9 w/ ?! J5 Z; R* G4 u
插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|