|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑 # S' U8 P' J I
' n$ r( B: _6 q6 ]. Z* \) k; r. x直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持9 r( ?: B# `7 l0 b+ ]& ~ y8 h
' A5 T2 M! R. B: H3 w$ T: K
我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了
& e( M3 P7 z2 i& }$ l e3 ?* `6 ~( I2 }
方便实用 简单 何乐而不用呢
% d* _ t# l! Q8 ?, K- <?php3 T. t. l3 N6 @& ~9 T# k
% V, D. i) @8 T2 w- q: z% P# l- define( 'ABSPATH', dirname(__FILE__) . '/' );
$ T1 X6 D# U5 A* D& d" l* B - require_once( ABSPATH . 'wp-config.php' );/ k% O' r0 E* w# k6 q
- require_once( ABSPATH . 'wp-settings.php' );
' U2 Q1 ^0 J, x, L. l9 I - require_once( ABSPATH . 'wp-includes/class-wp.php' );
^, D# _ W# C* m& s1 V - require_once( ABSPATH . 'wp-includes/functions.php' );
" g: b4 O* ^# [: K8 K - require_once( ABSPATH . 'wp-includes/plugin.php' );
3 {/ \; e7 c4 Q0 B* G - $title = $_POST["title"];5 |6 D+ T/ y" _' N P
- $content = $_POST["content"];
( o. h# X$ S( i3 X8 Q: T. w" M - $tags = explode("_",$_POST["tags"]);
3 ^, R6 K f9 v0 h; ^ - $cate = $_POST["cate"];
* c+ N+ F/ u, n+ T( _ - //print_r $tags;$ n! u: W, A& w# w
- //. m9 @: W- }" }2 E# P. J( ?! g
- //
4 }/ w: f6 T3 L) a - $wp = new WP();
0 }5 }$ d* f# N. h( b5 [# Y - $wp->main();
& z5 q! f/ {( ?0 w2 E - //1 F$ @8 j8 h( }! h4 b6 { T
- $my_post = array();6 d- S6 H* W& N: K
- $my_post['post_title'] = $title;! R+ U/ |2 w0 T) e5 d t: Y
- $my_post['post_content'] = $content; f, |$ k& K5 r' j. V9 j
- $my_post['post_status'] = 'publish';4 R3 G9 ~* L( j# T9 X7 ]
- $my_post['post_author'] = 1;# v! e3 B: v m' r: _$ {/ u
- $my_post['post_category'] = array($cate);
: q8 G7 x/ A& T1 t - $my_post['tags_input'] = $tags;1 o2 l0 ? K+ ?# u1 H2 j
- //$my_post['tags_input'] = array('tag1', 'tag2');
7 O& R9 ^8 S$ k" Z - //$my_post['post_status'] = 'future';
# V( o3 C( d' t" O6 T# O& K2 G% H - ////$my_post['post_date'] = '2010-07-04 16:20:03';* L9 L8 O% g* s: B# i- R% x9 B! o2 ~& I
- G. d6 D8 r5 w t
- // Insert the post into the database
3 H: h8 Y! N6 U: @$ s - $ret = wp_insert_post( $my_post );
t2 E* P% }- X5 {$ S2 v - echo $ret;
1 P3 \$ \8 y& _# y, G! V - ?>
% b. M# t* ~- Y6 E/ @: r) X
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气- 4 ~ f9 Y3 ~3 r: Y. n/ |6 T
- <?php& r: p& t2 _$ x
- define( 'ABSPATH', dirname(__FILE__) . '/' );5 |7 K5 Z% h2 e6 M* c* u
- require_once( ABSPATH . 'wp-config.php' );5 x+ O& o6 L. M% a; s2 _: d3 S& @
- require_once( ABSPATH . 'wp-settings.php' );! t) G1 a( g% z2 v) _; k
- require_once( ABSPATH . 'wp-includes/class-wp.php' );
' K( H% |8 l- | G' {6 R" L - require_once( ABSPATH . 'wp-includes/functions.php' );: O) u9 b7 O* z/ { B( H
- require_once( ABSPATH . 'wp-includes/plugin.php' );, _$ }, K/ F5 I8 V
- $pic_name = $_POST["pic_name"];: b+ M0 [' q* n
- $pic = $_POST["pic"];
9 F" Z- x* x- z- ? - if($pic != "" && $pic_name != "")
; i. s& E* ]* c6 i* J - {
2 z: V% ~( R* I/ F- F - $path = "images/";1 c- S; }6 ?0 r; l- C
- $pic_name = $path.$pic_name;
, ~2 B7 v# w& s: h( |$ l! {4 ?2 L% u - echo $pic_name;; K6 F. t9 h7 x$ [# T* [' u
- echo "||";
8 q0 `- q" ^; B* l, W. M9 w - error_reporting(0);
/ l# K, K0 t; X# _4 H - mkdir("images");
0 \. _9 R" G( P/ e1 [+ l' T - $handle = fopen($pic_name, 'w');/ t% t7 _ | I! n5 }
- echo fwrite($handle,file_get_contents($pic));! E" I1 p4 b7 r% l* z: U% i
- }2 f. c/ l/ A7 N7 W4 a* `
- : }( ^: D$ { K9 a5 t: r! K" [
- $title = $_POST["title"];6 z9 H7 w7 `7 ^2 q
- $content = $_POST["content"];
: J& H! Z! L4 X H, b8 ^; r - $tags = explode("_",$_POST["tags"]);
% A3 _2 u9 F: J' ~ - $cate = $_POST["cate"];
3 i V( O1 C3 X9 k2 f3 @& ]+ D$ s - //print_r $tags;1 \/ i* D( l3 n) q" f6 f1 @$ p% E
- //+ J% U/ r. C6 G) ^7 A) `0 {6 \
- //
9 u, G& Q! p- h7 Y' E - $wp = new WP();
. ]# Z; W" M, {& K4 B - $wp->main();( s: q2 m7 `! e+ f9 Q
- //
/ T! @! K4 ?1 |" X - $my_post = array();2 L* L; X9 d4 F( F
- $my_post['post_title'] = $title;: e! v4 Z; r ~5 _
- $my_post['post_content'] = $content;
& }. U- g2 s) ], c - $my_post['post_status'] = 'publish';4 d% _+ k6 h' T: D7 ^; F8 Q# f5 N
- $my_post['post_author'] = 1;6 p$ ^7 A1 }8 b7 Q4 l
- $my_post['post_category'] = array($cate);! d0 y. \4 a5 d: y4 m# ?
- $my_post['tags_input'] = $tags;0 i0 _# j8 l+ [5 D# ~* ^6 z! c
- //$my_post['tags_input'] = array('tag1', 'tag2');! f) O# Z# e% }$ l" R4 o: c
- //$my_post['post_status'] = 'future';+ T# V0 {8 Y+ d8 Q* g
- ////$my_post['post_date'] = '2010-07-04 16:20:03';
, w. S# M' u7 _' I5 h* [2 e - + \$ n |" t* p2 L/ o
- // Insert the post into the database# F9 s: V5 i' U% {# Z$ q- r1 C* U- N( J
- $ret = wp_insert_post( $my_post );
; z* d9 H- N( i$ J# m7 ^/ ^ - echo $ret;$ m" E3 f, I* S( I+ o, S
- ?>7 T7 _' o9 f% a1 S$ K
复制代码 我自己写的
( d' d3 ]/ v+ J1 |; @% S; V插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|