|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑 7 r+ C2 ~' }; _! ]
) {* P5 h0 L: S4 J5 G z& c* l& I直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持
/ R& \/ v; @/ |6 m/ h" F' _3 p8 M ~) m( L9 S6 n4 W
我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了9 j! N8 O$ I6 p
# Y+ H$ w/ X \. h' _3 ]( `
方便实用 简单 何乐而不用呢- / H" j5 _# l1 F: b
- <?php
0 w9 P* k5 L9 j, \, p% f. h
) U2 N* g8 s4 } @7 g- define( 'ABSPATH', dirname(__FILE__) . '/' );- l, D0 X: l' G" K# U( |4 C# O. e
- require_once( ABSPATH . 'wp-config.php' );8 E/ V- d$ }% H) l( v" \; f2 O
- require_once( ABSPATH . 'wp-settings.php' );
+ ~& f# D! O. f0 d [2 x9 c/ W( y. r - require_once( ABSPATH . 'wp-includes/class-wp.php' );/ r7 p+ J. _2 s% M3 B O
- require_once( ABSPATH . 'wp-includes/functions.php' );
& b7 \* y M) ~ - require_once( ABSPATH . 'wp-includes/plugin.php' );
4 k3 k& K& B' Q; W - $title = $_POST["title"];% G0 Q0 b$ ?6 G, d* x3 }
- $content = $_POST["content"];
4 j0 B5 J/ U# B3 p, E% M - $tags = explode("_",$_POST["tags"]);$ d: `. j2 J) f
- $cate = $_POST["cate"];
4 ]1 V4 |4 e B2 x - //print_r $tags;6 ^% Q/ }- I+ P. Y* v$ m
- //
# V) z( Y7 d/ i- _/ {+ _5 a - //
7 h. Z& M1 d8 T& y. X - $wp = new WP();
2 A) ?3 M, L+ v! W: `5 x! w - $wp->main();% l) S! k' F. T7 o( C( j
- //
& @* z/ U3 n4 t* J* I5 u4 B, l3 _ - $my_post = array();1 y) S+ X3 F! d/ r4 h6 B
- $my_post['post_title'] = $title;! `2 `2 b& v7 Q5 V
- $my_post['post_content'] = $content;
. h8 T4 a/ @; J+ v d" |) o- a - $my_post['post_status'] = 'publish';8 M5 c n& X4 x" y& G O, H
- $my_post['post_author'] = 1;
' p4 o( z) _. C) n5 a - $my_post['post_category'] = array($cate);: q: w1 D; s* p, F! K
- $my_post['tags_input'] = $tags;1 D+ O3 d& W( H8 d( p
- //$my_post['tags_input'] = array('tag1', 'tag2');* s: n4 X+ F2 E# s2 {$ L" }, d& Y
- //$my_post['post_status'] = 'future';2 C0 n' R3 v. s3 u8 d4 c1 I. A
- ////$my_post['post_date'] = '2010-07-04 16:20:03';
+ n6 q; ]4 p. u
! H0 }+ F" |0 J p$ H- // Insert the post into the database* G6 W3 K9 a, D& \& i0 |
- $ret = wp_insert_post( $my_post );
) ?! Z2 y* P) v3 K - echo $ret;6 m# v0 A: s; h. f7 Q/ r% H# F
- ?>
1 v1 c. u7 t8 w) p4 H& H @9 L
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气
5 s9 f5 n, `' I1 H- <?php3 q f; L& H% ]) ^3 H$ K
- define( 'ABSPATH', dirname(__FILE__) . '/' );
# Q. ?* n: ^1 |. F- W1 e* d - require_once( ABSPATH . 'wp-config.php' );; ?3 e, G+ W8 S5 {9 `
- require_once( ABSPATH . 'wp-settings.php' );) q6 f8 B& p5 a9 E3 F5 g7 h
- require_once( ABSPATH . 'wp-includes/class-wp.php' );) A8 V* a3 N G
- require_once( ABSPATH . 'wp-includes/functions.php' );
! T! v- V/ J8 o) O - require_once( ABSPATH . 'wp-includes/plugin.php' );5 z) r, X$ D- M& ?' J
- $pic_name = $_POST["pic_name"];* `9 E9 V0 s5 ~3 m
- $pic = $_POST["pic"];- I4 b0 L' b3 R* ]: \8 g" T
- if($pic != "" && $pic_name != "")
: l1 ]8 ?) s+ V# Q O# S& h. F2 | - {
3 `4 \" j! o% v. i: [4 Q - $path = "images/";7 a7 t; x) y$ A! {0 j6 }* z
- $pic_name = $path.$pic_name;* o( F& c$ {0 ~5 C4 t2 w- J& @: b( Z( D* u
- echo $pic_name;
! t3 c0 B5 ]2 S; Q - echo "||";
: }' y2 b! S ?3 [ - error_reporting(0);( l9 V4 ^, \+ H
- mkdir("images");
4 b; ?( {+ q+ d/ L+ o* O - $handle = fopen($pic_name, 'w');4 \! O, @6 c" c' \5 o3 U6 a
- echo fwrite($handle,file_get_contents($pic));
3 l! }$ _* r; M( O9 o# Q0 a( Q - }
. p* y( }, }6 W4 b& o' ]9 o6 X; ] - 8 R5 y7 n, a6 t
- $title = $_POST["title"];9 ?* V! }6 p& l9 k
- $content = $_POST["content"];2 [4 J d: ]6 [. D
- $tags = explode("_",$_POST["tags"]);
?; u- l) ?$ e: K; ^# @ - $cate = $_POST["cate"];/ N" `: X! d5 ?" g9 R
- //print_r $tags;: ^. `5 _4 I- i6 Q; V+ Q
- //5 f& A% n1 l( M; V0 w; A, [
- //
9 s ]( w( Z- X - $wp = new WP();) L o% K+ X% ~2 y2 y; M, P A6 u
- $wp->main();$ ?7 U! W& B* Q6 I
- //% b$ v% D$ N+ e7 K/ O! A# E8 r* x
- $my_post = array();
, }! u1 }8 l' c, Y4 z4 k* `. Z - $my_post['post_title'] = $title;
4 g( X0 y4 N9 U4 J; i9 d - $my_post['post_content'] = $content;
# B% \( B9 `3 `2 g; L& S% C - $my_post['post_status'] = 'publish';6 e: w" i# P3 V/ k$ T# f
- $my_post['post_author'] = 1;
+ `; C9 Y' k. N& B+ a' H - $my_post['post_category'] = array($cate);
# V2 \7 H+ @" r t+ i4 c - $my_post['tags_input'] = $tags;8 I% X, `, n8 B; \- B# Q, K
- //$my_post['tags_input'] = array('tag1', 'tag2');
& n2 r' l! R! S# ?! Q - //$my_post['post_status'] = 'future';: {( @" g: @" W0 }& a" L, W( E, v* g
- ////$my_post['post_date'] = '2010-07-04 16:20:03';
1 R5 c, }$ j: s: m# ?
" U& S) _5 w6 c- // Insert the post into the database
& E g7 w2 O+ u" [' j: w5 A - $ret = wp_insert_post( $my_post ); X. @3 M9 }9 n% b M! r
- echo $ret;6 S7 h4 n) p K# ^
- ?>
$ u) p. u$ M6 x' y
复制代码 我自己写的! O" O6 I; O& T' A- p
插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|