|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑
9 r; l [5 Q, _. Z# r
) U( k5 y7 H7 b) S+ B直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持4 ?" n; s9 p2 r0 g5 [2 d
, n5 b5 b& p) t4 J+ `我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了
; Z! D* Y& p$ `4 ~% `0 r7 Q: n T- X K' [ U7 S; k6 O- c0 E. e
方便实用 简单 何乐而不用呢
# _! y! w# B+ X9 g- <?php6 u5 A" ?; W( b, g( A1 t
- 2 x; h7 B& b4 K. r
- define( 'ABSPATH', dirname(__FILE__) . '/' );' h. g# x+ I( @9 o7 R
- require_once( ABSPATH . 'wp-config.php' );
; N+ K& e" Q, X6 d - require_once( ABSPATH . 'wp-settings.php' );& r& p6 C! K% I8 H& Q& ~
- require_once( ABSPATH . 'wp-includes/class-wp.php' );
: t$ o6 ?3 ~2 d. F9 M - require_once( ABSPATH . 'wp-includes/functions.php' );3 x+ Z6 w3 x( w7 K. X- i8 c6 ]
- require_once( ABSPATH . 'wp-includes/plugin.php' );8 z! V y) J) B w7 I. f* N; e
- $title = $_POST["title"];
# e$ \+ W l' m- t - $content = $_POST["content"];
$ X9 ^+ {8 H" ?. S, ~ - $tags = explode("_",$_POST["tags"]);
9 \% c0 ^2 f) |7 C$ w - $cate = $_POST["cate"];1 y# Q! {) A' }' s
- //print_r $tags;
: _/ I9 F/ ]$ }9 m4 ?4 T! G - //
' C; w5 _$ A+ u$ J7 K# p! A - //
, A( B% y7 _' a1 I1 Y, a% o - $wp = new WP();7 t3 V6 Y- o4 u+ a) | f
- $wp->main();( }$ Z# i4 g2 R( _, @& A9 ^! |
- //
8 R# C+ G4 c- R. k, a" y - $my_post = array();9 a; O( W4 H, z. c2 {
- $my_post['post_title'] = $title;
, t8 G. d5 b! J5 F! _6 c; K3 w - $my_post['post_content'] = $content;
& E, m: z1 @# u. i* `$ @4 j - $my_post['post_status'] = 'publish';
: W y& N" C1 y - $my_post['post_author'] = 1;- p, S! h: C. f1 E% E( E2 j! R
- $my_post['post_category'] = array($cate);
) G. K' y8 Z$ s9 a- u1 j) V - $my_post['tags_input'] = $tags;
: a/ U0 p" ~/ D. D - //$my_post['tags_input'] = array('tag1', 'tag2');; A/ r& u: b9 Q; G4 w
- //$my_post['post_status'] = 'future';* K, X3 V: i; I, y7 k
- ////$my_post['post_date'] = '2010-07-04 16:20:03';$ E! Y2 W2 T7 l
2 m4 i2 U8 m, N9 z6 v3 X- // Insert the post into the database5 x3 n ]0 M0 A1 h+ x" x0 l0 e
- $ret = wp_insert_post( $my_post );& e, B; |; g& d7 ?7 m3 l' L
- echo $ret;+ s1 d) n$ d1 r
- ?>, O7 p4 f1 C: w- Q% a, Z
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气
y L( X* R% Y5 \5 ~9 L- <?php3 e3 m" L X+ l
- define( 'ABSPATH', dirname(__FILE__) . '/' );
. s% I- p9 A# a - require_once( ABSPATH . 'wp-config.php' );$ h+ u0 n- G; @
- require_once( ABSPATH . 'wp-settings.php' );$ G- k6 ~+ Z# ]9 k1 f- t
- require_once( ABSPATH . 'wp-includes/class-wp.php' );
$ M6 i* c$ d+ G% \ - require_once( ABSPATH . 'wp-includes/functions.php' );
V S- g# U& \ - require_once( ABSPATH . 'wp-includes/plugin.php' );) k9 \+ c' Q2 J
- $pic_name = $_POST["pic_name"];
% D: P7 }7 A1 k2 ?+ o - $pic = $_POST["pic"];
3 w, C% K2 V$ I+ m; u+ b, r - if($pic != "" && $pic_name != "")4 c4 r6 c P9 G8 }2 U4 @
- {7 t3 Q! {6 r1 i6 B0 {
- $path = "images/";' z- u H$ ~. z
- $pic_name = $path.$pic_name;
7 ^0 E- I$ U( X& H; o - echo $pic_name;
9 |1 Z; ]6 D. j - echo "||";8 t# Q. {3 }# ~
- error_reporting(0);! P8 c7 y) K. y: [% c$ S
- mkdir("images");
3 O7 Z4 \9 t; F5 S, a* s) Q - $handle = fopen($pic_name, 'w');
. v' q2 t& L8 w - echo fwrite($handle,file_get_contents($pic));
% q1 h+ j& w, U, { - }
2 \: q9 l1 I& p3 E# G" z4 p6 h - ! t s( ]) Y" l8 n" i
- $title = $_POST["title"];
0 n( E0 N& p/ {6 B - $content = $_POST["content"];
, q. V8 h J- l% |4 r5 U - $tags = explode("_",$_POST["tags"]);
7 Z/ o' y. U# E# q - $cate = $_POST["cate"];
0 [1 r8 l% b3 u. w - //print_r $tags;1 S3 O, c& r' c; \! V' T7 O/ I7 U5 {
- //& ~' ]: \/ ^9 L7 ~& y9 q
- //' e$ b. G/ }, v8 r! Y8 b o8 u1 a3 @
- $wp = new WP();6 ^( _4 x! o1 a
- $wp->main();8 C& H! h* ~5 }4 |. P, v
- //8 s! }* M8 G" m9 A. {# u* Q
- $my_post = array();
( B3 a1 }6 D9 M5 ?* W4 o: G4 I - $my_post['post_title'] = $title;3 }. E3 g5 g0 l/ v- ]9 l
- $my_post['post_content'] = $content;, @& _- E; s$ X5 F8 v j& W
- $my_post['post_status'] = 'publish';
/ M, _+ _' `( B% [! K1 n q1 I - $my_post['post_author'] = 1;
/ a* t! u, X6 s" } [ - $my_post['post_category'] = array($cate);$ p( C# q" S4 s. z" x
- $my_post['tags_input'] = $tags;
- b; t, N, M0 O9 E7 R* { - //$my_post['tags_input'] = array('tag1', 'tag2');
6 S' M! _0 D7 V( E- {5 ~& Q - //$my_post['post_status'] = 'future';
$ X/ Z6 x2 q9 G - ////$my_post['post_date'] = '2010-07-04 16:20:03'; h% x% R, X5 B1 m+ C9 `7 ]# r* U4 s
0 r2 ]4 D2 M! }: s! {( w o+ L. n1 S- // Insert the post into the database
/ c8 x; y% J# F! M+ k9 V - $ret = wp_insert_post( $my_post );
$ t4 C# k9 b8 g% } - echo $ret;
0 x, o J. ?0 J' Z' l5 Y - ?>
y+ r4 M" n* ^
复制代码 我自己写的1 L, c) m Q4 k
插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|