|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑 + _5 U1 Q6 ^! [; r0 e3 \
Y" V9 r+ Q: c3 @% l; {
直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持! m# e# e) G, R9 U! V: G7 R( R# A
' L4 O( ]1 Q& W4 h0 w我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了( w; P& V! b7 o( s/ E9 s% m- V8 e
+ L- l- u* C) i! o
方便实用 简单 何乐而不用呢- 1 H! Z/ \. Y- {$ @) ^
- <?php
0 n9 E5 Y% @9 n& T - ; l% k0 n8 h* m- A" v \# O
- define( 'ABSPATH', dirname(__FILE__) . '/' );6 P# h2 R2 i2 u( |
- require_once( ABSPATH . 'wp-config.php' );5 p6 B# x k! l
- require_once( ABSPATH . 'wp-settings.php' );' d* A, U/ P* G: _+ b6 c7 p( S! g
- require_once( ABSPATH . 'wp-includes/class-wp.php' );- I% g. c) c4 P* _/ Y: p# G# `
- require_once( ABSPATH . 'wp-includes/functions.php' );5 V& X$ J4 n# \6 z7 @& o
- require_once( ABSPATH . 'wp-includes/plugin.php' );$ q8 q9 I3 M& [: r) c0 A
- $title = $_POST["title"];
0 q* @ h( g0 Q. I9 e - $content = $_POST["content"];
5 z7 @! g) V- }& l7 h3 E4 h - $tags = explode("_",$_POST["tags"]);
& G" ]/ B- {8 X: E+ n$ |: [ - $cate = $_POST["cate"];
1 K$ e- A& o, B: p, n - //print_r $tags;' c, J. n5 g1 Z ~% D2 H4 L
- //
. r& W1 {6 x7 [- u0 ] - //
4 l; t8 l1 a/ i5 ]* E2 z - $wp = new WP();& z9 ^$ J' {3 d+ V% w3 `
- $wp->main();
! z8 y" B+ X% @3 v, y$ n8 G - //
* W0 M; E# Z/ V - $my_post = array();5 V/ [( E2 z6 K! i7 N3 Z# v
- $my_post['post_title'] = $title;
$ |7 \: H7 k' p# L2 {; _2 r H - $my_post['post_content'] = $content;* O6 U) D" X# o, G6 C8 v
- $my_post['post_status'] = 'publish';
, p' h1 c' V% k - $my_post['post_author'] = 1;- C; o" \4 K' c! k0 e6 |+ A
- $my_post['post_category'] = array($cate);: g: L/ r0 P# \8 z
- $my_post['tags_input'] = $tags;8 c) l5 d; n2 B0 }* Q; e# q
- //$my_post['tags_input'] = array('tag1', 'tag2');
5 Q, }" S& u0 r% Y - //$my_post['post_status'] = 'future';$ o4 P: v! s) v5 S! x, k
- ////$my_post['post_date'] = '2010-07-04 16:20:03';8 z. x" c3 L% v
- ) l2 u. T# j: X J
- // Insert the post into the database4 c O" y: z% O3 }
- $ret = wp_insert_post( $my_post );
1 f% _4 s% C2 M: h' Y3 b3 u - echo $ret;# k' C' ^- j9 v
- ?>3 X: b$ C" C3 Y3 v) D" M
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气- 6 i; C$ I0 y+ [+ X% y, W4 \ Y8 o
- <?php$ f+ E" I3 y$ g* [& c7 j4 m: g
- define( 'ABSPATH', dirname(__FILE__) . '/' );' G8 Y! @5 H3 p- Y. q
- require_once( ABSPATH . 'wp-config.php' );$ |" T$ I# J! y8 [! y
- require_once( ABSPATH . 'wp-settings.php' );" A5 k- h1 |# K; @! Z/ D
- require_once( ABSPATH . 'wp-includes/class-wp.php' );4 O/ @: \" _( e, Y* Y' f
- require_once( ABSPATH . 'wp-includes/functions.php' );
) N8 `6 n" ~9 b+ U - require_once( ABSPATH . 'wp-includes/plugin.php' );3 \- c6 V" p8 P
- $pic_name = $_POST["pic_name"];: Z8 W2 ~; C& C
- $pic = $_POST["pic"];
' [6 Q/ Q2 f: e0 C* _/ h - if($pic != "" && $pic_name != "")
2 C! Z* D) T, h2 V+ o2 d - {/ o- q: N. `/ J
- $path = "images/";
$ m& I+ |+ n: {. Q( d/ H - $pic_name = $path.$pic_name;
c# L, V+ a: _. G1 D! ^/ ? - echo $pic_name;
, m3 i9 u+ z6 G6 _ - echo "||";; q M# O4 I$ H7 D/ h
- error_reporting(0);0 g. U1 }' p% p+ Q Q6 k
- mkdir("images");9 E$ U8 i3 x# s( b2 k, \8 |
- $handle = fopen($pic_name, 'w');
/ n# a( C* K* r7 Z! W: s. z& B - echo fwrite($handle,file_get_contents($pic));
, I5 a3 Z, e. e e3 ~* P3 k - }
2 T8 u, g) H( O, N9 k4 k6 h2 [
+ f( ^4 e2 g! v" ]" k( t* `6 ]- $title = $_POST["title"]; O/ T% K/ I9 U2 n9 ?$ M% ^
- $content = $_POST["content"];4 c# z. F6 ?- J" Q
- $tags = explode("_",$_POST["tags"]);& s& J, w/ @4 x* R9 S4 R
- $cate = $_POST["cate"];
, ]- j8 B" o' A - //print_r $tags;
$ i: q7 Z: E4 x* a1 O( q& Q - //
/ d$ @6 H3 M8 A - //
5 b1 a7 E# u! H z+ h# A6 c$ v - $wp = new WP();8 k4 @& [; C2 @; [
- $wp->main();0 M( z$ B* C) ^- c& Z% b
- //
3 }; | X& |1 D/ o/ I% O- v - $my_post = array();
0 p% r% B8 C; ]" z - $my_post['post_title'] = $title;' t# m4 K; D5 A* N& A
- $my_post['post_content'] = $content;3 k3 g8 ~- C: g
- $my_post['post_status'] = 'publish';
7 u" b. s; ~6 O - $my_post['post_author'] = 1;$ D1 G* T, _: V, O: R F" _
- $my_post['post_category'] = array($cate);4 A8 m+ y/ X* m1 ]2 t8 l6 A- M' _
- $my_post['tags_input'] = $tags;
, u2 I" i9 O( |) X0 d4 r - //$my_post['tags_input'] = array('tag1', 'tag2');7 q9 H; E5 m3 M
- //$my_post['post_status'] = 'future';2 h% C/ R3 e& _6 H$ c, U: J, n
- ////$my_post['post_date'] = '2010-07-04 16:20:03';
. N. a3 [9 F6 H2 Z
G5 X" ? N$ Y9 i1 c- // Insert the post into the database6 ~5 N, t/ z& ]# ]& \9 T
- $ret = wp_insert_post( $my_post );3 X4 F: R+ m- x4 a8 b
- echo $ret;- g3 N! {; F9 x6 B
- ?>
3 p. F; R' S1 G5 A" G$ r3 W
复制代码 我自己写的
3 m) O. O' v# t$ n$ c8 q) y插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|