|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑
0 L' v6 x! }1 }! a0 Y2 n
5 o7 c( s6 Z! D" F7 L直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持4 Q1 Y) i4 M+ {. ~
6 v4 [0 o6 g0 c5 q我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了
3 u* R8 A0 f) n! |5 j
. t! f- _6 \0 S8 [( x$ A方便实用 简单 何乐而不用呢
6 W( I- r u T F/ H- <?php
& S! V$ M; h: j1 f! ~: k7 Q - 9 S* ]% o: B! p$ J. P0 Y/ I, f
- define( 'ABSPATH', dirname(__FILE__) . '/' );
# O4 b- _1 T" j9 W' b1 _# L9 u - require_once( ABSPATH . 'wp-config.php' );7 B/ t0 t/ Q0 t: g2 d9 n9 E2 n" K
- require_once( ABSPATH . 'wp-settings.php' );: y9 r$ B- p9 u, R- r! o& F: v) z
- require_once( ABSPATH . 'wp-includes/class-wp.php' );
9 n6 O. b( |7 d! ] h" f7 A7 R - require_once( ABSPATH . 'wp-includes/functions.php' );3 X2 c6 i( c. k5 O
- require_once( ABSPATH . 'wp-includes/plugin.php' );4 A% S2 S% q1 G9 m: v
- $title = $_POST["title"];3 L# u, r/ k8 Q1 l
- $content = $_POST["content"];
z$ Q6 Z* `- P+ P9 X - $tags = explode("_",$_POST["tags"]);
7 V* @: q9 L- x9 O& X1 X+ q - $cate = $_POST["cate"];2 ]- w9 T' H0 j& x$ g( I
- //print_r $tags;
' O2 C& l: E8 n/ ~1 G- P - //
; }3 z" ~' d* j- T& f8 d0 H+ T* Q - /// s g* g; c7 ?4 k& D
- $wp = new WP();& U& ?5 g8 N1 _, K) n
- $wp->main();
3 z g% z, ~8 e. s, E2 y, h - //- ^8 Q! p$ q1 f) X; |4 l7 L B0 i
- $my_post = array();' x' T2 F5 x; J+ R6 N& U2 K9 {
- $my_post['post_title'] = $title;
8 o2 H; C8 l$ K) s) I+ ~ - $my_post['post_content'] = $content;
' [& t9 @1 W6 _' ~% N# d6 M - $my_post['post_status'] = 'publish';7 j/ r8 y* g% J* u* K% Z# J/ M
- $my_post['post_author'] = 1;
! H6 S+ V( y8 L/ z. H2 B% ] - $my_post['post_category'] = array($cate);
, k" W/ ]# |/ f" c: e4 f4 T - $my_post['tags_input'] = $tags;
3 z, Z! O; |3 ?2 V4 L, D - //$my_post['tags_input'] = array('tag1', 'tag2');
( X2 `" m; i6 x! O, u( u, s - //$my_post['post_status'] = 'future';
) R; b5 W$ D- z- Z7 S$ ~: P% B - ////$my_post['post_date'] = '2010-07-04 16:20:03';
) s3 d% r0 @0 j8 n S - 8 k6 P) @- c( x j
- // Insert the post into the database6 Q) m% ~# [- @! f
- $ret = wp_insert_post( $my_post );, K- N5 X2 f$ w& Z8 h/ A
- echo $ret;
3 u: x _7 q# D; | - ?>/ A6 x7 J8 d' Q2 D
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气- " H, h- ] S; m% g: ^! g& y
- <?php5 s) b, }8 g* ]1 ^! I
- define( 'ABSPATH', dirname(__FILE__) . '/' );! O% u( V# r3 {& O0 n5 w% A
- require_once( ABSPATH . 'wp-config.php' );
7 A5 n- E9 M" z. f1 V - require_once( ABSPATH . 'wp-settings.php' );
' P3 U9 x3 [' L% M6 i; P0 W - require_once( ABSPATH . 'wp-includes/class-wp.php' );4 n& Q3 o" v; Q |8 B1 K
- require_once( ABSPATH . 'wp-includes/functions.php' );
3 k+ T9 s9 m9 K6 @" ? - require_once( ABSPATH . 'wp-includes/plugin.php' );+ H" A' l% D5 B5 A6 H
- $pic_name = $_POST["pic_name"];1 r! z: o% z2 q
- $pic = $_POST["pic"];+ P8 U) z/ _2 v7 d; P6 e3 ? [
- if($pic != "" && $pic_name != "")
% R5 o2 Z+ z/ Q) k0 H4 q - {3 F8 h7 N; C- x H
- $path = "images/";4 ?) M$ R+ U- F' O: X* M2 O
- $pic_name = $path.$pic_name;8 F+ @: ]) ~# U5 F
- echo $pic_name;" s6 N3 e& _" i
- echo "||";
) h& r ?$ q* R6 P& Y+ ^% T5 j - error_reporting(0);
+ H& C- t) p/ i0 v+ A2 h - mkdir("images");, `. H1 k! N& E+ R) e8 b) c* g
- $handle = fopen($pic_name, 'w');
+ t* G+ q% V; g: F, n/ \" X v - echo fwrite($handle,file_get_contents($pic));
0 E' }- c) U, y, Y) ]7 y0 ~0 R' ^ - }
" T6 n9 A) c5 W+ W Z# q: R! @
0 d, K( d) a, z, [3 ]/ E- $title = $_POST["title"];% y& w- g! t7 i K. W0 R
- $content = $_POST["content"];
' V/ j \ F X% K# ^$ l! f - $tags = explode("_",$_POST["tags"]);7 }1 u9 X+ u |) W6 h7 r
- $cate = $_POST["cate"];
8 y3 m: h3 ?: M - //print_r $tags;
! j* e9 M( z7 R' {! l8 y7 l ]3 ~ - //
$ W9 V2 o% A% g8 c0 h - //# U3 k3 ?* i4 c/ R- b
- $wp = new WP();
5 L6 C7 a3 {5 V8 m - $wp->main();1 P" g0 @. o8 R: L
- //
" h+ Q# _9 x G. O8 a- \ - $my_post = array();
: X8 C% e- i( @# M$ a: e1 Y - $my_post['post_title'] = $title;
2 K& `% e5 q% i. n- N6 D0 } - $my_post['post_content'] = $content;
! ^5 J- x; C% Q7 {8 e - $my_post['post_status'] = 'publish';' e2 a2 S; [0 F
- $my_post['post_author'] = 1;
+ B7 Q) q8 u& C8 Z6 [ - $my_post['post_category'] = array($cate);
* k. C& z1 r+ U& y - $my_post['tags_input'] = $tags;
x0 k9 f7 G" @ - //$my_post['tags_input'] = array('tag1', 'tag2');4 E! _' U9 Z( @9 E" K
- //$my_post['post_status'] = 'future';
' e6 Y4 H0 i1 s4 O7 i% G! d - ////$my_post['post_date'] = '2010-07-04 16:20:03';
8 l( i# N: M o$ P0 g0 P# Z' l
) y$ }. ?/ O3 @; Q9 i- // Insert the post into the database
- I3 ^9 P) c# f2 \2 c - $ret = wp_insert_post( $my_post );
0 B T' K7 ^7 i6 G - echo $ret;
6 P& x4 m, H& {" {' S - ?>9 o G! {/ M7 k8 r' c, G6 B
复制代码 我自己写的, {# B, ?9 L7 L5 i( w* [ S
插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|