|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑 ( Y: H6 x' W# j2 Q' t* Z
; ~3 ^8 I" g0 G* ^直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持
- _7 X! S8 _! n+ I' T# v! p' @$ z1 Y5 n, J0 L9 W
我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了
' Y. v! L: d4 M9 f0 j
) U7 `; v# ?2 G b& i方便实用 简单 何乐而不用呢
8 m; Y) z8 F1 j! q w# }- <?php
0 ~$ P2 e3 w* W4 l - 4 e7 A: @4 R* R% x
- define( 'ABSPATH', dirname(__FILE__) . '/' );
9 ]9 C: `& a: U - require_once( ABSPATH . 'wp-config.php' );
" C$ K7 H) C; x - require_once( ABSPATH . 'wp-settings.php' );
/ ~3 w7 ~1 B- x2 V4 _+ F0 R: B% D - require_once( ABSPATH . 'wp-includes/class-wp.php' );4 G8 a m; F$ ~) R
- require_once( ABSPATH . 'wp-includes/functions.php' );9 C6 r) w+ P( d- ]2 z: F
- require_once( ABSPATH . 'wp-includes/plugin.php' );* J$ T1 a# Q2 n1 X
- $title = $_POST["title"];
; p) c, `# \8 z( B; u+ | - $content = $_POST["content"];
( @; _# X. ]# J) {5 T& W' \ - $tags = explode("_",$_POST["tags"]);( j; S$ K2 U+ |4 \
- $cate = $_POST["cate"];" N& I9 G2 y3 `4 V" S5 B$ s4 q9 _) E
- //print_r $tags;8 H3 g0 ?! D( ]5 t* Z
- /// K0 s8 w! h+ @" @' H
- //
& H: V4 y! a8 w) ?/ w - $wp = new WP();
, }7 ?) E! q4 a D- p7 W. b) h) r - $wp->main();/ B# x6 X6 }# h: J
- //
. N6 b5 E& p3 l: l8 ^. Q - $my_post = array();
/ d; }; V0 s: N- o - $my_post['post_title'] = $title;8 w) O2 O' ~5 U E: e
- $my_post['post_content'] = $content;8 D+ f$ v. ^4 l: B/ A
- $my_post['post_status'] = 'publish';
' n& o; h9 w9 Q6 s9 ] n0 W6 O) ~ - $my_post['post_author'] = 1;
& H9 A, g6 a3 k" k, d - $my_post['post_category'] = array($cate);# n5 J+ B3 Q4 [8 B
- $my_post['tags_input'] = $tags;: j7 i7 o) l# U6 T0 c
- //$my_post['tags_input'] = array('tag1', 'tag2');7 v( ]0 J' c2 q# X/ M
- //$my_post['post_status'] = 'future';7 P% q+ o! z( ]9 L
- ////$my_post['post_date'] = '2010-07-04 16:20:03';
! l& C; ]2 M8 x" h; @
9 }) V" Q5 L% \2 G9 ?) n9 @- // Insert the post into the database
) F6 A; c+ [- X" I - $ret = wp_insert_post( $my_post );# }" N3 M0 M$ M8 |7 O5 w
- echo $ret;3 q0 `8 g1 X [+ Y9 Z' D
- ?>
2 L1 E3 [7 w7 T% b/ w
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气- % P& Q. z) D0 r5 q0 n* M. n
- <?php
+ u: n$ D6 n7 S - define( 'ABSPATH', dirname(__FILE__) . '/' );. f0 x1 \6 r1 Q( S
- require_once( ABSPATH . 'wp-config.php' );3 b, y& H# n5 n3 @! Z2 Z: X: T
- require_once( ABSPATH . 'wp-settings.php' );2 {4 A5 W" F0 M( M. v9 @
- require_once( ABSPATH . 'wp-includes/class-wp.php' );+ J3 K# c- o/ l% v9 I* S
- require_once( ABSPATH . 'wp-includes/functions.php' );0 b3 f, G5 L( v* W* Z- Y9 [! f
- require_once( ABSPATH . 'wp-includes/plugin.php' );1 S5 P; r( c6 z# ^) V$ T$ t) M+ H: ~
- $pic_name = $_POST["pic_name"];
6 P: h5 ^0 b' a - $pic = $_POST["pic"];
+ |- ?: }3 N+ T9 Y( q3 N1 r - if($pic != "" && $pic_name != "")3 K" R& v3 X% p- g1 p q# D( p/ g
- {. w, L5 D8 D$ N4 L3 a$ v4 a! ~
- $path = "images/";$ [9 @4 f: ?% E
- $pic_name = $path.$pic_name;- V' j$ p6 k5 F- | E4 |
- echo $pic_name;/ R+ a9 s+ q0 x- O3 \* J. K( C
- echo "||";
; h6 \) y7 J" \5 V( f - error_reporting(0);
( b" r# e Q3 Q5 _ - mkdir("images");
' h! Z: U' E( i2 j4 M9 t: q - $handle = fopen($pic_name, 'w');- k) V& z f1 U1 p2 h
- echo fwrite($handle,file_get_contents($pic));6 [% p1 `5 m& Y8 s4 y% E
- }
0 w9 e% @- S# j ?3 W. J4 s - , v. \" l3 G9 z/ M1 d9 u/ s
- $title = $_POST["title"];5 H, s: H; u: Q/ e2 W) [8 l, A
- $content = $_POST["content"];! D4 m1 _9 Z" M/ z
- $tags = explode("_",$_POST["tags"]);
3 z% }8 O4 }% I+ q& F - $cate = $_POST["cate"];: R0 O- [. A7 O7 F
- //print_r $tags;4 r" v% e$ c1 G( D9 L! W B7 y
- //
7 k4 Q8 O+ H/ X3 @! W w - //" L2 w6 f/ Y% j2 K5 `
- $wp = new WP();
! ^- V( _$ ^+ N# S3 f$ @ - $wp->main();, c9 m+ a1 ?' C! e8 W+ _
- //
/ X9 {) B0 e( V$ O! _6 a0 c - $my_post = array();
. m0 ^% ]3 x$ f" v - $my_post['post_title'] = $title;
) q& ~' f) F) i+ a$ N$ ] - $my_post['post_content'] = $content;
. m* z0 e5 X1 e7 d p: ~. @ - $my_post['post_status'] = 'publish';& ~. P( ^7 S$ F& [9 @. S/ \: O w; G
- $my_post['post_author'] = 1;; ]% ]& }% e# A0 Y- |( T
- $my_post['post_category'] = array($cate);
+ R8 y9 C) m9 N' C( w( _' ~- | - $my_post['tags_input'] = $tags;& h3 v" f4 }) ]- j% v; p
- //$my_post['tags_input'] = array('tag1', 'tag2');1 t. D8 Y7 \2 L5 [( I$ H
- //$my_post['post_status'] = 'future';
4 Z8 H2 v1 H; }9 H' l2 s$ b - ////$my_post['post_date'] = '2010-07-04 16:20:03';3 M- |/ u0 d6 H! z5 H
- # x4 P: r$ L1 I0 ~( ?/ @$ b2 o
- // Insert the post into the database
$ X$ g- G; C% B6 }! Y) a: P - $ret = wp_insert_post( $my_post );/ s/ S& T! b( U5 P8 M9 D( f
- echo $ret;
' j; {/ k8 R. W: H3 L - ?>+ g+ u0 R3 u, p' x/ H# c
复制代码 我自己写的
' n% ]9 [8 u# D5 u/ B# B7 e插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|