|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑
7 h' p- J, g# h. u, _) t$ f
' q3 Z e N, B/ p直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持
" T6 i' o! [) j) f U9 P" k6 z& V& s0 N6 O. R% |
我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了
6 U$ B4 Y' z4 k8 q6 [2 j) U
8 y" R: x9 M, |6 u: _3 t- V2 u方便实用 简单 何乐而不用呢
. M4 T# K6 E2 t, E2 C: f2 S( A- <?php
3 X9 o: q, ]/ P' T
( [( m6 s& K' l+ l( } g% H; u- define( 'ABSPATH', dirname(__FILE__) . '/' );
( I' y7 w4 e. A" j7 R - require_once( ABSPATH . 'wp-config.php' );
4 X4 b/ O0 a1 O' K& z - require_once( ABSPATH . 'wp-settings.php' );5 _5 i9 Q) \, B7 P5 \3 n
- require_once( ABSPATH . 'wp-includes/class-wp.php' );
6 ~7 n4 n- V" H. c - require_once( ABSPATH . 'wp-includes/functions.php' );& f# B& t* f! w* j! k7 f
- require_once( ABSPATH . 'wp-includes/plugin.php' );) \8 D# J0 S) X( x5 m
- $title = $_POST["title"];9 [8 W+ T! [0 E. H% p2 b
- $content = $_POST["content"];
2 f8 t2 S' n& n# S2 ` - $tags = explode("_",$_POST["tags"]);! V' t! D/ ]0 C, d6 M+ B8 a$ M4 g
- $cate = $_POST["cate"];0 Z2 U) S* q) J* C+ D( e5 s
- //print_r $tags;7 o. d; W% i$ T' r: p/ v* q$ f
- //& k0 w1 ^4 q V" _5 r6 x2 F% s
- //
2 A: s! r Z' v* e0 B1 I$ v3 ]' c - $wp = new WP();
. K9 g6 t1 `* c - $wp->main();* T6 T( L! @0 K7 D/ A
- //
4 s) g6 F a7 M9 z, a s( l - $my_post = array();
$ [7 |/ f) w* W3 g. R, R _! ~0 `& t - $my_post['post_title'] = $title;
) f; n7 _* l. H$ j - $my_post['post_content'] = $content;
$ j+ b# ^! o4 B, Z: e - $my_post['post_status'] = 'publish';" O0 M/ J" [2 `# f* v" Z8 U
- $my_post['post_author'] = 1;
4 Q% A1 Y4 J% U& h$ w" N - $my_post['post_category'] = array($cate);
) \8 n$ `; }3 u ~# X - $my_post['tags_input'] = $tags;) {: u; O/ |2 g4 Y7 h
- //$my_post['tags_input'] = array('tag1', 'tag2');1 y: c8 ?5 U, ~* P
- //$my_post['post_status'] = 'future';
: x# V) W; U1 r# X' L - ////$my_post['post_date'] = '2010-07-04 16:20:03';
' E% B6 w" ^/ Q/ l4 q0 N
) D" x8 M/ _! V4 c5 R+ n5 D& {- // Insert the post into the database
. B* K' M& R: @8 w - $ret = wp_insert_post( $my_post );7 y& g {" C/ N w
- echo $ret;
# _2 S" l9 ~$ D8 L7 b8 A - ?>
4 [+ B* ~. \/ `2 j5 x4 g
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气
8 f; {! ?6 V* F! r7 _- <?php
8 P( d. q6 R+ o: s4 k - define( 'ABSPATH', dirname(__FILE__) . '/' );
! I$ M3 m/ w. L9 K, R6 _ - require_once( ABSPATH . 'wp-config.php' );& a( f4 F' P0 r W: F
- require_once( ABSPATH . 'wp-settings.php' );- \. R$ B* @, C9 d1 w! w7 Q- v
- require_once( ABSPATH . 'wp-includes/class-wp.php' );; I1 R3 g- W& H" b/ `6 S) F
- require_once( ABSPATH . 'wp-includes/functions.php' );, ^ \1 U9 [9 M% E7 R
- require_once( ABSPATH . 'wp-includes/plugin.php' );! J1 m2 f/ I9 J9 u. O5 e. ^
- $pic_name = $_POST["pic_name"];; b& P% _- A3 b( W
- $pic = $_POST["pic"];
+ w6 D3 \! n+ [! y9 H$ a - if($pic != "" && $pic_name != "")2 }/ R7 ]0 q- }2 a8 Y; E7 A- w
- {' Z$ B& ^, Q2 @0 |4 ?8 G4 _
- $path = "images/";+ M0 w- @" x) m5 T% l' v+ \+ H- [
- $pic_name = $path.$pic_name;
' d, D" W# L7 j - echo $pic_name;
1 ?) q# ` v4 w0 R' N h - echo "||";
# g4 G, m! L" _" M7 l - error_reporting(0);1 m0 U4 k. e: d2 h; A% x
- mkdir("images");
/ g+ ^8 J @8 V" d - $handle = fopen($pic_name, 'w');
5 W6 k/ p; {+ |2 L/ Y0 p q - echo fwrite($handle,file_get_contents($pic));
, ~, L0 h( h* Y3 Z* S" D - }
4 G3 }' u% d. d. W9 T$ i" H) H2 g+ h
% c5 E/ X3 ?( e1 ]6 y+ C- $title = $_POST["title"];( T7 T, G0 k; k6 r% p
- $content = $_POST["content"];8 `; E6 ~8 e" L5 u1 m7 k3 Y9 Z0 B
- $tags = explode("_",$_POST["tags"]);
' g1 ?7 g0 S; M! [; z, Q. M - $cate = $_POST["cate"];1 z0 I1 v+ j) @! i
- //print_r $tags;6 Z6 j+ [4 r5 h [
- //* O6 C( s+ h# e D
- //
" X2 d; q, N- i/ L - $wp = new WP();
* Q% h! C% {6 C" r# W - $wp->main();
6 f3 |/ V- _8 a - //- d$ ]* i, T2 z. H. G
- $my_post = array();
( h5 v! d) j' L8 A3 b - $my_post['post_title'] = $title;
- d7 \( {% Z4 p- w0 t7 t1 z( U - $my_post['post_content'] = $content;
3 [& C; C2 J- ?( f6 y - $my_post['post_status'] = 'publish';
- S9 Y6 d* L: `" [4 l$ o - $my_post['post_author'] = 1;
* |& ~0 w& t/ j" q+ i3 A% W7 d" o - $my_post['post_category'] = array($cate);
% I, ^ p0 F3 Q( l% U - $my_post['tags_input'] = $tags;" y+ h+ A+ ?* e6 r
- //$my_post['tags_input'] = array('tag1', 'tag2');
( j1 _9 i" S; U2 s2 _ - //$my_post['post_status'] = 'future';; V5 G( o* u( J. M- f
- ////$my_post['post_date'] = '2010-07-04 16:20:03';' _6 K" |3 o- I% i1 P( Z7 o
- ) o1 T5 S1 W* l0 e& d( h
- // Insert the post into the database; V# b# d( y. N$ y/ [
- $ret = wp_insert_post( $my_post );8 d' [* r! Y7 u2 G9 k
- echo $ret;
9 N8 @" K: M( k2 R; U - ?>: e2 U5 d+ }0 W+ d( N
复制代码 我自己写的% f r3 d7 y6 q! Z. G' M/ b
插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|