|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑 . K h1 ?3 U: a* R+ C
% Q, T4 U( c' p! F/ E: X0 E) w直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持' Y" c- q3 }* S- Z h2 d- z
5 W) t/ Z# T5 Z/ \3 Z' w
我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了; D; V4 i1 O' }2 U& J" }
& j9 }. b( K& P5 Z5 Y n* `: P
方便实用 简单 何乐而不用呢
/ u1 P7 E2 f2 C+ P! c* @- <?php
& Q9 S" C' J4 B- S1 b0 N |* _
. x7 _& W' O1 Z/ v# O- define( 'ABSPATH', dirname(__FILE__) . '/' );: h i _0 |. W' W& |$ H# j9 |
- require_once( ABSPATH . 'wp-config.php' );
, Z/ r [5 d8 h$ H6 S$ w - require_once( ABSPATH . 'wp-settings.php' );
& T0 C2 a: {) ^ - require_once( ABSPATH . 'wp-includes/class-wp.php' );4 [1 }% u0 h3 x9 w
- require_once( ABSPATH . 'wp-includes/functions.php' );
/ w# }5 N6 f* k: F - require_once( ABSPATH . 'wp-includes/plugin.php' );
. f. H# K# C: Q1 O - $title = $_POST["title"];3 J- k$ f7 o8 L" h( q) |" [/ G
- $content = $_POST["content"];
! `/ E$ c& M, J* Q. P5 m6 v - $tags = explode("_",$_POST["tags"]);
9 Z/ y2 A% \4 {& Y5 ]% ?5 t* R - $cate = $_POST["cate"];
$ P9 w, _0 B$ @/ `" \ H' P - //print_r $tags;
; G7 O- S" H7 v - //- c, S$ g. h L6 E1 K$ s5 }
- //: h# A, j+ b6 v% ?
- $wp = new WP();# r& \" C n& _5 [
- $wp->main();+ ]+ f( z+ L) |: [
- //
* f, ?6 d! [; `& F - $my_post = array();
- ]+ [4 }! ]" F9 ^% e5 L! I1 K$ G - $my_post['post_title'] = $title;0 K K% ~ C' o
- $my_post['post_content'] = $content;
$ l8 w9 y. m/ s& j - $my_post['post_status'] = 'publish';
: H8 n, t3 ~: c$ `" W+ n - $my_post['post_author'] = 1;# p+ H/ }0 L+ C. H0 E$ f
- $my_post['post_category'] = array($cate);
1 Q% k1 t, W( p' z5 E7 Y$ G - $my_post['tags_input'] = $tags;! R( F$ K0 s5 ]/ A! @# C9 z
- //$my_post['tags_input'] = array('tag1', 'tag2');
. v/ J2 \/ P! D, D# Z F4 H - //$my_post['post_status'] = 'future';' t7 z' q( ?1 y- `8 V S# n, V/ I
- ////$my_post['post_date'] = '2010-07-04 16:20:03';
N4 w8 P; l: ]% f6 r
3 r' C. R6 b1 _: r; A- // Insert the post into the database
& i% q {/ R' `9 d% O' {* v - $ret = wp_insert_post( $my_post );
3 N/ g2 F' z- `; m - echo $ret;
' c. g7 u4 J; X7 \; Y - ?>
6 W0 j) G. ]$ r' c4 y
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气- 3 z+ A+ A) D8 h& j& c+ h7 C
- <?php
1 w; A+ c0 O/ H9 j. O/ v4 h - define( 'ABSPATH', dirname(__FILE__) . '/' );
! m A' [! x% e: S( d - require_once( ABSPATH . 'wp-config.php' );- V9 h# c; Y6 w. ~7 o- N8 |0 o' c
- require_once( ABSPATH . 'wp-settings.php' ); u6 U& M! _+ y7 W
- require_once( ABSPATH . 'wp-includes/class-wp.php' );, w% p+ r! F9 m4 U8 G
- require_once( ABSPATH . 'wp-includes/functions.php' );
. `0 P0 _) q0 D W - require_once( ABSPATH . 'wp-includes/plugin.php' );' }+ e' d5 C- j3 f( F: v
- $pic_name = $_POST["pic_name"];
' m& b) s y5 `7 j$ a8 w5 X - $pic = $_POST["pic"];/ W- e) k3 l5 i% M
- if($pic != "" && $pic_name != "")
/ b/ L2 O8 T5 c$ o: d - {
! ~% Q5 N; R5 j" Q' P% q - $path = "images/";' w* |$ T. Y% t
- $pic_name = $path.$pic_name;; H) W! ^9 ^' @5 B
- echo $pic_name;) i$ p2 j5 k6 y- r# W0 L
- echo "||"; k8 n2 z6 `) b+ Z- c: e9 t
- error_reporting(0);/ H1 \5 d# ~5 `9 }' t
- mkdir("images");- [: ? Y/ e8 Q: t3 E& H
- $handle = fopen($pic_name, 'w');
3 h' P# l) C5 s - echo fwrite($handle,file_get_contents($pic));' Q. Z% j0 N0 z; R; o& {" t
- }
$ s8 ^6 m' L* _9 F5 P) N
9 |7 i9 M: q; g( n) H- $title = $_POST["title"];! i; _: P$ D0 l M& {6 j4 o( u
- $content = $_POST["content"];- w% @8 `6 X5 c5 q+ C
- $tags = explode("_",$_POST["tags"]);2 [4 t5 v. ~6 z7 `8 B, N& v
- $cate = $_POST["cate"];) X5 ^% o/ @+ a1 t3 B2 I% V+ w
- //print_r $tags;
* f# W% |0 b" ~; Z - //
1 }8 \! ] T1 j/ T6 Q6 C& C - //+ p; d# E; z6 _5 ~
- $wp = new WP();. n8 U9 f6 ~) z ^3 D; P# D7 L
- $wp->main();, l! R( L& V$ y1 Z& Q- H
- //6 `9 ]% S# N( G
- $my_post = array();" |% M% b- p5 b, [1 K6 G% c
- $my_post['post_title'] = $title;7 S/ j: B% v. o" i0 ]
- $my_post['post_content'] = $content;+ i7 q9 U$ H" ^
- $my_post['post_status'] = 'publish';
% ~1 D2 r' B9 V1 d+ s: r M3 \$ w6 @ - $my_post['post_author'] = 1;
; F$ V7 \% c& G) n+ @ - $my_post['post_category'] = array($cate);
* {& ^- e! d9 C3 i: ` - $my_post['tags_input'] = $tags;) |% O4 m! s/ t2 ^( m
- //$my_post['tags_input'] = array('tag1', 'tag2');
7 ~+ L/ d g: @+ c% L* W - //$my_post['post_status'] = 'future';
( s$ k2 N' `# K% k+ \ O+ r2 \ - ////$my_post['post_date'] = '2010-07-04 16:20:03';+ k8 U+ J/ ~$ L# r0 X
( M( i% ]" R0 G( v# n- // Insert the post into the database+ g, H2 ~" C% @
- $ret = wp_insert_post( $my_post );+ T! l) D6 j1 ~$ o, o% p, p5 ^
- echo $ret;8 U5 q `$ X! \7 |1 {1 v
- ?>4 \2 v8 T) O0 X0 o6 [+ x
复制代码 我自己写的7 }/ e- Y5 C& \! }+ V$ y2 \
插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|