|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑
7 s3 Z! W- A5 A4 g5 o7 O5 \" _% X9 s& `$ Y
直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持( r5 W! C2 F$ c% r6 \
5 e2 L& T' \$ t
我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了
' B& D8 B4 R9 ~: `. T$ {9 S0 ]( z( j( n( r7 Q# D* ]& X
方便实用 简单 何乐而不用呢
; t) U5 d% N- [% b4 F- <?php. I1 e# l# K: l$ J0 U0 k- l! E
2 o$ u1 h+ o9 H& d) T. G1 V- define( 'ABSPATH', dirname(__FILE__) . '/' );/ E; m# ?' d1 V* M
- require_once( ABSPATH . 'wp-config.php' );
8 B3 Y) X. |$ {! L- E8 B - require_once( ABSPATH . 'wp-settings.php' );
( s/ ` w' V& y0 q" o" T$ m& z - require_once( ABSPATH . 'wp-includes/class-wp.php' );3 p8 O7 _1 T- `2 E2 H" C3 B
- require_once( ABSPATH . 'wp-includes/functions.php' );
3 N" f1 l# R N2 B! I1 v: x - require_once( ABSPATH . 'wp-includes/plugin.php' );, c! u% ]+ W" d! Q& u1 V# l- R& P
- $title = $_POST["title"];
' t) c1 { m. h, w3 T - $content = $_POST["content"];- t: U7 h% F7 c4 w/ V
- $tags = explode("_",$_POST["tags"]);+ Q/ l/ f! ~5 _! x) g- R& L
- $cate = $_POST["cate"];/ B6 }2 Q. T* r
- //print_r $tags;! R. {# _' E6 S4 s
- //
2 [0 Y( h+ v0 w( ? - //
# h- G! J: X7 r, l* t0 F - $wp = new WP();
% V- i" c9 R* w - $wp->main();
% |8 m3 v7 r$ w ^ - //7 A" e% J1 G4 ?) d
- $my_post = array();
& k. o: X. L5 B+ u5 n) p, E& x - $my_post['post_title'] = $title;2 p1 \, l" j r; ~# J
- $my_post['post_content'] = $content;
* d" |6 V% K+ b7 z& U! G3 k - $my_post['post_status'] = 'publish';
& w- h; i' O9 x4 d; N8 X9 v2 C! t - $my_post['post_author'] = 1;
7 L$ b3 i. X! V5 m! P' p - $my_post['post_category'] = array($cate);
) J8 l- `+ B/ |. g% c! T } - $my_post['tags_input'] = $tags;- K4 S8 e1 |6 F# w
- //$my_post['tags_input'] = array('tag1', 'tag2');
( t" v1 q2 x+ B% w, }1 u - //$my_post['post_status'] = 'future';
: i1 d3 h/ A. k, ]( c - ////$my_post['post_date'] = '2010-07-04 16:20:03';
4 R' b5 I2 U3 s! W. A
" @3 H& _5 _: b% L- // Insert the post into the database
: n: S, ^+ ~8 f) _ - $ret = wp_insert_post( $my_post );/ c) q% {0 M9 P! b! ]
- echo $ret;
: ^2 K4 x/ V- A' C' n& t& M: T - ?>
0 Y' ^: a- C9 X; O2 Z1 G
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气
! q5 s: d: {& ~: i- Q- <?php
# j, b% _. P- y) I w: P4 ? - define( 'ABSPATH', dirname(__FILE__) . '/' ); y# y5 ^2 ] {! i8 T& H
- require_once( ABSPATH . 'wp-config.php' );
! x/ q/ K8 c0 ~% c6 \ w* C - require_once( ABSPATH . 'wp-settings.php' );
( S0 Q9 i4 T: N z {* m - require_once( ABSPATH . 'wp-includes/class-wp.php' ); m4 o% ]4 X8 ~
- require_once( ABSPATH . 'wp-includes/functions.php' );4 F y P" H) ?
- require_once( ABSPATH . 'wp-includes/plugin.php' );
* w1 j# V6 c- N - $pic_name = $_POST["pic_name"];
: m" ?* x. g% o& y - $pic = $_POST["pic"];& u; B- }) \9 v5 a
- if($pic != "" && $pic_name != "")
8 F# ^& C; J, F& A - {5 \0 U" `7 }5 H
- $path = "images/";
3 J9 ~& l0 u$ N - $pic_name = $path.$pic_name;; X# l" z, x6 t8 `# k" B
- echo $pic_name;5 d& \; ~( A9 }" }
- echo "||";# S, _7 O# e2 Y% I6 |
- error_reporting(0);# a* F; E, Z& o. i% B ]* c; Q
- mkdir("images");
# R# v! d$ |8 T ?% j - $handle = fopen($pic_name, 'w');
: G2 X, w1 J0 W1 { Z9 Z - echo fwrite($handle,file_get_contents($pic));; [% u3 \; N, L0 @
- }6 }7 @9 H* e( B8 ]! B1 c
- ( |* K0 q; Q. K& O9 ?
- $title = $_POST["title"];# L, }8 n; B' K; V2 L
- $content = $_POST["content"];; Z# g0 I6 }* L
- $tags = explode("_",$_POST["tags"]);8 D; c" h1 n3 H4 W; C+ F% S
- $cate = $_POST["cate"];) d; Q1 Q/ H& ]4 U
- //print_r $tags;
' ?7 G( S' O, X* H/ z X - //
' v! P# i# Z: L3 F, s: c4 t - //
1 }2 h$ g# k6 Z+ }, H5 J - $wp = new WP();9 d$ h6 V8 M. x& x% i% c( w( u
- $wp->main();* Y: C1 q8 G7 P( q7 |4 L$ y2 P: }; o
- // M z( [1 W! {4 d, V
- $my_post = array();3 y4 T5 ^. I# M: O. T+ ~
- $my_post['post_title'] = $title;$ c6 z# u H. U, p) z
- $my_post['post_content'] = $content;4 n: [' M4 g) C" M
- $my_post['post_status'] = 'publish';* z2 S# ]: V& d+ c7 _9 X
- $my_post['post_author'] = 1;
- A" e* K P3 T. B0 ^- h - $my_post['post_category'] = array($cate);
, L3 R6 j4 o N' F/ J$ `8 z( C - $my_post['tags_input'] = $tags;
! j3 r+ I: X3 C% R2 f) f$ P - //$my_post['tags_input'] = array('tag1', 'tag2');
) j7 a) y& h1 F" g e3 ~# C - //$my_post['post_status'] = 'future';0 M4 m/ x* [" e
- ////$my_post['post_date'] = '2010-07-04 16:20:03';
: ]$ S6 C5 `) z; W- u - $ O' j) O5 e( W$ U. u8 f+ V7 }1 u
- // Insert the post into the database" z- w$ `! y7 a# r8 C7 N
- $ret = wp_insert_post( $my_post );5 }& D! R4 s5 l. F
- echo $ret;
6 m5 e4 p! Z3 V( o3 \6 ` - ?>
7 M" M4 }# b) v! B& k+ @
复制代码 我自己写的4 X% Y7 O. F6 d; U- H: s1 l
插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|