|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑 3 T! V. s# h4 D! i* O
% @- o$ |$ j3 n, j直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持1 h: E/ u e6 N
9 @; Y' \' S9 B0 A) w, t `0 J" A我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了 v: w5 u: ^% [2 \
$ g* j3 h( `% _! w1 Q
方便实用 简单 何乐而不用呢
% v4 s( M* u8 K, j. [: \- <?php; n P& i8 B0 S+ s: r
, g) x% @& Z1 V. `5 e3 `- define( 'ABSPATH', dirname(__FILE__) . '/' );
1 N1 H$ L5 C0 R - require_once( ABSPATH . 'wp-config.php' );
% W$ `4 V b8 E6 d o - require_once( ABSPATH . 'wp-settings.php' );
6 J9 c5 {( S t - require_once( ABSPATH . 'wp-includes/class-wp.php' );
' k1 s+ V4 |( ^( g" u - require_once( ABSPATH . 'wp-includes/functions.php' );
$ l3 V4 ]* _/ H0 S' U - require_once( ABSPATH . 'wp-includes/plugin.php' );
1 ^9 D2 V. j2 z4 } - $title = $_POST["title"];& W* v: W# N7 \2 B9 C
- $content = $_POST["content"];
4 `4 V Z) R0 H& J& } - $tags = explode("_",$_POST["tags"]);
- P, u% M, g' |# W' i4 Y& F( P4 { r - $cate = $_POST["cate"];
) L w9 b5 T; S - //print_r $tags;0 y. c, F d9 P
- //
$ |& u! S+ K( P3 y! B$ c) W# ~ - //% }0 _, P0 E0 Q5 v( P: N
- $wp = new WP();( _% _# ]- `/ Z& v
- $wp->main(); T) W( ?! Y& d% ]% H
- //
% ]* b1 l$ X: D; g0 {7 u4 K/ Y - $my_post = array();- h' V9 I- E8 y: K5 Y( q
- $my_post['post_title'] = $title;
5 W' x5 M! D3 @ - $my_post['post_content'] = $content;
+ E& F8 y0 M: @0 v* j5 l' j - $my_post['post_status'] = 'publish';7 ?/ O7 T' ]1 Y% M& Z: S
- $my_post['post_author'] = 1;- h( K& q/ O) g
- $my_post['post_category'] = array($cate);7 U& }# ~; s7 B) k5 y; l z! g: _! t
- $my_post['tags_input'] = $tags;
6 m. z' K8 f$ ?7 j - //$my_post['tags_input'] = array('tag1', 'tag2');
% B2 r) z+ d3 {$ R: R - //$my_post['post_status'] = 'future';
! l& f# Z8 {5 _$ J: { - ////$my_post['post_date'] = '2010-07-04 16:20:03';& b% g: i4 k* X8 p
- % s" a& _ T2 d1 A; R; ^: l
- // Insert the post into the database0 x" B) N- B6 I, Z
- $ret = wp_insert_post( $my_post );% M4 r5 z. \2 H" K9 o. g; H; S- V
- echo $ret; }# }# {) P4 F$ i3 ~# T
- ?>
1 n3 ~' w9 j5 n9 B) y
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气- , p4 t3 e$ ^4 }
- <?php: H8 G, w! v' E
- define( 'ABSPATH', dirname(__FILE__) . '/' );+ k% r# r8 N( ~: P* v# k
- require_once( ABSPATH . 'wp-config.php' );4 S8 E r! x7 T( `7 I7 e
- require_once( ABSPATH . 'wp-settings.php' );# V6 t, S9 R i8 m9 Z- U
- require_once( ABSPATH . 'wp-includes/class-wp.php' );# i4 Y w; D4 _( c4 s) z
- require_once( ABSPATH . 'wp-includes/functions.php' );
) T0 k% h0 [& V2 Q* ? - require_once( ABSPATH . 'wp-includes/plugin.php' );
) |- C Z. s2 h% O! W/ o9 _ w - $pic_name = $_POST["pic_name"];
% @ M6 M8 F( C3 L2 _ - $pic = $_POST["pic"];
) @& g6 u" b2 s7 R0 a - if($pic != "" && $pic_name != "")* Q4 F7 D4 Z8 T
- {
( P* a' }4 Y( P9 c$ ~0 `" e' v - $path = "images/";6 x/ Z# C/ B' z
- $pic_name = $path.$pic_name;) L( A" Q! N Q/ ?9 {
- echo $pic_name;0 f5 U& r6 @0 N* @$ k
- echo "||";
/ i2 o1 o9 Q1 ?, q+ w6 t - error_reporting(0);+ I' l7 |6 {2 N$ C
- mkdir("images");
3 j0 [; w1 W# ~( s$ p - $handle = fopen($pic_name, 'w');
" V( C7 U- m% g5 T - echo fwrite($handle,file_get_contents($pic));6 ?4 K T0 w- Z9 g" D
- }
. D% ?$ p$ @; _! A
3 Y1 ]+ z6 V& u8 }( t- $title = $_POST["title"];2 G. d( Z1 H. m7 s
- $content = $_POST["content"];
1 _8 @9 j! {) J+ l0 X' A& E7 y - $tags = explode("_",$_POST["tags"]);
# a/ [: b: g( u# u - $cate = $_POST["cate"];- v5 p7 a6 J; E7 {
- //print_r $tags;# S; v5 Q1 D2 N+ ]+ ?$ m
- //
8 q6 H) s: ^0 v; F S, R& U - //
8 ]9 u! ?8 d2 l4 U. w* L0 ]2 y# p - $wp = new WP();
9 J1 L- s2 F+ K8 f- I - $wp->main();; k' ]9 n- h0 `, \# X/ J( h
- //
! K4 G ]7 T8 F. r" Y/ M - $my_post = array();
4 n$ J, a# G1 p" f - $my_post['post_title'] = $title;
9 J2 a) Y, D Z, E; l! G. B1 P - $my_post['post_content'] = $content;! @. _1 p+ W$ h# o/ P, m
- $my_post['post_status'] = 'publish';# N/ k: U1 v5 u4 P6 w& f
- $my_post['post_author'] = 1;& a% d3 d0 \3 j! a/ i2 Y: }
- $my_post['post_category'] = array($cate);
" `9 a: H i+ y+ o7 p - $my_post['tags_input'] = $tags;5 ^) B" c9 G8 c) N6 m: U8 F3 ^9 ~
- //$my_post['tags_input'] = array('tag1', 'tag2');
. m9 \& |" A, C- W: q$ |9 o - //$my_post['post_status'] = 'future';
/ u9 ^8 T, S( \8 g7 n+ W* z - ////$my_post['post_date'] = '2010-07-04 16:20:03';& w# t- X# q- Z3 c! O8 f1 j
- $ ^4 t- G6 v& ^- w! `( L
- // Insert the post into the database
- j( W: a! O+ o0 b$ v# b- J! ~ - $ret = wp_insert_post( $my_post );
; H# S4 @1 J% j$ { - echo $ret;
2 U$ }5 X: \, E# P) s6 U+ x7 P# u e - ?>! u: R7 V& J# ?6 _
复制代码 我自己写的9 V& U: [( f: _# E
插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|