|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑
J }3 [+ n: r o2 v" s
/ S# I# [, Z/ {, l4 r直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持+ Z1 p7 G+ t. }' P7 O9 |0 ?( H
6 v3 I; I/ D0 I; `3 i我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了8 P& U; ]0 a3 T- N
. j* i' ]7 d, p6 [% J方便实用 简单 何乐而不用呢- % O8 N5 D+ t$ I7 u7 v
- <?php. ^& d. ^5 D& j3 z$ |
1 d! l) f8 R d7 b+ L9 \. _- define( 'ABSPATH', dirname(__FILE__) . '/' );# g; }& @2 X7 `- Z+ }$ Y* X. F
- require_once( ABSPATH . 'wp-config.php' );
1 h6 b/ h1 C7 { - require_once( ABSPATH . 'wp-settings.php' );
) I y/ e# y5 P. d1 t" z - require_once( ABSPATH . 'wp-includes/class-wp.php' );
- h/ Y+ [( ~3 u! F- `, |: @ - require_once( ABSPATH . 'wp-includes/functions.php' );
& [% _. ? j8 G - require_once( ABSPATH . 'wp-includes/plugin.php' );
: b+ n' r( l4 V+ U4 C! t( {3 v - $title = $_POST["title"];; e) Q( t% i- o4 ^
- $content = $_POST["content"];% S8 d0 Q0 f6 {; }' A
- $tags = explode("_",$_POST["tags"]);
" b9 h7 \1 v; \& u - $cate = $_POST["cate"];! F' c% y( e: D0 z
- //print_r $tags;; Z. p D) J+ u! C
- //. S/ J; \6 X! p6 m) |& d* z! o7 _
- //' [3 Y( l' X; c' f4 S7 O# Q
- $wp = new WP();8 }- _( D7 C, D+ E$ J
- $wp->main();& Q# J7 t& t& E8 d+ q
- //
4 [) Q3 Y S' k/ p* D' y - $my_post = array();/ _" ~% ~2 c% s7 I M, h, C1 n0 ^ [
- $my_post['post_title'] = $title;
5 J6 A E( G4 y7 B - $my_post['post_content'] = $content;7 T) S7 Z% b1 Q$ H. M: \# k
- $my_post['post_status'] = 'publish'; K8 ^7 A5 {% d; {% V1 O$ @, N; A
- $my_post['post_author'] = 1; e# s, u! c; [' R+ A4 N
- $my_post['post_category'] = array($cate);1 U$ T' f8 _8 M- C; F; ?
- $my_post['tags_input'] = $tags;, ]( G, y& v/ T/ G- ~1 A8 p [
- //$my_post['tags_input'] = array('tag1', 'tag2'); ]- R& o, C' p' E/ q
- //$my_post['post_status'] = 'future';
) a( B2 Q$ u; \- J - ////$my_post['post_date'] = '2010-07-04 16:20:03';. o. V! ^$ }8 K
- ) C5 b4 I$ N1 r) G$ g Z5 o
- // Insert the post into the database
5 Z: D' k& b3 w; r, ?7 B - $ret = wp_insert_post( $my_post );, \' ?% T# c, ~ ^: Z6 G9 ]. G1 F
- echo $ret;* e& [3 M; J& S& D6 L
- ?>
1 a$ y$ b" O) H; Z6 g, j; N2 c
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气
3 @1 Y+ L& P0 ~" x# S- <?php) I2 x- O* t8 ^
- define( 'ABSPATH', dirname(__FILE__) . '/' );
7 i% {7 a- o2 W% ~ - require_once( ABSPATH . 'wp-config.php' );
% x+ ]: H( k8 d0 H" g9 @ - require_once( ABSPATH . 'wp-settings.php' );+ X6 W$ x2 m1 V8 f' u( h
- require_once( ABSPATH . 'wp-includes/class-wp.php' );
. M+ d; {' P2 \: V - require_once( ABSPATH . 'wp-includes/functions.php' );/ z% {3 Z5 n' y6 X+ r
- require_once( ABSPATH . 'wp-includes/plugin.php' );! {& I' V* _6 j( r& u q
- $pic_name = $_POST["pic_name"];
% L1 r. d2 C; m) i% R, `& g) {+ q - $pic = $_POST["pic"];
) |5 O1 f+ V2 c - if($pic != "" && $pic_name != "")( r, ?( T# } H& }7 O& d
- {
P4 `1 ~0 s- n8 Z, ? - $path = "images/";
9 s& C2 Q8 N* l. Z. A- a - $pic_name = $path.$pic_name;! k9 e7 `& {* |; B
- echo $pic_name;% q: M9 k# l4 g9 g
- echo "||";
# q& g$ K0 w0 m F) C - error_reporting(0);
3 M4 S% ?* k( e/ ^* }6 b0 k - mkdir("images");* O9 h, H! @3 c4 E
- $handle = fopen($pic_name, 'w');; a( _) D+ s* L/ V
- echo fwrite($handle,file_get_contents($pic));
: Z9 s/ h. C/ V9 b1 f1 d, i - }
. q; e1 T) x a, ]2 T- h, S - 8 D- y7 I: N: a" j8 d
- $title = $_POST["title"];8 R# y6 \/ ]# \' T3 h o1 p( B8 k8 l
- $content = $_POST["content"];' X* J$ V# l% A3 s2 u
- $tags = explode("_",$_POST["tags"]);
4 K0 _9 B& @* R- r0 R: c. M - $cate = $_POST["cate"];* b, T! q- t% k7 [4 S: r. z" D, Q
- //print_r $tags;; w' M9 @( F1 G0 m6 f2 N! k
- //
( q2 T- s6 k7 |' |) q) W' r - //
z/ j6 U6 |0 X2 G; S3 b8 u: [. F - $wp = new WP();
) E/ E& W& h) }7 _; @ J/ Y - $wp->main();
7 Q9 |9 v/ T& L& G9 [ - //
. i* x, X% W; b. W - $my_post = array();/ R+ f: ^+ Q# z/ j; H
- $my_post['post_title'] = $title;
7 n: k3 A! _, }& @$ K3 n$ w - $my_post['post_content'] = $content;
/ H8 t+ i" S: t. s/ G( ]& Y - $my_post['post_status'] = 'publish';
- v7 i. r! f( v" U9 B4 u5 A3 y - $my_post['post_author'] = 1;* H: M8 ]/ ]( y. e/ O0 s, B
- $my_post['post_category'] = array($cate);
; o) P- |7 m1 R* N6 O- X' w- x& L - $my_post['tags_input'] = $tags;
* f' L9 J2 _: X5 p2 ^2 q+ n3 k0 v - //$my_post['tags_input'] = array('tag1', 'tag2');
$ c1 V7 I% I2 P - //$my_post['post_status'] = 'future';5 D# Q9 b& C5 b8 r' s0 Z
- ////$my_post['post_date'] = '2010-07-04 16:20:03';
& G( z# U) \; t; C2 m. M. w
' z, p& u$ F3 z8 T; p0 m- // Insert the post into the database
" t2 V/ F6 b6 X - $ret = wp_insert_post( $my_post );
2 L2 a' B2 x+ i" c: P3 C - echo $ret;- R: U l9 Q, H- W! U4 A+ M
- ?>, `4 g, `( e* {
复制代码 我自己写的8 }( q; T0 y9 V- T
插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|