|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑
( z) |6 y8 k" |1 }4 @& p; c
; y3 n1 o& ^! D直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持1 j+ c ^% e9 w( n# y* |
5 h; ]9 t! N Z2 ^1 b V: j我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了* Q% m+ D3 a0 o; S$ Y3 r V4 n ?
" i4 i( x1 D7 D1 O& `7 _# j
方便实用 简单 何乐而不用呢- 5 ~, }; n6 J& |" Q% t! Q% d
- <?php$ |5 z- Y4 N2 P4 J
- Y0 w* n/ F, M+ I
- define( 'ABSPATH', dirname(__FILE__) . '/' );
+ o, a+ {' Y( I$ a- h8 o - require_once( ABSPATH . 'wp-config.php' );6 v; n8 {: a; z/ Z+ [# o
- require_once( ABSPATH . 'wp-settings.php' );
: y2 \$ ]1 @* c8 k) Q% i4 Z0 P+ P - require_once( ABSPATH . 'wp-includes/class-wp.php' );
1 Q9 z$ A5 n! S4 F3 u" w, u - require_once( ABSPATH . 'wp-includes/functions.php' );, s/ J+ t! d' L) F2 e$ c g
- require_once( ABSPATH . 'wp-includes/plugin.php' );- P2 F$ S6 b1 S) Q1 B
- $title = $_POST["title"];
/ L. _# p! W9 C8 p: [5 `5 \ - $content = $_POST["content"];
& Y& `/ y: ]7 v, F! ~ - $tags = explode("_",$_POST["tags"]);
j( s; y3 i _8 d& H1 U L - $cate = $_POST["cate"];9 p( U, U& [, t0 s- ^( Y
- //print_r $tags;& P; w% _9 m; U) {0 t- @2 P
- //
% B( p" m" _' E6 s! I6 z$ ^ - //
8 `0 t% w% f8 s7 g( z - $wp = new WP();2 g6 \8 A9 L3 F; o) X7 U' M
- $wp->main();
2 L1 i( f( p" h' a* ] - //
9 w7 p- \' J; n! n" u - $my_post = array();7 ^! q, ` R; y% A9 A) l
- $my_post['post_title'] = $title;: q9 H3 S4 Y. I- @- u4 K0 a
- $my_post['post_content'] = $content;* d+ ~3 N' @0 A# G s- B$ V, R3 A( U% E
- $my_post['post_status'] = 'publish';! H6 b0 D, `" O
- $my_post['post_author'] = 1;
3 a. L* e8 X" r) F9 Y4 B. J7 n - $my_post['post_category'] = array($cate);( }6 c* B# w2 X: [
- $my_post['tags_input'] = $tags;
! y2 D6 @$ ^ h: g: ^$ ] - //$my_post['tags_input'] = array('tag1', 'tag2');( Y3 u j! B Z! t# U/ X m- a+ j
- //$my_post['post_status'] = 'future';+ K9 q$ s$ U* B9 C
- ////$my_post['post_date'] = '2010-07-04 16:20:03';
6 m) Z) h) O6 a9 X" o* u7 w( n: W - ( N/ f" y, h. Q, g: U3 |7 c9 b( l
- // Insert the post into the database4 [2 ?7 l; s9 r6 |" s- k
- $ret = wp_insert_post( $my_post );
5 d& `8 y. n& R) A8 M2 z5 m3 P9 l - echo $ret;
3 F3 P. q, v7 y! M- ]4 M - ?>
. k4 G' m- d1 H
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气- & L1 d# d% K% M S
- <?php& A2 c8 `: e4 {# f( {- E
- define( 'ABSPATH', dirname(__FILE__) . '/' );
% Z3 O( o+ W# g' w# |$ I - require_once( ABSPATH . 'wp-config.php' );3 K' [; C! R% \( Y1 p+ S2 }
- require_once( ABSPATH . 'wp-settings.php' );% B$ a4 w s8 ^
- require_once( ABSPATH . 'wp-includes/class-wp.php' );$ d7 Z2 A* h# ?9 q
- require_once( ABSPATH . 'wp-includes/functions.php' );. E1 Y$ P q& Y! }3 o
- require_once( ABSPATH . 'wp-includes/plugin.php' );
6 n( i* M; f+ A5 b6 P - $pic_name = $_POST["pic_name"];
# p6 `, Z; M( d' G2 y9 w9 } - $pic = $_POST["pic"];/ P! O4 G$ s8 U
- if($pic != "" && $pic_name != "")
4 Z7 D4 R4 P" b9 ]7 C" ]: U - {! f f- B4 M, C5 R9 L3 X1 J
- $path = "images/";( K; C# i$ O4 J5 V
- $pic_name = $path.$pic_name;! O2 j% Z6 f* U4 Z$ Q/ s( ~* n
- echo $pic_name;
2 {& `7 G* W$ U - echo "||";
6 z2 Q7 v3 b) Q ^( f/ y% m - error_reporting(0);
7 C: W) m& Z8 [/ A' `" s! l, i - mkdir("images");. |' Y, o/ O/ Y" S' ^, M
- $handle = fopen($pic_name, 'w');8 B; l8 _2 f* D; C1 Z3 N
- echo fwrite($handle,file_get_contents($pic));
' t& Y# x8 \7 M6 C - }
; e7 y) } S Q3 H0 ? D% U' G
( ?3 s5 h) Z% H, n3 l$ m4 Z4 U- $title = $_POST["title"];* |) e3 R- n/ d. h/ S
- $content = $_POST["content"];: S! L X/ c: [# z6 h
- $tags = explode("_",$_POST["tags"]);2 A; f1 W$ I, \
- $cate = $_POST["cate"];
' N" }% A# I* F" `- k1 S# } - //print_r $tags;' x- J% {; C9 _, n+ F
- //
% j: k2 j/ k3 Y7 _7 {% M- l* l - //
& K5 R8 Q {4 @( F) W9 b - $wp = new WP();
' I4 k6 O6 @0 s5 ^! l - $wp->main();
2 U- L: _2 q3 x- x" A - //
0 z( D5 N* W* V/ V$ T* w; f - $my_post = array();7 O: i. O T o+ m" ~0 l
- $my_post['post_title'] = $title;
$ p; o8 j5 K8 n7 E7 Z& s/ p- y2 h - $my_post['post_content'] = $content;
# Z6 K/ D, m# P8 v4 s; I - $my_post['post_status'] = 'publish';4 I* h6 R' C# P4 W
- $my_post['post_author'] = 1;4 F$ w% R" w5 I! f0 F
- $my_post['post_category'] = array($cate);" v% F7 ?; o; c, Q9 K2 r S
- $my_post['tags_input'] = $tags;7 l$ s1 J, m( ~% H
- //$my_post['tags_input'] = array('tag1', 'tag2');" H' i# ?2 Q2 F; }
- //$my_post['post_status'] = 'future';
$ ?' ^1 |. I% ^, ~ - ////$my_post['post_date'] = '2010-07-04 16:20:03';
0 Z6 B# I* [8 x& ?! z - 5 t7 b3 v$ R, W+ f! d/ X8 x8 U
- // Insert the post into the database, @% o) w1 O8 Y" @
- $ret = wp_insert_post( $my_post );
4 j; v0 R2 E1 Q- m+ t - echo $ret;
Q( c7 k0 _0 h" G( i8 p+ Q6 T - ?>
/ m' N/ U z) X+ k( u" x
复制代码 我自己写的5 x- G' w' y0 F! }4 X: b. j
插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|