|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑
' Z4 J7 l- C* u! C. b* |2 c+ p' l) n# O* p* I! \4 Z
直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持/ f- N+ D) C+ b; D4 o
8 @0 _5 J, S. d6 i7 a我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了$ D2 G7 {: I- F4 B2 i
0 i0 O" y* F. P/ P" B: H
方便实用 简单 何乐而不用呢- * ?$ Z2 {" U. M! w0 i/ [
- <?php- ]+ l- n/ V) g" z
- ) g5 U+ _. o0 \3 T
- define( 'ABSPATH', dirname(__FILE__) . '/' );% _$ q3 o9 M5 E* p# w
- require_once( ABSPATH . 'wp-config.php' );$ G% G5 ~% O. w9 M7 P. g
- require_once( ABSPATH . 'wp-settings.php' );
% Q2 F* ]7 c: r, C' \+ |. `6 h2 @2 h - require_once( ABSPATH . 'wp-includes/class-wp.php' );2 V2 Q! r, \8 n8 m6 p0 K& a
- require_once( ABSPATH . 'wp-includes/functions.php' );, I- ~ X) Z7 A
- require_once( ABSPATH . 'wp-includes/plugin.php' );
8 V1 p# K2 ^2 v8 `2 }5 c" [ - $title = $_POST["title"];
& z2 o$ \9 i! I$ p - $content = $_POST["content"];
# m2 r: U0 {7 F- }9 k) L$ | - $tags = explode("_",$_POST["tags"]);
( q3 o7 |8 o, S3 W9 [) C - $cate = $_POST["cate"]; t' R! b# ]( f& H
- //print_r $tags;
+ o+ K5 n7 u1 |6 ~. @0 E9 V - //$ A" E. z2 R5 a, j l2 N7 r
- //
- J- S4 d5 }$ \( Z' p - $wp = new WP();
+ X4 g- x, Y7 c% B5 ^! q& F8 N; G- b - $wp->main();) h+ o, H1 N5 ?* k9 y* I
- //" X2 b8 s& F+ I& Z4 l
- $my_post = array();" u( X5 h5 c2 q. c0 R
- $my_post['post_title'] = $title;6 w. k" C8 d, y* U D
- $my_post['post_content'] = $content;
& A3 `- {/ P: x. E - $my_post['post_status'] = 'publish';
9 X* [6 F! t" U7 P' y - $my_post['post_author'] = 1;
4 G. O% \6 T* l7 P - $my_post['post_category'] = array($cate);
0 D6 q" @# s2 _) ]7 e - $my_post['tags_input'] = $tags;
9 H/ z% M9 {8 t5 c' P - //$my_post['tags_input'] = array('tag1', 'tag2');6 d% u+ K! H7 e. n7 w8 E6 A7 w. W
- //$my_post['post_status'] = 'future';+ \$ z. L% K$ {3 t
- ////$my_post['post_date'] = '2010-07-04 16:20:03'; i8 ^! b1 Z& s" d7 C
k9 \3 M0 W, k3 S- // Insert the post into the database
B, h' H4 H/ ^3 h - $ret = wp_insert_post( $my_post );. `8 i5 |& c8 C# ~! _/ o
- echo $ret;4 {& x- _; T2 z3 R+ S7 x9 w
- ?>
3 ]7 A0 E- A% C& [6 j
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气- $ ~. E; K3 C, t1 I3 | X, O: m5 G6 o
- <?php
0 T& q/ I$ q! Y1 V - define( 'ABSPATH', dirname(__FILE__) . '/' );
& a" l# ?- F. v% k. u1 f' D - require_once( ABSPATH . 'wp-config.php' );
: R+ l: k" _3 {5 B - require_once( ABSPATH . 'wp-settings.php' );/ l3 O3 k6 ?3 v: k) G
- require_once( ABSPATH . 'wp-includes/class-wp.php' );# u) W$ U( p7 M+ g$ n% x
- require_once( ABSPATH . 'wp-includes/functions.php' );3 ]% O! o4 q, b/ C7 N
- require_once( ABSPATH . 'wp-includes/plugin.php' );( S$ m8 q0 d9 s2 A/ S. f
- $pic_name = $_POST["pic_name"];8 x) y+ ?& q! c1 f( Y, c; C
- $pic = $_POST["pic"];
" Y6 h+ ?, H# l- L ? - if($pic != "" && $pic_name != "")
5 _. j' ~( e; X3 v: \0 ^' _ - {
; O5 W) L6 ~; L- Q - $path = "images/";$ i$ P. b7 \' F% X! ~$ g" R3 p
- $pic_name = $path.$pic_name;
- M1 |( {( M& L2 l - echo $pic_name;
* L4 J# c* o, A, E( k1 E: \! H - echo "||";
( [8 o4 u) z" [ - error_reporting(0);
- v2 [" ?6 B' U; W" V6 F( y% T+ z - mkdir("images");
' Y" I& R: Z3 P* F6 a - $handle = fopen($pic_name, 'w');
' p3 i! i2 ~0 h; d0 u - echo fwrite($handle,file_get_contents($pic));
% ?! }: S; b8 H* W0 ^' w: Z - }9 S, Y7 w; W4 S9 K" M0 V
- 5 M+ |/ G0 B( z" Q; p6 x4 \
- $title = $_POST["title"];
; ~; `) @- ^2 t1 j' y - $content = $_POST["content"]; ]3 T _1 x( o% z
- $tags = explode("_",$_POST["tags"]);# C& ]8 b' W+ ~ ?' q. \
- $cate = $_POST["cate"];
, w; u" e+ j- p% x9 Z - //print_r $tags;) o+ A* u! W h4 ~
- //. p! Q8 a1 c; G. X
- //
: ?. `2 u8 ~2 F- i9 g - $wp = new WP();" D' g4 @" L1 F. r8 N+ m; P2 a' F6 ]
- $wp->main();5 W% d1 c m% Y8 R5 ?
- //- J2 T0 b' d2 D' X Z
- $my_post = array();
. s( l. c, ], o2 E7 R" t4 r - $my_post['post_title'] = $title;
$ N+ U9 a8 I2 K7 Y - $my_post['post_content'] = $content;- J7 P0 r" z7 x
- $my_post['post_status'] = 'publish';
* J3 c c& c6 Z4 f; m1 ~ V - $my_post['post_author'] = 1;
3 y2 u0 O# q" j - $my_post['post_category'] = array($cate);
# j& P/ }# y8 ?2 e, a8 I4 U - $my_post['tags_input'] = $tags;
4 F" L2 {" X( J: J - //$my_post['tags_input'] = array('tag1', 'tag2');
, u! y' G: w7 o0 \: }1 x" y* J# D - //$my_post['post_status'] = 'future';
3 p# Y% ^! h& e; f- ] - ////$my_post['post_date'] = '2010-07-04 16:20:03';
- r5 u8 U) R& {% `- a* T
$ E( E# ~. w) M7 B5 o* C4 n( R- // Insert the post into the database/ R I6 A/ H& a0 X
- $ret = wp_insert_post( $my_post );- G- ]+ P6 E, I7 D* g
- echo $ret;
1 j" @" J) |0 v8 {# {2 R - ?>
7 h5 x' z( O5 h/ T
复制代码 我自己写的
- K% D# u, `& l6 ?& N插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|