|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑 : @& B: W' C7 r, M! I
; W: \' F# X9 f- P8 e直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持3 G$ g3 U& }! \& F N2 _
. D( j( n) ]3 s5 A) V: h) u
我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了
" i! {" L G& t9 t. Y- G9 P- i e- r8 N
方便实用 简单 何乐而不用呢
' N9 F" a" B3 h% ~. ? b- <?php
3 t2 `; b( { n. y# l - % _0 N) m2 U" c# i N
- define( 'ABSPATH', dirname(__FILE__) . '/' );8 d4 `+ R6 }1 s) ?
- require_once( ABSPATH . 'wp-config.php' );& U" |0 e9 n! V8 \3 j% A. c
- require_once( ABSPATH . 'wp-settings.php' );" A+ D1 V. U( P- f) s
- require_once( ABSPATH . 'wp-includes/class-wp.php' );$ I R* K$ N. m/ \
- require_once( ABSPATH . 'wp-includes/functions.php' );. K; _4 v z. W" N* x6 G3 O8 F9 w
- require_once( ABSPATH . 'wp-includes/plugin.php' );
1 _. R! ]& O: F, n0 T _ - $title = $_POST["title"];* L U" T" f) a. _& ?( e1 p
- $content = $_POST["content"];
/ V+ C# _+ q& Z - $tags = explode("_",$_POST["tags"]);
3 g! d0 a" V% H7 s - $cate = $_POST["cate"];) \5 }7 \; a) L, R
- //print_r $tags;. a1 H* R& f* }1 T6 f
- //; O# ^+ {" e) j0 K1 W
- //, G1 a" e5 F3 F) M
- $wp = new WP(); \8 P7 z) J" m
- $wp->main();
) U& E% k0 x: |! S% ]+ V% h - //
1 J/ O) _8 X3 Y4 Y3 V' n$ `$ ` - $my_post = array();7 x, F% u; e* s: {
- $my_post['post_title'] = $title;
1 M7 R1 X7 f( Y1 |" C; P - $my_post['post_content'] = $content;
5 a* L% k' a' z/ B6 P n; M - $my_post['post_status'] = 'publish';* y7 w1 H0 y# j# d0 F
- $my_post['post_author'] = 1;
7 a1 O# V" d* C- ~ - $my_post['post_category'] = array($cate);
6 W! J' a( q, ]/ a2 W3 k7 p! l - $my_post['tags_input'] = $tags;3 ~) {3 ~& l/ c1 a, \
- //$my_post['tags_input'] = array('tag1', 'tag2');
. A( ~7 V& l2 J2 {1 D' _% @, I i8 I - //$my_post['post_status'] = 'future';
1 `7 Z1 K1 A' T2 W% j - ////$my_post['post_date'] = '2010-07-04 16:20:03';
: I1 m+ P" t7 ~1 d' k$ t; P, p
+ x; r" v5 O# U- // Insert the post into the database
, [& W& D0 S4 e2 l - $ret = wp_insert_post( $my_post );2 J' r8 x! ^- b3 j) A9 ]+ C
- echo $ret;
+ t% z. o' y) }& o$ l - ?>% A9 z' b& F. Y7 c) @
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气- 2 m8 ], \" ]7 ?. M& x* K+ q' w
- <?php8 Q* w& n7 q; ], `" i0 R1 c. i
- define( 'ABSPATH', dirname(__FILE__) . '/' );
4 p7 b( M+ e: j8 ?) e - require_once( ABSPATH . 'wp-config.php' );
! H( G* Q! q+ @/ f. o0 Q - require_once( ABSPATH . 'wp-settings.php' );# d% i d4 r5 v2 {6 z0 N! P5 h- n6 ?0 w
- require_once( ABSPATH . 'wp-includes/class-wp.php' );
; P% P- U* {$ i' W* F& @ - require_once( ABSPATH . 'wp-includes/functions.php' );
: q, [' m% G2 O, ~- Q - require_once( ABSPATH . 'wp-includes/plugin.php' );
5 D# a2 j# |6 t2 T& _: P$ M - $pic_name = $_POST["pic_name"];4 o; Q4 C& H! O7 U5 W
- $pic = $_POST["pic"];
+ G. F/ [' q8 R, g! z* e - if($pic != "" && $pic_name != "")
! ]" l" Y/ z. e( c/ _: V% j- E, C - {
# X2 P7 G7 I* @; I& f/ x% y - $path = "images/";
! k: K9 L0 z/ {- {& ^$ P. K - $pic_name = $path.$pic_name;
) o6 j' ]5 n Q. ? - echo $pic_name;3 E! b, t9 j/ U; V; b
- echo "||";
6 x% i9 C6 T- P/ M; @ ?+ L% q - error_reporting(0);
! W% X' t! X' L1 P - mkdir("images");
% n) U/ e( `9 V - $handle = fopen($pic_name, 'w');
8 v1 O* ~0 L9 y - echo fwrite($handle,file_get_contents($pic));
" L# [$ W1 T( E6 e0 x - }
3 r/ ^6 j; n( n8 X5 I' N5 s0 I% l
% k1 k8 l2 M% o1 C- $title = $_POST["title"];& I4 N/ E; \- e* s3 ~; p: N
- $content = $_POST["content"]; @0 S# g/ x- l. p7 L2 g( Y" ]
- $tags = explode("_",$_POST["tags"]);
0 Z6 w. R* g% _8 a - $cate = $_POST["cate"];
) P3 @' A, A7 R% M$ h! a - //print_r $tags;
0 d! N+ I' P9 ?. n3 k6 r1 V - //
4 M2 \6 |0 w, P) Z3 Y+ w - //+ p# @, C) o& j9 E @
- $wp = new WP();- r s" K7 [0 n! g! R# I0 y! Q' m
- $wp->main();. U& F! i" F6 t
- //6 O- z3 ]6 k: Z( U5 j) l6 m3 d
- $my_post = array();) D U$ i* ~- _- _8 V
- $my_post['post_title'] = $title;6 s0 q$ R; Y- M- R+ C- f/ w
- $my_post['post_content'] = $content;
: p) l$ P" K$ [$ B* C, G3 h. i - $my_post['post_status'] = 'publish';
! b% T9 ?; x$ b% b& y3 {, ] - $my_post['post_author'] = 1; ^% F0 y4 d8 O6 x h6 y
- $my_post['post_category'] = array($cate);
: u% o3 ]: p/ H1 t$ J( o# L - $my_post['tags_input'] = $tags;7 a! b5 L/ @# R2 P6 o
- //$my_post['tags_input'] = array('tag1', 'tag2');
8 P9 ^( ^/ v* n! f - //$my_post['post_status'] = 'future';0 I, M, l) {8 }
- ////$my_post['post_date'] = '2010-07-04 16:20:03';
* I+ k2 K M z" T% u2 a9 B
; d @, W" D# X, _% R- // Insert the post into the database
% F g$ x1 i, c( g* s3 v - $ret = wp_insert_post( $my_post );
$ {# |: ~$ m+ u/ j! r* b - echo $ret;- `4 z3 h9 |. U1 t
- ?>+ \9 D+ t8 R* G! \- q7 k$ r9 _
复制代码 我自己写的; u- o9 {6 x( ]) n
插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|