|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑 0 a! s, Q2 y; \, M9 D, p
4 [7 H _& n3 H1 y* B6 _- W
直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持
* ]6 M) j+ E. p7 D9 }6 V2 s9 C4 F9 a! Z3 K
我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了& \4 v" J1 P8 }9 V
9 P. w6 [5 Z8 E Y! J方便实用 简单 何乐而不用呢
# ~4 V0 i) d5 Y; x6 R- <?php
$ _* F/ E+ h4 W6 b. z
% \. V# L) V& s' @6 N/ O: s5 ]- define( 'ABSPATH', dirname(__FILE__) . '/' );* V. l4 _" f2 l* y
- require_once( ABSPATH . 'wp-config.php' );& w ]; g/ ]% h- @5 n
- require_once( ABSPATH . 'wp-settings.php' );/ Q5 T, L- _9 h1 g' j
- require_once( ABSPATH . 'wp-includes/class-wp.php' );* m" y; b+ z+ p# v2 A) U
- require_once( ABSPATH . 'wp-includes/functions.php' );
1 A' u# K/ i; B: V! S - require_once( ABSPATH . 'wp-includes/plugin.php' );
2 {" E5 k" z# G, b& O - $title = $_POST["title"];
( M+ H! M4 D# i8 s. X' P$ _" S - $content = $_POST["content"];
% `# S- J" E- \. L - $tags = explode("_",$_POST["tags"]);& Z# H% B; `) L! ]/ ~. \
- $cate = $_POST["cate"];
9 f+ ]( N2 d8 A3 p - //print_r $tags;' `1 U a) ?% @
- //& T# _( j" r$ d2 `/ j5 L! ?% y
- //
! `; S2 `. t1 A( c - $wp = new WP();
& C8 ^! R: o3 i* {( u - $wp->main();
' f! X7 s3 |* X1 D0 Y+ q) O( ] - //4 u* y6 ~0 _' j' O* {
- $my_post = array();; k# x' u! z( F \2 p/ j
- $my_post['post_title'] = $title;
; J: v+ ~ S4 w4 ?: g y' H1 s - $my_post['post_content'] = $content;
$ T- z9 B3 ?9 W5 N' ^) S" @; f - $my_post['post_status'] = 'publish';) S6 V3 B' g R5 F" f; I5 f, i
- $my_post['post_author'] = 1;
( |' U8 C4 \! Q3 ? - $my_post['post_category'] = array($cate);9 H+ d- ?1 T* P. n9 y ^ l- }
- $my_post['tags_input'] = $tags;9 c: i5 U6 a, c
- //$my_post['tags_input'] = array('tag1', 'tag2');
% J6 u6 y4 e& B1 Z. v/ k - //$my_post['post_status'] = 'future';
2 G5 `% M; Z9 C& V9 z+ ^ - ////$my_post['post_date'] = '2010-07-04 16:20:03';
/ K5 I4 r) U$ H. N, X
9 ]" b# w' I. }4 n$ ?- // Insert the post into the database
9 i- v. \' I) {+ @0 M- I - $ret = wp_insert_post( $my_post );8 r3 G7 N. m1 o e7 ^. H
- echo $ret;
# r% [* G6 } }1 J; B - ?>: Q* P5 j5 w* p
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气
( n- F& E' C0 W8 a, j& \- <?php2 ?! x+ t8 K3 V+ q) r" L2 \: E
- define( 'ABSPATH', dirname(__FILE__) . '/' );
! d% ]% N# a, S C; T - require_once( ABSPATH . 'wp-config.php' );/ W' i7 J+ T+ X6 h N" ~% g
- require_once( ABSPATH . 'wp-settings.php' );
4 F6 j: W& G/ t0 a& |3 J0 {( z - require_once( ABSPATH . 'wp-includes/class-wp.php' );5 s$ j6 r, T; i8 J/ \5 J" t( m$ y
- require_once( ABSPATH . 'wp-includes/functions.php' );
" U6 v% `' [# D7 n3 N; u - require_once( ABSPATH . 'wp-includes/plugin.php' );
. o. R; Y& L+ s+ A - $pic_name = $_POST["pic_name"];* a7 i: e& e3 V! o' o$ \
- $pic = $_POST["pic"];
. K" p; [( c! ^! B+ G; L/ Q - if($pic != "" && $pic_name != "")/ c( Q) L b {! S! U4 e
- {
1 q# M( ~6 q( q7 ~7 k - $path = "images/";/ T) J+ ^) }* f
- $pic_name = $path.$pic_name;
0 O& i2 \; p6 t' x0 G# W( D$ A - echo $pic_name;9 y' n5 h# O! } K$ y$ _' |% p
- echo "||";
2 h& [$ [; c; T5 e - error_reporting(0);
" i7 e" [5 E% D2 x& q& ~ - mkdir("images");1 W2 l0 [/ @" S) m! _! Z1 \& g1 @3 @
- $handle = fopen($pic_name, 'w');/ C- s3 N% Y. [2 ]( |- k
- echo fwrite($handle,file_get_contents($pic));2 ]* B/ x+ m" y; {; N
- }
0 H' p; a* T& E2 B
$ [" i$ N* W! c) B1 y1 \, _: n- $title = $_POST["title"];3 Z& q* g& a: P+ A+ U" w7 X& k
- $content = $_POST["content"];
6 I/ x2 i$ r# |1 Z0 b- V* \/ H$ L - $tags = explode("_",$_POST["tags"]);
% o3 ]) F% j0 O% d2 k0 d5 L( F - $cate = $_POST["cate"];7 F0 H3 v" P2 i9 y$ M( `, _) W
- //print_r $tags;7 Z1 c, Y9 |/ m2 M, X
- //- A% H, W; \7 u) N7 x* f/ S
- //* A8 _7 w7 ^/ _, T
- $wp = new WP();
+ g5 E0 n3 e9 [- Z$ r" v* B) S - $wp->main();
8 i, `% i1 C! W- ?( B, G - //
. C; P0 s- t! d9 J6 B$ q, h4 E+ j - $my_post = array();# r6 H/ x6 F" y* v0 o) u
- $my_post['post_title'] = $title;
5 Q& |4 z$ s z' ]+ l' t - $my_post['post_content'] = $content;! Z6 S# a) ?& O1 a1 f6 N
- $my_post['post_status'] = 'publish';
, q4 |6 V) w, u1 ? K @* q# o - $my_post['post_author'] = 1;5 ~; L- w4 B/ n
- $my_post['post_category'] = array($cate);4 ?' a) L5 W$ j
- $my_post['tags_input'] = $tags;* K4 M0 W0 t% `7 k7 L7 \2 d
- //$my_post['tags_input'] = array('tag1', 'tag2');
# }' `/ z% x- d - //$my_post['post_status'] = 'future';
/ _" ^' I! s9 A8 M8 i3 m8 W - ////$my_post['post_date'] = '2010-07-04 16:20:03';
9 O& m: B# B8 o& ?6 v$ w) g
) N( ?( K, I0 p- `% s- // Insert the post into the database
: ~8 p: h: \* h( j/ t0 c - $ret = wp_insert_post( $my_post );; K9 s% T! g( T& X8 S
- echo $ret;' }& P* F l/ W( w6 e" Y' [5 a
- ?>, j# }4 a; d7 l
复制代码 我自己写的( S/ W/ P4 |* y5 \, o+ S
插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|