|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑
7 K2 @6 u2 v7 y* G& L7 r' c- R- Q/ F t8 f* T4 H5 N; C
直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持
4 _+ ?* A* x8 G0 Q* z" g7 q D# B5 U* c* a3 |
我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了
1 i& ]( P5 _! [( D9 T9 c
7 p, y9 c% Q! w8 a0 {! j方便实用 简单 何乐而不用呢- . O% @+ e1 s. k S% N) z- f
- <?php5 S% P" H) P! u6 t
- 5 E+ f( d& ]! R7 A8 c
- define( 'ABSPATH', dirname(__FILE__) . '/' );
/ M+ }5 B) R9 m4 v - require_once( ABSPATH . 'wp-config.php' );8 r7 |7 N/ V. s# g, I
- require_once( ABSPATH . 'wp-settings.php' );3 Z6 n# l( f4 ?1 [
- require_once( ABSPATH . 'wp-includes/class-wp.php' );
# P" ]4 j( O' c0 r) j a# S& I0 ?8 ? - require_once( ABSPATH . 'wp-includes/functions.php' );
- g- w$ x% \+ c. l- X" u - require_once( ABSPATH . 'wp-includes/plugin.php' );
! B8 x/ w" Q: D* {% G% |7 Q" ], T" X! a - $title = $_POST["title"];
4 K$ [* w3 k/ K0 x/ U - $content = $_POST["content"];
) m5 J( M7 i- Z, f5 v - $tags = explode("_",$_POST["tags"]);
2 ` n9 m g! P* q b - $cate = $_POST["cate"];
6 Z; E- r, w3 `; T+ |. y$ z, q. b - //print_r $tags;9 K% N; I5 N$ v. Q
- //2 T0 X# ^/ X" {( K* |( u K4 S X- k
- //: x, ~+ l, m: M0 t2 l6 Q/ }
- $wp = new WP();
; _! E) a4 S- u5 V - $wp->main();1 Q {2 ?7 R1 I
- //) c' V) X* x5 H! Q P
- $my_post = array();8 I' h" T l! j7 b2 N" S
- $my_post['post_title'] = $title;+ w5 i! R2 M C& k5 O6 t' p& e
- $my_post['post_content'] = $content;
6 |2 [! [( p( Z) v5 N- @ D - $my_post['post_status'] = 'publish';
2 H( |( t3 @- K$ c# q+ o* k - $my_post['post_author'] = 1;
* A& Q' S9 ~4 [( W4 j& I$ w+ { - $my_post['post_category'] = array($cate);3 l& W6 S* f6 h! k
- $my_post['tags_input'] = $tags;7 \1 e% n# m% q! Q) O1 Y, \/ P
- //$my_post['tags_input'] = array('tag1', 'tag2');; d% b0 }3 _0 V& s6 g4 H
- //$my_post['post_status'] = 'future';! I ]1 ^- y1 @1 T9 e/ i% E5 s
- ////$my_post['post_date'] = '2010-07-04 16:20:03';
& E4 w% \/ q$ ?# z9 d1 l- J {( i - 1 R: e3 y5 Q+ n/ Q
- // Insert the post into the database8 V! J. l' d$ W2 a
- $ret = wp_insert_post( $my_post );
; m E7 ^: m( f+ K' G - echo $ret;" D5 k0 p; g8 k* t) U. G! K5 n
- ?>$ l/ L4 }9 T; [
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气
: l9 z# [, a s1 @4 e/ T n5 u5 J- <?php7 t' H4 n& n7 L3 K; D0 @. l. ~7 ]
- define( 'ABSPATH', dirname(__FILE__) . '/' );' W0 {0 S# r, n3 T
- require_once( ABSPATH . 'wp-config.php' );
m% x/ R5 m# N# `5 |& G - require_once( ABSPATH . 'wp-settings.php' );
; t4 }! P5 I* e' {, B% I0 O+ \ - require_once( ABSPATH . 'wp-includes/class-wp.php' );
$ m `; {- f- o- Q$ Y4 n3 L - require_once( ABSPATH . 'wp-includes/functions.php' );6 Q. V3 H" K9 x6 k
- require_once( ABSPATH . 'wp-includes/plugin.php' );0 U4 Z$ P4 |5 ^9 \5 ~& c. A
- $pic_name = $_POST["pic_name"];! q5 M4 _) b+ F: \7 K! N9 ~
- $pic = $_POST["pic"];" D- N6 }: {. W9 J
- if($pic != "" && $pic_name != "")$ j. L( J+ i- T
- {
0 C# C" K' M! L. o4 t9 r - $path = "images/";
( W' u; H' y0 I5 w5 K/ }6 J2 [+ A3 {1 R# V - $pic_name = $path.$pic_name;! Q9 V9 h" n! H1 t
- echo $pic_name;* k# ~- u4 K6 e1 X& g
- echo "||";
- {: ?$ Z+ r) S/ V0 W - error_reporting(0);$ X2 z2 R/ K* }3 W
- mkdir("images");
' l! b7 Y2 ]7 M$ Q - $handle = fopen($pic_name, 'w');/ X7 Y3 }% H I1 U& o
- echo fwrite($handle,file_get_contents($pic));
, [0 i1 F. W9 M5 I5 n - }9 Q0 j1 s [: v' j9 Y) V" Z6 F
- $ O" M2 d6 \4 t1 ?' r3 x3 w
- $title = $_POST["title"];
! B. `4 P! t8 |* k+ p - $content = $_POST["content"];
, g9 [; j/ L1 f: _/ p6 b) S - $tags = explode("_",$_POST["tags"]);
9 Q5 L3 u6 I% ?) L' }8 l; x - $cate = $_POST["cate"];
& ]2 u3 D2 Q- l3 T - //print_r $tags;
+ j' W8 C: P' y- \4 J: Q. E3 e8 A - //
1 | X/ q, O, ^ - //
7 }! V/ d7 w: o6 }* X5 s1 t - $wp = new WP();/ w$ A7 Q: n! o. ~5 h9 ]
- $wp->main();- J$ ?& I. u) b: C4 v9 k! H
- //
+ _1 I9 h2 a2 Z) | - $my_post = array();
- b7 z& v5 R2 x- E' N$ J - $my_post['post_title'] = $title;9 m% `5 `" |. p" Q& e
- $my_post['post_content'] = $content;) y/ j# b- u8 D" k& W( @/ l+ K u
- $my_post['post_status'] = 'publish';
$ j0 X5 T( p! e& }% @ - $my_post['post_author'] = 1;
- y8 \5 M: f% v" n5 |3 l! Y; B - $my_post['post_category'] = array($cate);
' w8 x, o( G) } - $my_post['tags_input'] = $tags;
, s3 A% t/ ]8 t1 f% }8 Q4 R - //$my_post['tags_input'] = array('tag1', 'tag2');- P7 Q2 t+ R. @! H. C; r( H8 r/ j3 M
- //$my_post['post_status'] = 'future';
$ A: A4 {/ t7 i. j/ S' A - ////$my_post['post_date'] = '2010-07-04 16:20:03';: R2 l# f: v5 U$ g+ u& F
- 7 a% x! X2 L0 D, [
- // Insert the post into the database2 c% c" B% Y9 V; ?6 P
- $ret = wp_insert_post( $my_post );
- L1 l; @4 }5 ? - echo $ret;
I, o, u- a0 C: Y Q - ?>. Y( u5 l0 d) U* v
复制代码 我自己写的9 W- b# P! F* \, L% p
插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|