|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑
9 G/ _% u. [6 s1 f2 L+ S
# i8 f" @5 @/ P& H, x# Q9 d直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持
) U" t q. w4 m# x+ t6 r# I8 m% ?% @0 z8 `: Z0 C6 |5 d
我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了3 m% o9 H7 Z* e! [. p
5 V% c- c- b7 A. u3 U# ?方便实用 简单 何乐而不用呢
+ G. L" Q- L3 z0 @6 Y- <?php
9 [# i* z# v9 q1 c6 w" ^2 P
! \) t5 A Y0 X* q- define( 'ABSPATH', dirname(__FILE__) . '/' );
. d$ q' a5 S4 G1 d/ r - require_once( ABSPATH . 'wp-config.php' );8 k8 O+ Z6 ]) d5 T: A6 D1 W
- require_once( ABSPATH . 'wp-settings.php' );
+ C- ^( _ D7 w: {7 o% }8 n" J [ - require_once( ABSPATH . 'wp-includes/class-wp.php' );
/ Y: U9 Y! c8 W! |0 w& t. d2 k - require_once( ABSPATH . 'wp-includes/functions.php' );: D7 |) I L1 ]1 _
- require_once( ABSPATH . 'wp-includes/plugin.php' );- x+ K$ | T7 {/ L% p7 r
- $title = $_POST["title"];
8 ?+ J) s& E/ q/ k* c f - $content = $_POST["content"];
2 Z8 S0 Z, @& j0 I4 W# Z9 N - $tags = explode("_",$_POST["tags"]);
) X- A7 v! ^& U& L: y9 Q - $cate = $_POST["cate"];6 t3 p- r$ M- o8 s5 P' f6 @
- //print_r $tags;
9 A p, N) I! s - //+ J0 ]$ M9 j" s, M$ F' Q/ ]
- //
3 \# e' \! t; B, g5 L7 m - $wp = new WP();1 K+ ~; ?/ _: n) ^
- $wp->main();8 Y T# V: m8 F3 ~* k: e& `
- //
5 G5 n4 D2 u3 Q* i+ M9 i% y# c - $my_post = array();' J- {5 T: f/ B! y
- $my_post['post_title'] = $title;
3 i% d8 V* V7 w- ]/ [ - $my_post['post_content'] = $content;
/ L( L: S* N$ t. o- Y# x0 x, F - $my_post['post_status'] = 'publish';5 b& o. Z* V4 U9 _$ K0 v
- $my_post['post_author'] = 1;. M1 n" }. J9 o% Y. O8 N
- $my_post['post_category'] = array($cate);
$ }" E! n2 K7 ?" |; X: H. ~* |& r5 p - $my_post['tags_input'] = $tags;
9 L5 K9 N( H, O* t4 t E - //$my_post['tags_input'] = array('tag1', 'tag2');8 ?9 W( e- c. \6 b% Z
- //$my_post['post_status'] = 'future';
/ L# {; Q# r* N7 s0 y3 o+ ?0 E/ C: k - ////$my_post['post_date'] = '2010-07-04 16:20:03';6 R3 c: p9 d- {+ k
, Y) s* F c, D8 F: w# ^0 E" |' w- // Insert the post into the database
3 ?" N6 Y# P ?. L7 a: l3 P3 K - $ret = wp_insert_post( $my_post );$ ?! d6 E$ d n% O Q
- echo $ret;
( ~% p4 T. v/ D W7 ^ - ?>
) P) U1 t3 o6 @& `9 A) ]
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气- 3 }) }4 g3 L! U; K7 ]8 N
- <?php9 i& U- N% M& D( j1 L
- define( 'ABSPATH', dirname(__FILE__) . '/' );
+ h5 D1 }( ^8 w( d& t - require_once( ABSPATH . 'wp-config.php' );% }% k1 Q; t) |* f7 E
- require_once( ABSPATH . 'wp-settings.php' );5 a/ L6 |; _; x+ D, w3 l
- require_once( ABSPATH . 'wp-includes/class-wp.php' );
+ q9 Q; i$ {* Q9 l" ]/ u0 x - require_once( ABSPATH . 'wp-includes/functions.php' );
4 e f+ E: l- I! e; C, ^: o - require_once( ABSPATH . 'wp-includes/plugin.php' );
5 P5 ^8 t) D, Z$ e3 f; U - $pic_name = $_POST["pic_name"];& R1 Y8 I+ A \/ Q- P3 a% D- v0 h2 l
- $pic = $_POST["pic"];
7 E/ ]: K( x+ p - if($pic != "" && $pic_name != ""). g5 r1 `7 p5 u
- {
5 L& i; O( ]0 p - $path = "images/";
?: [8 c8 X3 C. x( P - $pic_name = $path.$pic_name;8 Z; u- b) ?2 a! M
- echo $pic_name;
8 L, y U1 t( K! R c8 z2 b - echo "||";
2 W$ l2 n4 W1 n- J- n/ f - error_reporting(0); b; {$ H0 L5 U+ L/ V
- mkdir("images");
# P+ z6 m# t9 g* T+ e4 X' R - $handle = fopen($pic_name, 'w');2 N' q: c9 K- s# f% ]7 U
- echo fwrite($handle,file_get_contents($pic));; ?1 h& N3 h1 X5 g4 i4 E4 W3 ^) C
- }
( z: k5 Y, w) p* U5 ?- H
* \' @# n! X' y& `, R6 G: w- $title = $_POST["title"];3 u) W; m7 c/ Z& z6 {
- $content = $_POST["content"];
( s5 W8 l$ a3 M" _, F. A. _. b1 s7 I - $tags = explode("_",$_POST["tags"]);# H% H$ F* R+ K# A( [ ^; R; m
- $cate = $_POST["cate"];
9 | ^7 r: e$ ?, N! T. w - //print_r $tags;# b( ~" d4 ~' n: ~; O& ~
- //
! z- t5 W9 f& z, P9 U - //
3 c6 y" t5 j Q9 h6 B' z - $wp = new WP();
3 P3 T8 t" {& Q$ C6 ?: m3 i - $wp->main();
, H/ Y1 k5 O4 g9 o' f - //# r* W* l6 U; ], |) t
- $my_post = array();
! D5 k8 Z8 J4 D. o( A N - $my_post['post_title'] = $title;# u/ B- G k) A& d* y/ f
- $my_post['post_content'] = $content;7 l8 n6 K8 ]1 p1 z
- $my_post['post_status'] = 'publish';
3 |% O; n1 O( w" n' ]2 b+ W - $my_post['post_author'] = 1;
7 Q4 s! s3 x' S+ f* q0 J0 \ - $my_post['post_category'] = array($cate);( Z/ n$ N- u' l( Z, }
- $my_post['tags_input'] = $tags;
4 F @/ K. Z h" ~ - //$my_post['tags_input'] = array('tag1', 'tag2');- E. B {! Y# R' H
- //$my_post['post_status'] = 'future';" j( I) y3 A2 }; j( ?
- ////$my_post['post_date'] = '2010-07-04 16:20:03';
: q5 b, O8 m& k6 Z3 x& H( W& i
; x, Q$ {( ^" d1 j$ T- // Insert the post into the database
* w, f* N8 }( b) t+ o - $ret = wp_insert_post( $my_post );8 r- J5 T8 X$ L/ ]; _# c5 e1 q& l
- echo $ret;
# F% D" m# m, E6 ]! e9 N - ?>: d1 b4 ^5 c- S3 Y5 Q
复制代码 我自己写的
- _' Q. T# X! C8 e6 k7 _插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|