|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑
' t/ \' M8 X4 f9 u: I; D" }, q0 O3 E( J% J8 ?
直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持9 M7 ^8 [3 |: C1 n+ D
. O6 y9 n! u0 b; ~: d
我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了
+ U! k- k; Q, Z7 J( c C2 N: v9 A& E+ q( K" t% g
方便实用 简单 何乐而不用呢
" E1 y3 x9 K" g5 c4 v( d" x- <?php
& W+ s% x: B: X3 }! g: J
/ U! n" v* X7 d+ J5 W# L' |- define( 'ABSPATH', dirname(__FILE__) . '/' );. ?% I' T2 f# x. k1 g/ J
- require_once( ABSPATH . 'wp-config.php' );! R3 p: U' X8 U9 F
- require_once( ABSPATH . 'wp-settings.php' );
9 b. u3 ^% o( ~ x1 I# V' ?6 F - require_once( ABSPATH . 'wp-includes/class-wp.php' );6 [8 t( {% T# ^& N% [, f3 k
- require_once( ABSPATH . 'wp-includes/functions.php' );
/ X7 V% d/ N6 |' ~0 W - require_once( ABSPATH . 'wp-includes/plugin.php' );
) v7 j$ j' n( Y. J8 n' M* F - $title = $_POST["title"];% @; E- n/ W0 O+ e. Y& y9 z. I
- $content = $_POST["content"];
8 s( \& n. H' |: _ t3 m - $tags = explode("_",$_POST["tags"]);
3 F1 c/ m. v, I- ~ ^' T! I3 L0 w - $cate = $_POST["cate"];
0 q) Z3 A" F5 K: K% O N# T - //print_r $tags;
) O$ Q4 b/ Q& N! z& j3 J - //7 Z( a% l; m( C# h1 G* t( Q# a
- //
0 U' E$ @9 C( F, R - $wp = new WP();6 G' b- {6 y a+ s9 d
- $wp->main();
9 P3 O: x; ?4 d- d, R m( U( s - //* c$ i- A4 G Z Y8 j& C
- $my_post = array();3 D" B# j* c8 T5 u
- $my_post['post_title'] = $title;/ d# \6 b; p3 k/ } }
- $my_post['post_content'] = $content; B( F) u- T& R: C8 d- |4 f8 P7 \
- $my_post['post_status'] = 'publish'; z. u M# Y! C4 y0 P' H" W
- $my_post['post_author'] = 1;
4 R/ K" Y" ]9 A& o( F5 S - $my_post['post_category'] = array($cate);
6 Z* f: Q- K* Y5 X f0 g, F8 B - $my_post['tags_input'] = $tags;
- M$ h, d) w, s! }' I$ c2 i9 O - //$my_post['tags_input'] = array('tag1', 'tag2');
9 _) | \6 e* `9 ] - //$my_post['post_status'] = 'future';
7 L1 B) i. v1 p0 q - ////$my_post['post_date'] = '2010-07-04 16:20:03';
7 t1 ]9 k5 R7 Z# W
1 T* y ?/ r+ y6 f! H6 x- // Insert the post into the database: g4 J& B9 {, @$ x
- $ret = wp_insert_post( $my_post );# ]! X, l- l! m1 R4 z- T" w5 e# q
- echo $ret;3 w- Q7 z$ t& H7 p
- ?>4 _/ Y9 Q/ c0 P, t$ t
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气- : w$ g! x9 C' ^& A
- <?php% J) l' W) u/ e$ l
- define( 'ABSPATH', dirname(__FILE__) . '/' );
" I/ ?: t0 s/ |9 N - require_once( ABSPATH . 'wp-config.php' );: a0 ~' q* V3 U E) A
- require_once( ABSPATH . 'wp-settings.php' );/ I7 E' e' u5 v( [
- require_once( ABSPATH . 'wp-includes/class-wp.php' );
; A0 e1 z" ~ b: o! I - require_once( ABSPATH . 'wp-includes/functions.php' );
# j; o$ [% N# L: |& i j - require_once( ABSPATH . 'wp-includes/plugin.php' );' u; w1 {7 o& J' b
- $pic_name = $_POST["pic_name"];
( u9 B( D8 ^- ^ - $pic = $_POST["pic"];4 U2 v1 q' f i3 S4 `
- if($pic != "" && $pic_name != "")3 D+ `; ?% z' @( @; G
- {
) }) w$ ~* w7 E% K* _4 W( ]1 d5 r - $path = "images/";. y0 b# d' O2 p" _, n" d" L
- $pic_name = $path.$pic_name;. I* U+ a# u4 g1 M
- echo $pic_name;; j0 j h9 x$ _9 \. [+ y. C. k! M
- echo "||";8 `+ h4 U0 T/ U' e" l3 o$ [
- error_reporting(0);+ s' k2 R+ H9 G! ?
- mkdir("images");
3 P6 O' W( `& o( h/ y% g+ G - $handle = fopen($pic_name, 'w');) [/ D! J& Q o, N
- echo fwrite($handle,file_get_contents($pic));
3 S: T$ y+ {0 s) E - }6 _9 \7 Y6 k5 D% N( H
- 0 a9 `! H8 T" }8 B7 D5 A
- $title = $_POST["title"];, v4 q$ ^; T7 d( J1 `7 h) b G4 b
- $content = $_POST["content"];
5 G6 J- [9 ~* w! H; g - $tags = explode("_",$_POST["tags"]);
6 R; {& H1 g! e5 H7 W# {; F% U - $cate = $_POST["cate"];
' F3 K/ C C6 e% i0 S, V8 W - //print_r $tags;
; F, q" U$ f' y$ T K" V8 s - //
! k8 i A" B; r: [- }! X4 g* t2 B0 I - //
/ o: E) [) |' ~- P/ `( b - $wp = new WP();$ l" @7 }5 T' k9 J' X
- $wp->main();: K% U: `( ]; ?% w! w, m: o
- //
$ A4 m* ?# I Y" o* t/ G1 O - $my_post = array();, a* x. U& X- J, p# q
- $my_post['post_title'] = $title;+ b7 P8 A% ]2 ]8 n+ O- O
- $my_post['post_content'] = $content;/ T& w) b8 v$ N y, u" h! W
- $my_post['post_status'] = 'publish';
) ?( t7 O$ l/ o8 k& G - $my_post['post_author'] = 1;5 `8 O/ R# H+ Q0 a0 L# ~; q
- $my_post['post_category'] = array($cate); B8 }6 Z' o. d& Q" n- J* _
- $my_post['tags_input'] = $tags;
- g+ x9 p. L! E9 y7 {2 m - //$my_post['tags_input'] = array('tag1', 'tag2');( F+ P0 h2 G) T) [9 _1 E
- //$my_post['post_status'] = 'future';. B5 @; }) Y' \; }" {6 F; g, L; }
- ////$my_post['post_date'] = '2010-07-04 16:20:03';: N: O* q, `& ] A
- 6 _; s$ _7 c9 ]; O& r/ S
- // Insert the post into the database1 y- `" Y8 D H6 C
- $ret = wp_insert_post( $my_post );9 X: [! w9 x+ p2 G& Y# R0 X( Y
- echo $ret;
+ {0 @& M7 i" J: @" `2 _/ ^ - ?>' q7 L( J% ~& a, t4 n9 S
复制代码 我自己写的
% j) U! e+ s1 p/ N插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|