|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑
& u% U2 C0 n/ k/ b% c5 t* \: {" ]6 k0 K5 x
直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持
5 I; ~+ f2 d6 w3 a) u( l8 B( Z% b# `; R) h e- }
我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了
* x" c1 s ? W: ~6 {8 u- ]1 {) `) q4 \% D5 H8 J; D
方便实用 简单 何乐而不用呢
: w! J* I+ K8 g- <?php9 R/ e( _8 d1 i
- % T: |! [& S. Z6 e$ o% z4 |& Y
- define( 'ABSPATH', dirname(__FILE__) . '/' );9 J2 j+ E, f7 R
- require_once( ABSPATH . 'wp-config.php' );
) n; n# E O7 k7 m - require_once( ABSPATH . 'wp-settings.php' );
; [" v0 U" J, d# e* z - require_once( ABSPATH . 'wp-includes/class-wp.php' );
1 n6 t6 u! O0 u- t7 ?- U - require_once( ABSPATH . 'wp-includes/functions.php' );8 [* ]/ m! A+ K0 P
- require_once( ABSPATH . 'wp-includes/plugin.php' );
8 m% I* g9 T# \ R - $title = $_POST["title"];) y, H0 X# L7 v, u) w) j
- $content = $_POST["content"];
4 H j5 C( z; z! Y! o - $tags = explode("_",$_POST["tags"]);
; b8 b: W) s9 k& N - $cate = $_POST["cate"];7 O) c6 ]% k2 m$ {0 o4 l% C
- //print_r $tags;
# Q& _1 H; _& O/ c7 W - //3 ~* T# d& y; Z/ ~# {7 ^2 S
- //
9 D+ h# z% M( ~' [4 Y - $wp = new WP();! j" O& Y0 v7 i+ M
- $wp->main(); L# {& w7 \- G V% l
- //
, ?3 f2 t+ r6 r0 j! W7 m - $my_post = array();
# \( s; [1 P& R - $my_post['post_title'] = $title;, @9 b% B/ o/ d& A7 r+ Z# {9 Y/ P
- $my_post['post_content'] = $content;) C* |7 v* }) x4 ]% V
- $my_post['post_status'] = 'publish';
7 F% _& b' P3 C5 d2 m. d- u - $my_post['post_author'] = 1;
c; w+ X8 T+ j; N0 _ - $my_post['post_category'] = array($cate);: j+ I7 o& y* T
- $my_post['tags_input'] = $tags; f l# _1 t, V$ v' h7 w
- //$my_post['tags_input'] = array('tag1', 'tag2');
+ A# O* J; X$ l2 R" V: i - //$my_post['post_status'] = 'future';
) Y5 y# y# u0 q6 s: w$ g2 g | - ////$my_post['post_date'] = '2010-07-04 16:20:03';* M4 X* ^# E; X5 z: L( x
9 X2 _! j& M, E- // Insert the post into the database9 Q Y* _9 ?0 M: o& |
- $ret = wp_insert_post( $my_post );
' C. G/ }7 U4 w1 Z/ l - echo $ret;
+ q: ^) F! I+ X! l- W - ?>: } r d! |& g
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气
$ v7 f: g9 | {) Y# s- <?php9 L `7 a$ G' d+ R) `
- define( 'ABSPATH', dirname(__FILE__) . '/' );
- e' y9 P' ?1 o& G* Z5 b3 v - require_once( ABSPATH . 'wp-config.php' );& ~# _9 O# ~2 S. T9 x
- require_once( ABSPATH . 'wp-settings.php' );
0 c7 Q6 f6 Y5 \& d- f - require_once( ABSPATH . 'wp-includes/class-wp.php' );
# a* b' Z& N7 c7 {+ o5 w% B - require_once( ABSPATH . 'wp-includes/functions.php' );
# n. w" _6 ]* y, ]7 h - require_once( ABSPATH . 'wp-includes/plugin.php' );
( g4 A9 M+ l: ?+ `& q - $pic_name = $_POST["pic_name"];
' A* t% A s0 b; I" s) j: z; Q - $pic = $_POST["pic"];" E |2 I5 L* D5 E# y0 i
- if($pic != "" && $pic_name != "")9 K2 ^" [1 L4 y3 U; l% E3 k9 p
- {
4 ^' N* A" @5 X) A - $path = "images/";
. h5 `5 i6 y6 o" H5 K - $pic_name = $path.$pic_name;$ b" X* B F; U8 r f U
- echo $pic_name;4 b9 @# t9 Z5 Y5 c9 d( v( u9 {1 I
- echo "||";
9 U6 q# ]. ~, M. m/ o - error_reporting(0);
7 Q! d( @+ {- e# b7 C( H1 k - mkdir("images");6 e- f3 u% _0 p
- $handle = fopen($pic_name, 'w');
- s( Y3 `! j4 ^6 F - echo fwrite($handle,file_get_contents($pic));9 T: G. Q8 R, u( R
- }' g! K. P" k' u/ a9 m
* a7 e8 ~9 f7 N; ^. E" k- $title = $_POST["title"];! d C s1 G3 M/ h* y
- $content = $_POST["content"];7 Z0 u7 h5 l1 \# u2 C( c( ~
- $tags = explode("_",$_POST["tags"]);# ?3 T8 C3 k$ j8 v
- $cate = $_POST["cate"];5 M& T! c4 J0 @! `& S
- //print_r $tags;( e. j( ~2 h2 y9 r% g7 } r! F% u
- //7 g9 A/ h4 @+ W! \& D# d
- //2 l0 \+ ^' ]4 \
- $wp = new WP();
: e3 x X, \, k$ C r" S, S1 V - $wp->main();& a& O1 Y; R$ z0 r
- //
6 L: { L; g6 h5 H4 ^- u5 f - $my_post = array();7 n& b% w; L; J. ^* h/ G
- $my_post['post_title'] = $title;0 {! f5 u- P7 E7 c
- $my_post['post_content'] = $content;
& X0 l# G* F! ]. c8 C2 ]& a - $my_post['post_status'] = 'publish';. r, c& W4 x1 V
- $my_post['post_author'] = 1;& K3 } i* a6 z' h! W. t5 k
- $my_post['post_category'] = array($cate);
- U* Q8 A& a( i% T) r+ ] - $my_post['tags_input'] = $tags;' c% D0 j" J9 Q/ E3 e4 x/ f* j
- //$my_post['tags_input'] = array('tag1', 'tag2');
9 [' V3 J, Y& M! s5 ?, B6 J$ O - //$my_post['post_status'] = 'future';* }& n5 h- s2 @& t. i8 |
- ////$my_post['post_date'] = '2010-07-04 16:20:03';2 N; x# V+ a/ l2 i6 {& r# H+ F: N
% b7 W0 a8 }; g% s$ C- // Insert the post into the database
, p. c0 G8 f: K5 o0 M3 d$ Y, L - $ret = wp_insert_post( $my_post );
4 I7 L, O, Q' h" I/ ` - echo $ret;$ O r1 t5 `, N8 Z9 ]2 ^- f; A
- ?>0 @7 T, r' |1 G# N3 }
复制代码 我自己写的5 `% h3 m/ C0 K3 o3 u" E
插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|