|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑
3 ^5 m# K7 s/ R7 p, T* T# l
) I& X- ^( i' G# N! J直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持
& {) Z `% J) V9 }# S* Y7 K, H. Z% x+ N
我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了
. Q. d/ x) \" d( Q" B: @# j! g3 T
# c7 }6 H; l' \* a! y方便实用 简单 何乐而不用呢
$ ?% [; [- O1 p7 |- U5 Q- <?php8 a$ D' f n) P" W# q
7 J& f, B, [/ F7 Z$ z, |- define( 'ABSPATH', dirname(__FILE__) . '/' );& d8 n/ l8 n2 b# O% K% _$ [1 W
- require_once( ABSPATH . 'wp-config.php' );
, @7 y% [; R4 |) j - require_once( ABSPATH . 'wp-settings.php' );
: \/ O8 Q( a( o: Q* f( W" O - require_once( ABSPATH . 'wp-includes/class-wp.php' );
8 V1 `& M# n" q1 H, E* ?" t6 ^2 v - require_once( ABSPATH . 'wp-includes/functions.php' );, E5 m$ L/ {7 m V
- require_once( ABSPATH . 'wp-includes/plugin.php' );
- `, I, A& F" Q2 E* Q - $title = $_POST["title"];
! u& \) E, ~. P - $content = $_POST["content"];& B4 x ^8 R% v% |* {$ Z$ H
- $tags = explode("_",$_POST["tags"]);2 f6 m7 }6 T- N
- $cate = $_POST["cate"];1 N# F% [0 G5 H7 u. o- l
- //print_r $tags;
( d! C G2 X8 ^4 X& U - //
1 K! s4 X0 d6 E1 @ - //; [6 l; K) A8 L+ e3 B
- $wp = new WP();# T/ w( y* \+ {1 Z
- $wp->main();
: o) ~- h( f2 @) \1 U - //! w) v( r, U% q0 y+ B! h, G
- $my_post = array();+ i% o% l8 G7 }- E4 @! {
- $my_post['post_title'] = $title;
) u% ^1 |+ i; V l/ ~0 P9 r& k+ g - $my_post['post_content'] = $content;. V' ]; N" t% P" J" X7 \$ z
- $my_post['post_status'] = 'publish';
# A, _* N+ e# y# b8 O6 ^ - $my_post['post_author'] = 1;
3 C6 j8 G) S9 P3 z2 { - $my_post['post_category'] = array($cate);
) ~) m5 B) k, a/ f. e( p5 b - $my_post['tags_input'] = $tags;" j- @9 {3 O, D7 ^& L3 z
- //$my_post['tags_input'] = array('tag1', 'tag2');
, k' X: e2 M' ^+ P0 r2 A - //$my_post['post_status'] = 'future';
% Q+ r/ [) c4 ]6 { a - ////$my_post['post_date'] = '2010-07-04 16:20:03';
# a* c/ T' s9 }# @) {' G2 a
& z2 P. v* K7 |1 C8 D. d6 }- // Insert the post into the database
/ p( i# F- J5 b( U5 h7 c- I( y - $ret = wp_insert_post( $my_post );
" t ?& M _7 B3 |, J& c7 W1 z' S - echo $ret;
8 f W% T" T2 c1 S E4 h5 k3 _ - ?>- E' f! \& R Q5 E% }/ Q ?
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气- 1 O) Q7 F8 D. E- F
- <?php
) [# w/ m% S! t3 [0 l& { - define( 'ABSPATH', dirname(__FILE__) . '/' );5 s. Y5 T1 x T6 P% n
- require_once( ABSPATH . 'wp-config.php' );
6 g% O$ k! t( [2 q- { - require_once( ABSPATH . 'wp-settings.php' );3 N% u$ B- B; a1 V4 V
- require_once( ABSPATH . 'wp-includes/class-wp.php' );% A7 {" j5 x g- E2 _0 N
- require_once( ABSPATH . 'wp-includes/functions.php' );& P/ z9 `% k" a% H
- require_once( ABSPATH . 'wp-includes/plugin.php' );# B6 [5 z Z- E
- $pic_name = $_POST["pic_name"];; s4 T! i9 m( G: H; z3 E
- $pic = $_POST["pic"];
; d! n: `8 ^5 A0 J9 g - if($pic != "" && $pic_name != "")
5 {9 _0 ^! O0 ?, h! O' x - {
6 k( o9 j2 g( P- o' m( W - $path = "images/";
8 i7 @7 r6 i) U! ^/ v7 B - $pic_name = $path.$pic_name;3 Q* Q6 k+ ]' P; K( t
- echo $pic_name;
* T. x) V/ M7 c - echo "||";
: d$ w: t2 ?; S9 i: E - error_reporting(0);3 H0 R8 w2 ^' J6 T9 P
- mkdir("images");
3 F2 {% J4 v. I4 B+ Q( [6 |; G - $handle = fopen($pic_name, 'w');+ S& Y$ b% T: J1 J) n$ o& X! b
- echo fwrite($handle,file_get_contents($pic));6 Z3 X/ ?6 t! a9 |( @) G1 q
- }
3 z& ~7 h: g" T) o' q$ y - ( D! K+ l' s9 q, V( \
- $title = $_POST["title"];3 G7 r: t S0 {4 w- s, j
- $content = $_POST["content"];
1 t3 a' u2 `/ o( B, g7 V; i - $tags = explode("_",$_POST["tags"]);
3 _ S: _! a$ u3 C - $cate = $_POST["cate"];* F) K% j9 U5 u" C4 z/ h+ @
- //print_r $tags;
1 ~8 G" d" ]- W. @1 c$ T - /// a$ g* g. B; K" r8 v/ R! }' [
- //
: S: V, g' @+ T% w( o - $wp = new WP();
- N5 e4 c0 r' ]1 w3 F q - $wp->main();6 }4 e7 I' Y2 ^: y9 W' d0 w T
- //
O9 c2 {! {* \! d. k - $my_post = array();: w( s+ I- U1 l% N, { k1 ] R
- $my_post['post_title'] = $title;
' J) C+ W2 q) _0 E - $my_post['post_content'] = $content;
0 s. w2 l9 M! x2 ~ - $my_post['post_status'] = 'publish';7 C5 q: \- P, @2 r
- $my_post['post_author'] = 1;
8 p* ], k y' H" G3 ~2 x% o# B0 | - $my_post['post_category'] = array($cate);" O/ Y6 {) P, m" B- X) X' O
- $my_post['tags_input'] = $tags;- J* ~3 T/ A- E8 O# F7 i
- //$my_post['tags_input'] = array('tag1', 'tag2');
+ P( j5 |2 ^2 G1 T - //$my_post['post_status'] = 'future';: S& r5 e j7 N+ r C
- ////$my_post['post_date'] = '2010-07-04 16:20:03';0 A$ |/ _: p5 [7 Z
- ' Q1 F& O Y& Z2 c
- // Insert the post into the database! [3 A2 F' ?4 d# U
- $ret = wp_insert_post( $my_post );
- K4 U5 h Z: y7 Z8 `0 R - echo $ret;( u( ? a. Z! C
- ?>
* F y+ w l0 {: R6 F7 Y4 n Z. Z
复制代码 我自己写的
# j2 }! Z, d4 o4 Q3 L插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|