|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑
" Q% }4 s2 K1 K
9 i% x2 v6 |2 K! ?直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持
. K( W S$ f A: M9 h. U4 A) m6 Y8 r4 [8 s
我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了
* _ U" _. \5 g# b( Q
- ^, H8 S, s% }3 l# @9 K方便实用 简单 何乐而不用呢- % N' `- C" a* h
- <?php4 r" j' }$ U M
: ~; |$ r6 K2 X4 D @/ M- define( 'ABSPATH', dirname(__FILE__) . '/' );
9 E7 X, E% K7 F6 X% k - require_once( ABSPATH . 'wp-config.php' );
& ~3 m1 r# S" Q5 I# ` - require_once( ABSPATH . 'wp-settings.php' );6 t% Q, \% R+ M0 n k" z! ]
- require_once( ABSPATH . 'wp-includes/class-wp.php' );
- m% s C, d6 }; I! w - require_once( ABSPATH . 'wp-includes/functions.php' );
6 E0 W) m( M! u - require_once( ABSPATH . 'wp-includes/plugin.php' );
+ f* Z7 C% n% `3 J1 [; U - $title = $_POST["title"];
/ ^7 Y w0 X3 r - $content = $_POST["content"];2 W/ k' C& v" s) N. k: [
- $tags = explode("_",$_POST["tags"]);! y1 i, z0 m# }4 W& H, {. Z
- $cate = $_POST["cate"];
) V7 q/ D( \: `3 ~, M - //print_r $tags;
% Q9 H8 D2 {" ] - //
" [- q' k( b$ s' \5 W - //5 Y- N5 J3 U3 _ l$ m8 x
- $wp = new WP();
C1 {/ x4 g9 w& U' U; r5 n/ G. w4 G - $wp->main();
2 [6 ]* @6 R7 M* f - //
( B% P8 A+ m: t' O, R$ |. q - $my_post = array();
3 C: Q& u8 i% w/ S - $my_post['post_title'] = $title;2 Q- b5 K% ?$ F& _; f, n7 \/ d# V) ?) Z
- $my_post['post_content'] = $content;! F( @& H$ l: |2 E7 U: q( O
- $my_post['post_status'] = 'publish';
1 e& X. L5 C5 \ - $my_post['post_author'] = 1;9 g. ]! j: Y. A; D
- $my_post['post_category'] = array($cate);
0 k- Y# `7 }& f: x - $my_post['tags_input'] = $tags;
- W1 \: e2 w+ ^2 U, S2 L- E - //$my_post['tags_input'] = array('tag1', 'tag2');
* j5 s% N* F: E6 J - //$my_post['post_status'] = 'future';
! v0 {6 R2 u0 ] - ////$my_post['post_date'] = '2010-07-04 16:20:03';
5 Z2 k, t4 q( n" @ - * J* Q" J. l& g1 Y: n; t
- // Insert the post into the database
& f. Z8 F1 c6 ]3 d& F. N/ o/ \ - $ret = wp_insert_post( $my_post );: \+ g4 b! M6 Q! ]
- echo $ret;- W7 V! H4 p- x' s; {" x
- ?>' H8 ^) d* r) k$ m0 e5 i4 F' Q# E
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气- 4 P7 L5 m. Z/ c$ a: i
- <?php% L. T1 Q& ~! p( a- J
- define( 'ABSPATH', dirname(__FILE__) . '/' );; @+ _6 x! r# ~' B/ P }$ S
- require_once( ABSPATH . 'wp-config.php' );
1 b# S# m( K7 G# e4 F ^& L0 F ] - require_once( ABSPATH . 'wp-settings.php' );
P8 {/ N+ U4 c; w7 b, O2 p* Z - require_once( ABSPATH . 'wp-includes/class-wp.php' );0 b5 x# N2 K: J2 g7 \" ^
- require_once( ABSPATH . 'wp-includes/functions.php' );
& b7 A8 z1 v5 K - require_once( ABSPATH . 'wp-includes/plugin.php' );
. s7 [) N9 Z$ A; y( u" z - $pic_name = $_POST["pic_name"];
# j( V: d& n3 M2 V3 i4 Q - $pic = $_POST["pic"];
! D: B* u+ k. G5 T* B0 s. n - if($pic != "" && $pic_name != "")
' ~5 {2 \3 ] k' Z4 L - {" U2 `$ Y( }& G% b
- $path = "images/";: ~, f5 I5 P7 F2 ]
- $pic_name = $path.$pic_name;
1 r7 h6 ]5 O3 h7 i8 A) w - echo $pic_name;
# c5 K* }: |' ], ~- l8 E+ }4 c - echo "||";
# [ E: m F: A. `5 O - error_reporting(0);: f( d1 h6 G! F8 b
- mkdir("images");/ t- I" S" Y( t( _% S
- $handle = fopen($pic_name, 'w');8 {2 L/ o2 W h& T
- echo fwrite($handle,file_get_contents($pic));
- I1 G) q7 s, t+ x, { - }4 Z2 u$ o4 I8 O3 U" G: w8 ?2 m
- 9 g; P0 |: w4 U: s6 y# s
- $title = $_POST["title"];5 A9 R/ M0 U0 c* O6 x! @( O+ B6 _
- $content = $_POST["content"];
5 S, [) |. }# A9 d - $tags = explode("_",$_POST["tags"]);
" ~8 S F; h& d! n6 W2 X/ ~0 B! a - $cate = $_POST["cate"];" O$ M0 }- \& ~$ Y& N6 [
- //print_r $tags;
+ X9 `+ ]& ]4 P+ _% |4 ?; Z - //4 q8 \% P- `7 O2 x
- //
9 Z/ w( C1 D1 U0 X6 q1 A - $wp = new WP();1 I: V4 Y4 ^7 i' U q4 o4 W
- $wp->main();
0 a2 h! [! X7 c - //: J! |9 @8 `0 y: M6 E* A" \+ K
- $my_post = array();
/ H# t" \) L6 L+ M/ e/ _ - $my_post['post_title'] = $title;5 V4 }5 l- l# Z* R/ D- R( g7 f! L
- $my_post['post_content'] = $content;4 R* X8 N, H- s' O: i
- $my_post['post_status'] = 'publish';
. x' C+ P8 F' z% Y; l6 _ - $my_post['post_author'] = 1;: R/ _# _9 T2 n( Q9 @5 M# U
- $my_post['post_category'] = array($cate);- f. d/ o6 F# e- Y8 _3 I
- $my_post['tags_input'] = $tags;& S# N' L0 \" B
- //$my_post['tags_input'] = array('tag1', 'tag2');
- J3 X; T& e* ~/ A( q) M" a - //$my_post['post_status'] = 'future';
0 M' ~6 X4 e7 Z# \2 q4 X( { J - ////$my_post['post_date'] = '2010-07-04 16:20:03';
' R& r7 W% G8 Q8 N5 ]& V/ Y+ u6 {5 y* x - # [& q, p/ @2 ^) o1 V, {4 M4 |
- // Insert the post into the database' a! S& T4 i0 k0 o
- $ret = wp_insert_post( $my_post );
: Y7 l+ s+ T5 g/ L6 b( L5 } - echo $ret;6 a8 L8 q; \7 @/ ~7 F
- ?>* c0 f9 g" i4 I, q0 N
复制代码 我自己写的4 v( G% @- c2 N+ S. ]
插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|