|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑 % a5 _; J2 K1 Q2 S/ _
G6 \ ?, X6 @0 v$ S9 ^
直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持# a j2 c8 p, o! p- l8 m
% o4 M6 p9 A9 k! C3 k6 x* j5 ^6 r我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了& C; h- o4 N% y9 a- m& H
: v' N' _" Z0 H, U v6 k方便实用 简单 何乐而不用呢- , ~! B. ?3 E6 s9 g# |0 a; N
- <?php8 f" i$ x0 _3 e' A8 Q5 p
- ) v' w H$ R' z" l
- define( 'ABSPATH', dirname(__FILE__) . '/' );
1 `/ m% U9 \4 r6 J: U1 m - require_once( ABSPATH . 'wp-config.php' );
/ `& e% t1 Y/ y/ B; a" G) B) U - require_once( ABSPATH . 'wp-settings.php' );8 Y4 B9 u5 k1 P! W: e8 m
- require_once( ABSPATH . 'wp-includes/class-wp.php' );
5 e9 U2 P+ Z7 @2 G* W2 \, }0 ~ - require_once( ABSPATH . 'wp-includes/functions.php' );
2 M$ |' a% I1 v1 \ - require_once( ABSPATH . 'wp-includes/plugin.php' );
( w* n% q! y; b1 z& U# w - $title = $_POST["title"];
6 c% L1 w1 }+ B0 n! v( g8 \ t - $content = $_POST["content"];' F# B4 A9 `) j* K, Y
- $tags = explode("_",$_POST["tags"]);
$ E' L) V5 |% L0 o# [* S2 J - $cate = $_POST["cate"];0 L$ d* y9 A. x% }" f! Y
- //print_r $tags;! N% \+ A/ u3 y6 z7 U
- //3 U) z; m* U( |3 [4 z. O; |. D
- //1 V: l8 w1 T9 `
- $wp = new WP();* F7 A4 X8 E Z ^, d6 e7 L5 O
- $wp->main();
* E' L, H) w4 O6 ]) C, Y# A# m - //
5 S" B) U# B6 b4 `0 i - $my_post = array();$ J' t8 t# f. D9 }+ O
- $my_post['post_title'] = $title;
. o4 e0 G' R5 A! X8 u - $my_post['post_content'] = $content; }9 u" q+ h' M
- $my_post['post_status'] = 'publish';
2 U }1 ?6 ]* q3 S7 b" h - $my_post['post_author'] = 1;
, R, o' j+ r& t. b0 ? - $my_post['post_category'] = array($cate);
! I& w* _: r+ H4 |2 I2 l5 f3 {6 a; N* f - $my_post['tags_input'] = $tags;( E- K& m2 S: L& C2 D3 F# |' n4 q
- //$my_post['tags_input'] = array('tag1', 'tag2');# w% B6 H4 f. g8 p7 y
- //$my_post['post_status'] = 'future';% Z$ T2 x; B# \# b
- ////$my_post['post_date'] = '2010-07-04 16:20:03';; ] ~ Z- w+ b0 ~8 m: V
% y3 _4 W/ D3 ^3 K1 w- // Insert the post into the database
; A7 W l' M1 r" J9 T: G/ E - $ret = wp_insert_post( $my_post );
5 ]2 n. S5 J7 ^0 T0 } - echo $ret;' h/ E J$ A+ U( h1 o. U
- ?>1 c$ W3 { y( K, i; C
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气
9 d3 v2 n: I! S+ Y3 `1 H% v- <?php, v2 u% S7 @# P
- define( 'ABSPATH', dirname(__FILE__) . '/' );2 b+ ?" u, O% Q# P- g' T
- require_once( ABSPATH . 'wp-config.php' );! z$ ?# m8 Z" A$ x; Z; l
- require_once( ABSPATH . 'wp-settings.php' );; T* X: S2 H+ L: k
- require_once( ABSPATH . 'wp-includes/class-wp.php' );
+ U2 C1 C+ |( p - require_once( ABSPATH . 'wp-includes/functions.php' );
& s( c7 S% L0 J5 _! ]2 f M4 z - require_once( ABSPATH . 'wp-includes/plugin.php' );6 ^4 I2 R' }' k6 H i
- $pic_name = $_POST["pic_name"];
7 E7 ~" |+ |2 ]8 x5 n - $pic = $_POST["pic"];
6 r. W: |2 h* L! N0 N - if($pic != "" && $pic_name != "")- w0 m9 e: b G% {/ f
- {- c& h: n% Z% k* S. O' Y' f
- $path = "images/";7 s+ z! w" g% [/ f
- $pic_name = $path.$pic_name;
' a) k6 d4 U1 ]3 j+ f - echo $pic_name;
8 d& f6 V2 K; ?' G7 |# l - echo "||";1 j, J! a$ e/ E3 `6 t1 _# l0 _
- error_reporting(0);
& w' p; h b1 l6 _7 o3 i8 z - mkdir("images");
7 ?$ E( x" n! D6 f5 X- P# ^ - $handle = fopen($pic_name, 'w');
* @- h% i: A. ?2 J* a) ` - echo fwrite($handle,file_get_contents($pic));$ j; o5 e( v) R$ k% G$ e
- }. H$ h! m$ F7 b% r5 O$ C8 h/ h
1 C8 |/ r z' ?, E3 b- $title = $_POST["title"];9 {; `1 X& L0 c5 z
- $content = $_POST["content"];* ]$ Y5 E! k3 v7 w {
- $tags = explode("_",$_POST["tags"]);
1 {# {& ^" K9 M# d, o( C - $cate = $_POST["cate"];
* l7 e/ ^$ M) F7 ? - //print_r $tags;
4 d- W1 R/ O0 _, H' U4 d0 v - //) L( a% M* L; y, {6 W
- //! C! o) [, K" T5 M7 B
- $wp = new WP();! i! g: C" d: H, a$ t0 z, l) n
- $wp->main();
( `& R& h2 G9 W2 X - //
- u2 Z' J" c" T, C' V9 L i: q3 m - $my_post = array();
+ }* H0 u/ i5 B - $my_post['post_title'] = $title;
# h1 q; `9 u+ P1 p8 e - $my_post['post_content'] = $content;6 e- Y- m; f% ]& l
- $my_post['post_status'] = 'publish';
; T( f g; `$ [" X+ g0 s" h: [ - $my_post['post_author'] = 1;
+ ]4 o( n' b- f; t3 v - $my_post['post_category'] = array($cate);
& k* {" S4 Y! W, o, E3 [6 U$ H - $my_post['tags_input'] = $tags;& p: W" v2 [+ ]$ p- B* w& _! x) b
- //$my_post['tags_input'] = array('tag1', 'tag2');+ I% t3 a8 [! N @" w
- //$my_post['post_status'] = 'future';
6 \# K# ~: W5 a: j - ////$my_post['post_date'] = '2010-07-04 16:20:03';+ j$ [! o+ L' a- \! B3 N# A
1 B" z: P5 k) I2 n- // Insert the post into the database0 K0 B6 ?, Q- V3 u! @* x
- $ret = wp_insert_post( $my_post );3 c8 {! }& z! t% n/ A; |& b
- echo $ret;
, n; Z/ |1 Z" G; T7 m8 }; W - ?>+ D- G* { f9 J! O
复制代码 我自己写的& @0 O% N; r* ^+ U( J9 i
插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|