|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑 z( m" Y! U9 |- e/ @6 h8 _ n) s
; {2 d, L# v7 A" V
直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持* e. H& r; K- x
# R' G# @) o8 h* e
我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了
% V. X# N( Y" Y. u
_$ L1 ` \: R# X方便实用 简单 何乐而不用呢- 1 X5 v/ r8 R& a t+ ]2 |- ]0 H
- <?php6 k% t* @- x6 P% A, K0 j
; s6 e" h# `4 @# M9 b$ V- P- define( 'ABSPATH', dirname(__FILE__) . '/' ); M' I& c/ \, w: ~
- require_once( ABSPATH . 'wp-config.php' );
6 J2 _$ V" t* {( c: [: i - require_once( ABSPATH . 'wp-settings.php' );
2 q! Y& h8 D: w6 q: _' W - require_once( ABSPATH . 'wp-includes/class-wp.php' );; j% P& ^+ M, M- }& r7 S( y
- require_once( ABSPATH . 'wp-includes/functions.php' );5 E! i3 t) C3 }/ z
- require_once( ABSPATH . 'wp-includes/plugin.php' );
$ f o# ?6 }' q. K0 n: | - $title = $_POST["title"];
& d6 V% L) D0 C1 o - $content = $_POST["content"];1 u( A8 a1 q$ g8 r
- $tags = explode("_",$_POST["tags"]);
5 o# _7 M5 M1 {; ~4 ` - $cate = $_POST["cate"];" @8 I8 O% |& T0 S6 d7 C9 r
- //print_r $tags;% i) e+ D3 o* Q. W+ G
- //& {+ q2 U. g+ l" F4 Y
- //
* A' [" J$ x! ~9 \& C* f1 v" A2 H - $wp = new WP();5 |2 a6 x- N$ ~4 S
- $wp->main();
" I, \3 `- M% z( M- L" a/ m - //
) V7 h- s8 k8 E% y0 R+ A, ^1 j - $my_post = array();
) r, K, b1 g- U- h9 U$ \( } - $my_post['post_title'] = $title;% G& p' Y# v/ s. z5 G0 H9 G7 ^
- $my_post['post_content'] = $content;: Y3 \. y+ N" Z) l: f+ }9 h; O
- $my_post['post_status'] = 'publish';( F) w% Z% M: T
- $my_post['post_author'] = 1;
, t+ G/ i2 y" U - $my_post['post_category'] = array($cate);( T* @ f8 C6 D# j6 C
- $my_post['tags_input'] = $tags;
! j4 I! P6 N7 K - //$my_post['tags_input'] = array('tag1', 'tag2');! D) R) M$ t# {! N' z9 e' P1 ], h
- //$my_post['post_status'] = 'future';) r6 P, I1 v5 o/ w* f; ?4 c: G# t$ L
- ////$my_post['post_date'] = '2010-07-04 16:20:03';9 Z. B0 o. N& k3 [1 m S; C, h* q+ z% a
8 a- F$ g7 \0 f' E* w- // Insert the post into the database
4 e* o) J1 ^! }- {3 i - $ret = wp_insert_post( $my_post );
( T4 E% r# j( [) j) k! [8 X: K - echo $ret;, p/ b% C* E6 H# L. ]) X
- ?>$ X; B# t2 ]2 q% ?/ C! c
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气- 3 }, ^- e" z* X, ]' L
- <?php9 s( E. Y- m, F
- define( 'ABSPATH', dirname(__FILE__) . '/' );. x8 A3 `" f' a( Q; H6 H
- require_once( ABSPATH . 'wp-config.php' );" G7 x& n2 x5 G/ {. A Q/ h6 t
- require_once( ABSPATH . 'wp-settings.php' );: f. B( d8 M0 O
- require_once( ABSPATH . 'wp-includes/class-wp.php' );
! ?! [7 I" V, e! R5 ` - require_once( ABSPATH . 'wp-includes/functions.php' );0 T+ M" [# |) X6 K1 Y, I8 m9 H9 z
- require_once( ABSPATH . 'wp-includes/plugin.php' );8 G' Z$ [9 A3 X' O6 _
- $pic_name = $_POST["pic_name"];
. l0 y. Z8 T& E ~# @ - $pic = $_POST["pic"];
! n4 U: \/ i+ | - if($pic != "" && $pic_name != "")9 X! y: @% q( e- G3 S0 q& X$ o
- {
2 K3 K/ v6 [7 V7 z; z9 ] - $path = "images/";+ w* F0 Q) K4 _2 N2 T; U: r
- $pic_name = $path.$pic_name;
, u( c; z S: w* j. n' { - echo $pic_name;
3 c1 W2 W- t. c H; }- H( j - echo "||";
N8 }, u8 l5 m& V" G - error_reporting(0);
6 e+ ]1 r. S/ h6 `. C0 u# c( A1 L - mkdir("images");$ q* X, l% G" u6 @; m
- $handle = fopen($pic_name, 'w');4 \- S" E, x ^2 E+ z
- echo fwrite($handle,file_get_contents($pic));
$ h. R3 f# s1 W* J: u; F4 p - }
A" o$ z4 c1 k5 u( F - 3 s1 P. m; f3 E0 R0 `
- $title = $_POST["title"];
6 B$ N; s* N- x - $content = $_POST["content"];! a. J* S& X" j! i, \& w
- $tags = explode("_",$_POST["tags"]); x M" ?: |- W$ a' |/ Z# V4 M
- $cate = $_POST["cate"];& g- H; Z$ A/ F* P. J
- //print_r $tags;
, V; n% A Y/ F - //
* H9 c* n4 P! [5 h1 W: D6 N - //
. i% \# P5 K* y# m0 e" ^& D- ] - $wp = new WP();
5 O0 q& d; H H* x: r: Y9 Z- v* T1 S - $wp->main();! Z& @+ o% v# D. h, c0 c* y E
- //7 s0 L$ T1 N: ?% k$ U, S6 Y5 Z& }
- $my_post = array();
1 y: j# K( A3 s) ]- T8 w& O - $my_post['post_title'] = $title;, C% _. J d0 `$ Z- ?+ t
- $my_post['post_content'] = $content;/ _7 h7 N& R' Z: b( z
- $my_post['post_status'] = 'publish';
0 i/ K. H0 V7 e* w - $my_post['post_author'] = 1;+ O8 X, r+ H1 ^
- $my_post['post_category'] = array($cate);
% H/ W( z: y6 ~! j - $my_post['tags_input'] = $tags;1 r/ F. Y4 |1 S$ w& h
- //$my_post['tags_input'] = array('tag1', 'tag2');
' y, n8 Z6 c, J, H. A - //$my_post['post_status'] = 'future';7 t8 i4 B2 U- U
- ////$my_post['post_date'] = '2010-07-04 16:20:03';
- Z! Q V/ Y N3 x0 n! d - ' V' L3 \- ` ^- ~1 b: c4 g( K; i
- // Insert the post into the database3 J ~7 G7 @0 A$ ^+ O; A+ F7 S
- $ret = wp_insert_post( $my_post );6 u0 [9 x' B. g; W0 x3 `
- echo $ret;
, A! {* Y9 T5 z: V - ?>
+ p( y; o% A4 t# h* n$ A
复制代码 我自己写的! Q- X. \' v1 s9 `2 N" h. y- q
插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|