|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑 4 ]$ ~0 Z; e2 [' e8 {
% ^3 c6 N( H" h' A. a6 P, _8 P
直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持: q+ e0 T2 D2 u9 T# S0 N# X
: y8 Z+ X: g; O% ?$ O1 y我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了; m; c: l1 ^. t: K; B! M
: \: y7 S! f J
方便实用 简单 何乐而不用呢- 3 E" Z2 @ k; q# X- r3 m
- <?php
5 ~+ s- `4 v( k' W5 v$ i - % a& [/ c0 I! B( ^/ n/ d
- define( 'ABSPATH', dirname(__FILE__) . '/' );
) N$ y! Z$ t6 l" i. C: K% _) D - require_once( ABSPATH . 'wp-config.php' );
! {; h: R8 R3 L - require_once( ABSPATH . 'wp-settings.php' );. R# i% t D& e; U/ d
- require_once( ABSPATH . 'wp-includes/class-wp.php' );3 i/ j/ h. ]' L
- require_once( ABSPATH . 'wp-includes/functions.php' );
' K1 L& j5 Z4 ~7 Y. j - require_once( ABSPATH . 'wp-includes/plugin.php' );! [4 c, e$ N8 ?* N, ^
- $title = $_POST["title"];
; \0 f$ x. M, } - $content = $_POST["content"];
4 a* j0 w: Q, Y2 I8 | - $tags = explode("_",$_POST["tags"]);
+ v3 g0 y$ {" u* Y" |( z6 H" s3 q" r - $cate = $_POST["cate"];8 o9 U9 k( L6 f. T* V6 Q: K+ ?
- //print_r $tags;4 Q m5 G U! x, a0 M3 r- S
- //$ u( T* q5 z+ c' T
- //$ _ U O2 a3 w2 @' `7 U0 @
- $wp = new WP();
3 l9 O/ {; h# L4 o, e- c) o l - $wp->main();9 {, d+ B7 [* `3 c8 U# h, ~
- //8 F' o9 K; V# F8 x8 m
- $my_post = array();
3 a. k2 J# _$ I* v' y- |/ w4 ~ - $my_post['post_title'] = $title;6 x1 Q" C' n3 y
- $my_post['post_content'] = $content;6 ^9 [% J0 A6 l2 r
- $my_post['post_status'] = 'publish';/ ~+ c/ _7 T7 W# q0 A9 C
- $my_post['post_author'] = 1;
r# P' k, K: e: d, x2 D, L$ p - $my_post['post_category'] = array($cate);
* q0 K# h9 i" o+ _1 |2 _8 d& d - $my_post['tags_input'] = $tags;
- O2 E* c% ~/ s1 I+ o3 y8 Z9 t4 a+ S - //$my_post['tags_input'] = array('tag1', 'tag2');
, ~) x M+ {5 b! g - //$my_post['post_status'] = 'future';4 H& p# v! B& F
- ////$my_post['post_date'] = '2010-07-04 16:20:03';
5 I; @! c7 J4 ?
- V; _3 I- [! W! J9 C! L- // Insert the post into the database
w8 }* [5 y' N% a - $ret = wp_insert_post( $my_post );; x3 F6 _4 a |* V0 ~
- echo $ret;
# l" `) o. B4 d: U - ?>
E, ^: Z2 c }' h
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气
, W* A. l0 z& n, j. [& |2 D) [- <?php
+ @/ X0 h7 `/ ]; Q, L* x* W - define( 'ABSPATH', dirname(__FILE__) . '/' );( t: m0 P3 D r# J. f
- require_once( ABSPATH . 'wp-config.php' );' p# r) T8 i3 e% J) i$ Z7 T
- require_once( ABSPATH . 'wp-settings.php' );# u; A) f( O2 z/ L$ t
- require_once( ABSPATH . 'wp-includes/class-wp.php' );( S# C& a9 v$ e
- require_once( ABSPATH . 'wp-includes/functions.php' );7 d* q" L( x4 M; ~ g/ o( }0 N
- require_once( ABSPATH . 'wp-includes/plugin.php' );
8 Z; v: N9 W# E% c - $pic_name = $_POST["pic_name"];
! P, |0 j% w4 f7 Y - $pic = $_POST["pic"];2 i# h4 M: S- j2 R4 l
- if($pic != "" && $pic_name != "")
% Z; n# E9 q2 p# d2 {& ? - {
5 ~ r, O n& l3 c - $path = "images/";9 X+ y/ S" M( ~3 L/ D+ |! J
- $pic_name = $path.$pic_name;
# d1 i' Q- B) B) [" b. J7 @1 M - echo $pic_name;4 ?+ I( b( U4 |7 {
- echo "||";, }' ^, M% l4 i! J9 \
- error_reporting(0);
3 |) Y8 y- n, y' v - mkdir("images");0 H& w$ F5 S: ]( J
- $handle = fopen($pic_name, 'w');& W j5 V% |/ n7 |1 e
- echo fwrite($handle,file_get_contents($pic));# H: \5 G5 W" k7 M# z5 j: p
- }) r9 N4 G& x. p0 {8 h; q5 h8 c
1 G Z% q- r4 L2 i2 ^0 _& L- $title = $_POST["title"];
# i+ T& Z' I' ]# ^2 a - $content = $_POST["content"];
8 V: T3 M, Y- D3 y; R: [- q - $tags = explode("_",$_POST["tags"]);7 R' M- r- y' B; v7 O. Z/ j9 o
- $cate = $_POST["cate"];
u7 G1 y# A" D4 b) K - //print_r $tags;6 b6 \ ^6 X6 l7 H* G7 L
- //
9 W0 U: V$ B! Y - //6 {) l6 a7 d( G$ k6 B
- $wp = new WP();' I. i. G4 a$ \1 r
- $wp->main();, h& Y. v/ F ?' J- C' @
- //
: P2 T/ J; S- d - $my_post = array();
& T2 H# F* }$ u! a - $my_post['post_title'] = $title;( d' q0 B7 r, f. h! T
- $my_post['post_content'] = $content;
! o2 Q/ M% W( y& ?/ B [+ t - $my_post['post_status'] = 'publish';- { i9 @. k0 l* G
- $my_post['post_author'] = 1;
' H9 w9 }% X5 C. p - $my_post['post_category'] = array($cate);( b$ K; p) U: c# Q- E) s5 V/ r2 P
- $my_post['tags_input'] = $tags;2 \1 H# R/ @, ^( t9 r
- //$my_post['tags_input'] = array('tag1', 'tag2');" w5 }! o8 D3 ~3 ]! C% k) p0 k* W# P
- //$my_post['post_status'] = 'future';
8 a* U7 D/ R% q6 z+ s - ////$my_post['post_date'] = '2010-07-04 16:20:03';2 p. m* I$ F+ Z7 J1 h9 j! }
- 9 x5 c2 e' x; {
- // Insert the post into the database
! ?& b2 @# |, W8 o - $ret = wp_insert_post( $my_post );
8 P6 i8 V/ C+ ^ - echo $ret;
1 p4 Y- m. P& ]8 S" [ - ?>
& x, W3 S& f9 k5 @* i
复制代码 我自己写的5 f+ e1 O8 V4 b" U
插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|