|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑
* H# X$ G# ]5 t# N% \, A T0 p: u' S5 X
直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持 @2 h% E- q7 W/ @! @, q/ Y" @2 @
8 T Q; O" Q+ M5 Q我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了! z! e1 L; U- s4 ^) j1 f8 s
4 k* V+ U, z" w3 ^
方便实用 简单 何乐而不用呢- - ~) T1 a: G- s
- <?php
% |* T4 F# J' M - 0 \7 |8 \- O4 V# I, o
- define( 'ABSPATH', dirname(__FILE__) . '/' );6 h9 }0 k+ D3 \3 l3 d" a5 X
- require_once( ABSPATH . 'wp-config.php' );
; s! o5 u: I, k: E; j& p( }; I3 K - require_once( ABSPATH . 'wp-settings.php' );
4 H- _4 M0 O5 N! ^- M2 v - require_once( ABSPATH . 'wp-includes/class-wp.php' );7 I( ]8 [# n- q" I+ T$ b" S
- require_once( ABSPATH . 'wp-includes/functions.php' );
. j1 i8 }$ H- s. N: Y - require_once( ABSPATH . 'wp-includes/plugin.php' );8 ]* [+ V/ O H) l
- $title = $_POST["title"];: ?0 ^( M: _- q7 [) V
- $content = $_POST["content"];
) f+ [; g8 e5 d5 p) u - $tags = explode("_",$_POST["tags"]);7 u: X _0 y3 ^( t7 m" \! b; O* \
- $cate = $_POST["cate"];- W/ p/ j( f& S( y2 n
- //print_r $tags;
/ ?! B( h# X3 s4 I/ z1 A' {- z" A - //- e9 z( {* l) ?. v2 F+ M; {
- //; c$ h( ~$ K$ T+ B4 ~
- $wp = new WP();* @# d% e- A7 b
- $wp->main();4 k# G7 n- \/ d5 e
- //
- D! A1 E9 T5 B, F# S; u' l+ b - $my_post = array();
3 j9 P" V4 @, R5 L5 M - $my_post['post_title'] = $title;0 U1 b4 j/ ^2 T1 {! \
- $my_post['post_content'] = $content;8 L# l `0 v9 A# J
- $my_post['post_status'] = 'publish';5 N& m a9 r' A
- $my_post['post_author'] = 1;- A( N8 ~6 i) E1 l. @# i+ E
- $my_post['post_category'] = array($cate);" m( U9 M+ Z( U9 E1 A! v
- $my_post['tags_input'] = $tags;
% U/ u' C" q7 _% j$ {( f - //$my_post['tags_input'] = array('tag1', 'tag2');
" q0 P: N8 B. I1 \0 a3 R - //$my_post['post_status'] = 'future';9 P$ a1 B+ ^ g# F
- ////$my_post['post_date'] = '2010-07-04 16:20:03';- u8 k9 r1 C# X) c% R0 e! s
- ' a! b- w& v4 x" @0 |9 g5 m0 \( i
- // Insert the post into the database
0 h0 r) F1 ?& t$ O. P - $ret = wp_insert_post( $my_post );; b7 `9 B2 @6 O! k4 t! A1 [
- echo $ret;& V8 R7 h5 R8 J9 P# W: I) O; E+ L
- ?>/ P+ G, G. y( D$ y8 [
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气- - b. f3 L* w# b- x9 j1 e
- <?php
, M% c4 H# b2 s - define( 'ABSPATH', dirname(__FILE__) . '/' );+ M; g0 D4 V2 m# }9 l+ v
- require_once( ABSPATH . 'wp-config.php' );
6 s* `( k7 ?& \- s - require_once( ABSPATH . 'wp-settings.php' );
# q" q; `& w3 u - require_once( ABSPATH . 'wp-includes/class-wp.php' );
) b* Y. T; T5 S, W - require_once( ABSPATH . 'wp-includes/functions.php' );
- b" r, s# {3 J- E: x4 x9 a* K - require_once( ABSPATH . 'wp-includes/plugin.php' );
; R# G5 @9 M! g) {0 q$ _, `3 C - $pic_name = $_POST["pic_name"];
% f+ A b5 E; |9 S+ P* R - $pic = $_POST["pic"];2 I. y% I# W7 Z* A: t
- if($pic != "" && $pic_name != "")
0 P& E, L% ~" [4 G - {
9 V) x* l" l0 h - $path = "images/";* \7 R/ v6 L; \! g1 J1 v! w
- $pic_name = $path.$pic_name;: u5 Z; _% z& Z1 `
- echo $pic_name;/ e, r3 j9 T; y$ [, o" |6 m3 w1 v1 q
- echo "||";
6 E2 ]$ } N0 @ - error_reporting(0);
. ]" k) d& d, C( e7 ~) n) L6 H L# l - mkdir("images");
) ?0 V+ F& d. [! Y. y! S' t - $handle = fopen($pic_name, 'w');- C r9 V) }2 q- J- S/ p" J" j e
- echo fwrite($handle,file_get_contents($pic));
% [7 V+ O- \1 ?0 |3 n - }
/ `, [% {4 f* L$ z - ( C1 L) B- z7 N
- $title = $_POST["title"];
! q: R0 R. z8 E% w - $content = $_POST["content"];
& ]; T& Y9 A! m - $tags = explode("_",$_POST["tags"]);% u* w7 p9 X! L& D3 B
- $cate = $_POST["cate"];2 H+ c1 Y$ n8 f4 T" C2 ]
- //print_r $tags;: r2 ~! X. @9 c
- //
8 p7 x3 d0 s, i8 g - //
0 v# H [2 {* U: ^3 J - $wp = new WP();: f) D0 A/ B/ q8 C* ]& V
- $wp->main();8 N8 d7 P' a6 S; N& g) l% m' i
- //
* Q( \4 [0 ?. N/ I9 p - $my_post = array();9 P4 ^! J7 I0 O8 G0 i7 p- x
- $my_post['post_title'] = $title;
. H$ j' H/ k) e6 Q; b - $my_post['post_content'] = $content;
0 Q# J6 d+ Q/ ?# j1 b$ O - $my_post['post_status'] = 'publish';
M: m, e1 | ~/ r - $my_post['post_author'] = 1;
9 l, e% F3 f/ C/ C* I; y' b - $my_post['post_category'] = array($cate);
. b, i J2 g; M. e/ B - $my_post['tags_input'] = $tags;. F) ?* S3 d6 z/ h7 K; s6 [- j- }
- //$my_post['tags_input'] = array('tag1', 'tag2');# z4 L8 A" E1 ~- U5 R: \5 Q2 z+ ?
- //$my_post['post_status'] = 'future';9 U0 ~/ h3 b/ q# E7 t: q5 S8 M4 Z
- ////$my_post['post_date'] = '2010-07-04 16:20:03';$ C2 d0 \3 w, N: K6 e
7 [7 P4 B: F2 M2 h# P1 ^- // Insert the post into the database
6 o+ p) F4 a+ O9 _ z+ t4 C - $ret = wp_insert_post( $my_post );1 _% I/ K7 `% c* [, Q' ^
- echo $ret;
, E) \8 _( P' }; K: N2 d - ?>
! j- O( _' L, o8 z# s% ~! q
复制代码 我自己写的
" X; _0 `5 P6 T插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|