|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑
T% }! [+ C. r7 [0 f' U# J4 i# @, y& A; S, }
直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持& `( ]1 o: x N, c0 p, C
3 _0 c. H4 ^5 |1 Y. [& E9 v9 A* V我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了
( a0 L. K" ]$ Y0 W; @
' A4 N5 v! l8 g* R/ g F方便实用 简单 何乐而不用呢- 1 q% v" ?% [3 P7 ^, a+ p
- <?php, w, z% V! W9 ]: [* y; y
4 m6 A. A1 _, ^( A- define( 'ABSPATH', dirname(__FILE__) . '/' ); s6 A# o2 y" M$ ~2 i8 Z5 h& j- b( E
- require_once( ABSPATH . 'wp-config.php' );! {$ i2 a2 @- _1 Z
- require_once( ABSPATH . 'wp-settings.php' );4 X& \6 h1 m7 u* m7 u; ~
- require_once( ABSPATH . 'wp-includes/class-wp.php' );7 X9 O5 e* ^, i2 P# C6 L" n
- require_once( ABSPATH . 'wp-includes/functions.php' );
3 y k# f/ S6 f: G5 _8 G' X7 x3 A - require_once( ABSPATH . 'wp-includes/plugin.php' );
+ n: }- k: x) P, S! Q' J* |% ^8 ^7 p - $title = $_POST["title"];6 E* ~0 l, f( z' j# B9 Z
- $content = $_POST["content"];& w& i% }6 _1 D m0 w
- $tags = explode("_",$_POST["tags"]);2 x1 y2 S: Y2 i) L2 _
- $cate = $_POST["cate"];
# b, r( G0 v4 q6 r( u% r - //print_r $tags;% x, b* O5 O4 @9 O7 Y6 [7 S0 v
- //6 |+ |/ t3 {9 i% }) Z
- //2 E; s1 v1 ]5 U" f, C4 J* n
- $wp = new WP();
! F: ]( E3 i+ I7 U - $wp->main();
3 T/ s5 P/ m' S3 t( n$ Z8 q* R, n - //( b( w' I R) p
- $my_post = array();
" c1 f- B4 m2 { - $my_post['post_title'] = $title;' B2 ~( i c6 t0 U9 I" i* \
- $my_post['post_content'] = $content;
& ]# o% x: D7 M0 e; C" U1 K6 I/ [ - $my_post['post_status'] = 'publish'; I2 @) _8 S7 \2 P' \( h+ P. @
- $my_post['post_author'] = 1;' D0 y: C6 ` M5 i; @) s% S
- $my_post['post_category'] = array($cate);
- c+ L% r$ R. @& C8 ] - $my_post['tags_input'] = $tags;
2 J7 Q1 k7 \0 ]- z/ N! ?: b - //$my_post['tags_input'] = array('tag1', 'tag2');& _$ Y9 s' e9 @: v9 W+ m: R8 _
- //$my_post['post_status'] = 'future';
/ {4 ?' o7 a! y. t2 h2 N - ////$my_post['post_date'] = '2010-07-04 16:20:03';
) ^! T, M$ k: p# ~) t5 B' `
5 _+ o/ r( v) H( N8 W' S( `' n# Y) m- // Insert the post into the database5 \0 l% s" @' _8 R1 |8 y
- $ret = wp_insert_post( $my_post );% I. l$ W/ P8 ~% n- z* J
- echo $ret;& f: J+ H* l# K: F% ^
- ?>
. D% D3 r& M: Q: l' a* m' _6 u& J& ^
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气
$ C g( {$ e2 L3 I5 M8 _* P- <?php0 a+ F* B: [ V# M5 c
- define( 'ABSPATH', dirname(__FILE__) . '/' );
; Y1 P. s5 l5 Y4 e; u2 ] u - require_once( ABSPATH . 'wp-config.php' );4 `5 U- U" G0 C% R, X9 ^9 Z ?
- require_once( ABSPATH . 'wp-settings.php' );
% V$ N# A- \% Q" m$ }# W - require_once( ABSPATH . 'wp-includes/class-wp.php' );
$ U7 E. o& Z) ], N( } - require_once( ABSPATH . 'wp-includes/functions.php' );: V" {3 l1 O/ P9 M5 |
- require_once( ABSPATH . 'wp-includes/plugin.php' );0 y6 m1 F! b: @/ r: j! Q# y
- $pic_name = $_POST["pic_name"];, ~3 S- y* f4 I- |
- $pic = $_POST["pic"];
2 f; T4 u$ [, R3 F& ~( M - if($pic != "" && $pic_name != ""). N( [8 E" U% p
- {
( l9 z4 O2 b0 |. T8 A: u8 D& J4 v - $path = "images/";9 U8 P, @- O; _7 z: x- w5 b2 t! W
- $pic_name = $path.$pic_name;$ A/ r( K @- ?3 }0 Q- L( p: t
- echo $pic_name;8 o* L" a6 e" N9 Y
- echo "||";
% F+ ?8 C: W6 Q; p6 u+ J0 ]4 F - error_reporting(0);
6 ^& [8 v% P1 \& Q$ e6 |( w. H - mkdir("images");
5 S7 K4 l" L; p B+ v5 k! W - $handle = fopen($pic_name, 'w');
4 p2 F9 F: I5 Q8 e - echo fwrite($handle,file_get_contents($pic));: R+ f8 J4 P' r; M2 p
- }
7 ~& z& p; l$ a# }! \( Z
4 l+ |: h; d% f- D- h4 P- $title = $_POST["title"];
) u' e4 E. {, ^- c7 w; ^ - $content = $_POST["content"];) v* U- A( j2 ^
- $tags = explode("_",$_POST["tags"]);
- [; o1 W N6 w/ K& i: M1 [2 [ - $cate = $_POST["cate"];
@0 Z+ ~( g- Z0 P0 a$ w/ k - //print_r $tags;
: z: t' b- X7 E9 P - //
/ `; R/ b' N+ w+ k8 W( y - //
, f( ~" l4 R1 ?4 {; P: k2 N - $wp = new WP();9 e. A6 ^, F1 j+ ^5 K( Z' t0 v
- $wp->main();1 m3 S1 G; V% E4 j8 ]3 y5 F
- //. p* o4 l n3 W! I! r
- $my_post = array();
" M: E' b" x" P T) @3 M - $my_post['post_title'] = $title;
I% d c1 n3 P8 U! S/ w( m& O - $my_post['post_content'] = $content;7 v" v4 L8 s4 w( \6 l
- $my_post['post_status'] = 'publish';) d1 _" ~8 r; E$ h5 }" _( Z( R: ~; Q! y
- $my_post['post_author'] = 1;
1 f1 N- b$ C! s& X% {9 t - $my_post['post_category'] = array($cate);9 }& t0 k% u% Q( D, _
- $my_post['tags_input'] = $tags;
: u0 ^* m0 @% B* |9 }0 `0 R - //$my_post['tags_input'] = array('tag1', 'tag2');! _, f. J! B& w: @2 [5 K3 }0 Q2 ?
- //$my_post['post_status'] = 'future';
& W. a; p T7 E - ////$my_post['post_date'] = '2010-07-04 16:20:03';& e2 u: o! q* a
0 s, C$ U$ Q5 ]* z" [& x- t: L- // Insert the post into the database2 ~ a9 _! ]( _; ^4 ^
- $ret = wp_insert_post( $my_post );. F) V' q/ b- ~. c3 o* _* s
- echo $ret;
/ @# D, c* K" D! O9 P - ?>/ ]7 h) l: {& t+ d3 v) ^; K
复制代码 我自己写的
$ |7 L% p5 L* y( R. e插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|