|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑
, B8 Q6 d/ G9 x" G; E7 C( i4 k" B B% W H9 f
直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持
) j- Y4 y0 B4 g5 m
+ O( M7 b+ C; n0 q( j我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了
. o% Z+ t0 c+ a0 |3 A
& @ F' R3 K# \方便实用 简单 何乐而不用呢- , b; o% h& G Y+ H( A% C! q
- <?php- g u- B2 z; R- u A" N0 o, C: d
4 z1 u- u9 c( Y0 T( g( r; D- define( 'ABSPATH', dirname(__FILE__) . '/' );
/ r% p, K* o8 Y% f - require_once( ABSPATH . 'wp-config.php' );
+ {4 L% C" t& F9 O' U" L; m0 v - require_once( ABSPATH . 'wp-settings.php' );
R B; K5 R; @5 I - require_once( ABSPATH . 'wp-includes/class-wp.php' );3 L5 ]6 p, ]9 Q
- require_once( ABSPATH . 'wp-includes/functions.php' );/ i2 }) _, ^* U( m2 Q4 S7 c
- require_once( ABSPATH . 'wp-includes/plugin.php' );
: {7 B8 Z. v4 z3 j |2 H - $title = $_POST["title"];' X0 T5 r/ |% T: m6 w
- $content = $_POST["content"]; x: K6 }0 G! k8 T7 p+ w; p7 {7 L p
- $tags = explode("_",$_POST["tags"]);
0 E: j- ^4 q3 ` u2 A0 V2 `( q - $cate = $_POST["cate"];$ z: p, {* ^# _# O; Y0 c A) A
- //print_r $tags;0 e. Y2 ?* R. i0 c, M6 x
- //8 z+ \7 d; _, e
- //
6 w! B! h( {5 G! R4 B6 H - $wp = new WP();
9 `* h7 t# U7 ]* [; ~4 U - $wp->main();4 e6 V+ m( K% s0 f7 t( M
- // `4 O9 z5 O( I5 o/ p" g* m
- $my_post = array();
% r$ M# T# \1 M7 e" k$ d4 J - $my_post['post_title'] = $title;
7 G ?+ J F* ^) w% ^9 b; | - $my_post['post_content'] = $content;
, c4 u7 n1 _( M. s - $my_post['post_status'] = 'publish';4 A, |; u L8 ?
- $my_post['post_author'] = 1;0 G9 ~6 B2 H6 p0 f' K
- $my_post['post_category'] = array($cate);0 f* Z" j4 @# H$ X" R) i* j9 a8 L
- $my_post['tags_input'] = $tags;* H" l" ]- U* M, g% d. z) h6 [5 t
- //$my_post['tags_input'] = array('tag1', 'tag2');- M% n5 P6 X9 C% J7 h
- //$my_post['post_status'] = 'future';
% {7 ]& \( P! l - ////$my_post['post_date'] = '2010-07-04 16:20:03';
% z& x8 G, T9 \, ~
8 N' ^8 u3 p- r8 u1 G+ b7 J. O- // Insert the post into the database5 F& g+ P v' [1 ~9 L3 `6 U
- $ret = wp_insert_post( $my_post );
/ R3 O3 ~9 T% C - echo $ret;7 q4 q* f: t; x A
- ?>) {( X" j: E0 e2 J# w- S
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气
& ? f! f+ d3 |: L% H- <?php2 O+ l* z3 r2 F) J
- define( 'ABSPATH', dirname(__FILE__) . '/' );
5 G& v" V1 d3 t3 ] - require_once( ABSPATH . 'wp-config.php' );9 ?9 ^5 _% {7 x: q
- require_once( ABSPATH . 'wp-settings.php' );
+ Q# ]9 f$ y' k0 a - require_once( ABSPATH . 'wp-includes/class-wp.php' );, I% h* A+ M, D& P& Y, u
- require_once( ABSPATH . 'wp-includes/functions.php' );
, l, V6 G6 ~ Y* i- I - require_once( ABSPATH . 'wp-includes/plugin.php' ); W! N& ] \0 X2 ^8 e M% x* ]' M. l
- $pic_name = $_POST["pic_name"];: F% i T7 n* A1 d0 Z
- $pic = $_POST["pic"];
5 y9 S+ ^; r( a3 j0 k+ C - if($pic != "" && $pic_name != "")
! V! b8 }% t& t2 i& M- K - {# [9 s9 w1 l8 F# w0 ^
- $path = "images/";! ^' L: V, z7 O u) C
- $pic_name = $path.$pic_name;
* h8 V% E6 V. Z# @1 h - echo $pic_name;. W3 c- A6 j# u' P# b0 h0 g7 R
- echo "||";: C: E3 S: q# K; k3 ^% A
- error_reporting(0);3 a1 Y7 _) l3 [' u$ `
- mkdir("images");& ]8 r. J3 c+ T7 O
- $handle = fopen($pic_name, 'w');
6 h# i" U* F& M3 ^0 d o* c4 V - echo fwrite($handle,file_get_contents($pic));
+ Q% _5 f/ l/ o$ R - } \ p8 M z3 B) i0 a$ m g
- t9 g7 t; I. g- $title = $_POST["title"];
$ @5 a9 C7 `: ?2 H - $content = $_POST["content"];
( `9 ^( y7 G3 t1 @; ? - $tags = explode("_",$_POST["tags"]);" M9 p" n* y" F, X
- $cate = $_POST["cate"];$ W6 Q- j. V9 s0 J$ u+ k% V; L
- //print_r $tags;, o# L, e# \ o7 A* _; |8 \
- //
8 ~0 B- b' x+ t/ d: S - //
1 o: f" `; u; J8 ?. t5 t6 ~ - $wp = new WP();
8 V1 ?6 C% a. M - $wp->main();* t1 r! c3 u( f" N+ J5 S4 n
- //# C/ a# p9 ]! a; }! M+ Q" Z) O/ d& {
- $my_post = array();
% d" X0 u' H0 \ - $my_post['post_title'] = $title;: T9 _! I; B8 \# H$ @8 }! M0 d
- $my_post['post_content'] = $content;' k* ~$ N' s' \& q) R$ `
- $my_post['post_status'] = 'publish';
0 S" S' \% G, n9 U - $my_post['post_author'] = 1;
8 T0 _% P) j% v. K0 p0 d+ v9 R* Z+ f! N - $my_post['post_category'] = array($cate);9 l$ T) R5 `) X* _
- $my_post['tags_input'] = $tags;
: W* y4 Q8 s T: B( [" Z% ]* g, ?# v - //$my_post['tags_input'] = array('tag1', 'tag2');
2 [& C7 |# ]$ m - //$my_post['post_status'] = 'future';
6 q- t5 p% u9 ?7 ^: | - ////$my_post['post_date'] = '2010-07-04 16:20:03';6 H7 H3 u3 s1 q6 A% P6 ~
9 W- E1 g) K$ y4 Y" Y6 J, |- // Insert the post into the database
; b- q* ?3 ~* k. k! ^9 r# Y& N - $ret = wp_insert_post( $my_post );; K7 Y) _# t2 v4 Y( Y" q0 H/ P7 t
- echo $ret;
4 f7 C5 O7 f, }# k+ O - ?>2 m- v* }- _4 E/ a0 M
复制代码 我自己写的
2 l w$ a% s! A k; N插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|