|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑
# f) L0 g1 g2 o7 C
! A$ N* S* G% {& P1 P/ Y6 M直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持+ y O9 Y; _- `6 L& A5 ]
% f9 g7 x/ c: n
我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了2 }+ [5 |& h4 x6 L
+ V6 k7 K p# Q* ^2 }" D
方便实用 简单 何乐而不用呢
e$ {% A/ r) A: F- <?php1 Y. w" t* I1 `
# j$ E3 e# w2 J7 {- define( 'ABSPATH', dirname(__FILE__) . '/' );8 P$ m4 j$ |+ F5 x# Z3 O6 x: B, s
- require_once( ABSPATH . 'wp-config.php' ); g' d2 V" h3 ~9 L# _2 z, g2 w
- require_once( ABSPATH . 'wp-settings.php' );9 s E) Q! B- I/ d9 D) E& c6 \! k
- require_once( ABSPATH . 'wp-includes/class-wp.php' );
" q5 o3 R, h' _) J6 {9 {$ Q+ n - require_once( ABSPATH . 'wp-includes/functions.php' );/ x3 x/ T9 y7 f* K& \7 A2 R
- require_once( ABSPATH . 'wp-includes/plugin.php' );, x& E3 }# p! G- S, L, Q, I4 H" ?3 W
- $title = $_POST["title"];) s5 i }, f! ^7 S& _. g
- $content = $_POST["content"];5 A1 v8 u; S* p; l( }4 l) U
- $tags = explode("_",$_POST["tags"]);
4 Y0 z, x% R/ d - $cate = $_POST["cate"];* D; ?( [% n1 s1 l8 h L" C
- //print_r $tags;2 e) R6 S& ~. h; y
- //
$ f% k' Y+ n7 _ - //% i" b9 v9 X! A/ a5 x* y/ w5 T
- $wp = new WP();4 O4 i( D7 R8 w: I' Y' M
- $wp->main();
/ M, F0 [1 A# b: K - //
4 p @; |+ z1 e2 S7 v: p }' \ - $my_post = array();
3 H. j: v! E6 }% M0 ~9 M - $my_post['post_title'] = $title;) M! x! G- i$ n# b: q
- $my_post['post_content'] = $content;
% h! S! U6 L6 Z' s' c2 M4 R - $my_post['post_status'] = 'publish';' R4 P! J( T; v1 `' @1 |
- $my_post['post_author'] = 1;) G3 }. U) S) R( Q' ~
- $my_post['post_category'] = array($cate);3 M6 u) q5 M) ^5 z; [( ~
- $my_post['tags_input'] = $tags;
8 j0 T9 Z1 u. i4 R8 x6 n - //$my_post['tags_input'] = array('tag1', 'tag2');
' ]- c' b) l& p* _; s. ]* D8 _0 \ - //$my_post['post_status'] = 'future';
" f" w. s8 V+ W4 h% P9 x: J( u - ////$my_post['post_date'] = '2010-07-04 16:20:03';. h1 e" E" r' W. P% V
- ( F3 g+ m8 p4 }' s4 S
- // Insert the post into the database9 I1 B- F( _: d0 Y8 _0 Z$ u7 {7 S
- $ret = wp_insert_post( $my_post );
" m. G& u( V1 G6 s- \* } - echo $ret;
% n* G {) P( z) X7 | - ?>
0 u% K, g: H# d P- G8 m' Y
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气
8 S: ?" S; ? q ~: _) F a- <?php
0 N1 C+ `: W \% Z: m) I - define( 'ABSPATH', dirname(__FILE__) . '/' );+ }: K6 ? T( f4 B3 R2 ]
- require_once( ABSPATH . 'wp-config.php' );0 ~3 g& E# L7 a$ @ u
- require_once( ABSPATH . 'wp-settings.php' );
, @" P' U7 v6 \0 T% S - require_once( ABSPATH . 'wp-includes/class-wp.php' );
4 u+ m6 H' r. T4 _% k - require_once( ABSPATH . 'wp-includes/functions.php' );; Y& s! ^1 ~& q: n! Q% X
- require_once( ABSPATH . 'wp-includes/plugin.php' );
M8 B! C8 U8 l6 h+ o - $pic_name = $_POST["pic_name"];& |" d6 M2 g7 ]! `
- $pic = $_POST["pic"];
& K+ j$ c& |/ }, z* ] - if($pic != "" && $pic_name != "")- N+ U+ P" |) G) b2 I2 v. C, X
- {
0 u5 p5 k$ e4 ~. |5 { - $path = "images/";
6 a l0 Y7 H2 q$ j2 ? t - $pic_name = $path.$pic_name;; S r, z+ i5 z
- echo $pic_name;9 `( v' X$ o A0 V0 I" [* {
- echo "||";
/ ^6 O1 M( Z& G+ e8 e* R, Y1 ~ - error_reporting(0);
9 w' W4 `8 k1 f' ^* S/ w* V - mkdir("images");: {" k9 u) V. t* [' l
- $handle = fopen($pic_name, 'w');
: k2 R( f7 e7 e4 W - echo fwrite($handle,file_get_contents($pic));
, O* ~4 g9 D) {( [& F# k - }
3 l' c8 N6 |1 [; x( v X- J
$ ^& j; W# ~/ Z$ ]+ J# ^- $title = $_POST["title"];/ L# V. R$ o! O
- $content = $_POST["content"];
" c+ d- U9 q2 \# a1 U - $tags = explode("_",$_POST["tags"]);
/ W7 _3 q1 \% p; F# D/ B - $cate = $_POST["cate"];/ A; X! P9 P+ m4 [8 B
- //print_r $tags;
0 C& G' p: O" P _8 I - //6 u) @1 |9 U* _) n8 x+ ?, @6 j3 {
- //, l$ b0 N5 C7 Y+ g8 U O
- $wp = new WP();
) \/ N! Z% U5 K" J - $wp->main();
- u- v# H6 |. Z, E - //
% @& E8 V- S3 [9 R - $my_post = array();
4 r0 l$ ^% b! Q4 k! F - $my_post['post_title'] = $title;1 z* ^5 U2 @( I ?% l- O
- $my_post['post_content'] = $content;
1 U: f( ^/ V: }+ L6 q) b; n# M( M) i - $my_post['post_status'] = 'publish';2 S, p; A/ S' q7 R& }( L( X
- $my_post['post_author'] = 1;: x1 ^( ]& l% q" X; A
- $my_post['post_category'] = array($cate);
6 k. P, s) Q* @) z - $my_post['tags_input'] = $tags;
/ D2 q" ]4 C; ~& m: Q - //$my_post['tags_input'] = array('tag1', 'tag2');- o3 V; D8 t/ R" T
- //$my_post['post_status'] = 'future';
/ ` m5 s! q0 w1 E; L$ p - ////$my_post['post_date'] = '2010-07-04 16:20:03';
( O' z) v6 ]4 m, |+ B" \% f' | - 1 I, _* y( k, r5 b, u+ L" z! i6 T
- // Insert the post into the database
9 ~" w3 i( H: F, `0 z) b% S# \" [; F - $ret = wp_insert_post( $my_post );9 a3 M* p, p7 T z
- echo $ret;
) c! A( P8 ^0 m, E - ?>; }8 U$ n$ A, V5 t( S. C' H# N
复制代码 我自己写的; D/ h4 U( P& E. h; ~
插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|