|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑 ! f+ {1 {. [; C9 [
) S$ A% I# Y9 R, G/ ]
直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持 R+ L2 p" p; X+ u8 e' S, N5 a0 [
$ p% H) s9 E( G. Q9 W6 `我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了
' X% ?7 a7 y/ I
, M' W+ Y% @% g$ u( u- m$ |方便实用 简单 何乐而不用呢
# R& r" @: X2 U8 r* j$ Q% w9 F9 l- <?php
w) A& a, i8 m, j* e - , ? _& ]% P; A+ Z
- define( 'ABSPATH', dirname(__FILE__) . '/' );
$ U& B5 u( h- P2 a# s# ] - require_once( ABSPATH . 'wp-config.php' );
# r: h( W+ g, c$ k: A - require_once( ABSPATH . 'wp-settings.php' );1 D$ G! Z/ J( d y% ~! Y
- require_once( ABSPATH . 'wp-includes/class-wp.php' );5 e4 C% Y+ H! Q
- require_once( ABSPATH . 'wp-includes/functions.php' );
. w8 A$ K! t( d+ m5 ~+ F - require_once( ABSPATH . 'wp-includes/plugin.php' );
8 \3 O* y' y6 `, {3 Q0 f - $title = $_POST["title"];
9 Z; m3 [( _- B( M; ] - $content = $_POST["content"];
! `4 r! e$ I$ l. U# p' c$ [$ ~ - $tags = explode("_",$_POST["tags"]);! [: s! [; {* q! C1 R2 B: p
- $cate = $_POST["cate"];0 H( Z! P, x6 U9 z( T
- //print_r $tags;, r; T! b- l, I1 l( K9 P
- //4 d1 d' |# d& Z$ q7 U6 l
- //4 J1 t0 P- Y ?' K6 }/ U
- $wp = new WP();
9 y1 q6 {8 V( X+ G* x2 G/ h" M - $wp->main();
; P( ~) ?1 g8 N e- w7 h - //
" y1 Y) A. z/ W9 v3 B6 T- C - $my_post = array();
7 ^* b2 b& K5 e! w3 s. W5 j - $my_post['post_title'] = $title;; a. j* [" \" J- k
- $my_post['post_content'] = $content;7 P1 i9 P$ [+ I$ a7 n9 z
- $my_post['post_status'] = 'publish';
/ u: }3 C# l; [5 _ - $my_post['post_author'] = 1;
3 A) b2 u' |* V, M - $my_post['post_category'] = array($cate);
" Q; Y! \8 u: q - $my_post['tags_input'] = $tags;& t3 K- E1 {9 _5 l3 ]
- //$my_post['tags_input'] = array('tag1', 'tag2');+ T- w2 ~% u% _( v
- //$my_post['post_status'] = 'future';5 ^& T3 S2 I& o* k9 J
- ////$my_post['post_date'] = '2010-07-04 16:20:03';
& {4 {, M, p1 h( J - 5 H. S; C( m/ m. K( a# A' a1 K
- // Insert the post into the database" g; b3 K. A/ O$ m) A/ s" R I
- $ret = wp_insert_post( $my_post );
- J9 @5 K! |) |4 O' c/ t% X - echo $ret;
. w1 l9 Q: z6 K - ?>
/ B& M/ d3 `# \+ i @: G
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气- # D- I2 [; X f0 `; Y( a* J
- <?php2 [& Q* G5 c! @, d* y* N( U
- define( 'ABSPATH', dirname(__FILE__) . '/' );
$ p" c* Z. L$ A1 }, ?/ ` - require_once( ABSPATH . 'wp-config.php' );
" {0 A( P9 r# v4 X6 r2 T - require_once( ABSPATH . 'wp-settings.php' );
) T: [; K' W5 Z& ? E- n - require_once( ABSPATH . 'wp-includes/class-wp.php' );
- g5 _) k# W+ c7 n4 a1 d - require_once( ABSPATH . 'wp-includes/functions.php' );: v2 y( Q' j2 g4 ]: H% b
- require_once( ABSPATH . 'wp-includes/plugin.php' );) S/ u& c# y& }% c" F' p
- $pic_name = $_POST["pic_name"];
, `. i7 ?1 |/ ]& G - $pic = $_POST["pic"];; u- e- R4 U! x" n
- if($pic != "" && $pic_name != "")$ ^( W5 k/ E% k& D9 X/ t
- {
1 e) q" w- f: t+ i' N, c - $path = "images/";
! C& r9 W: D9 s - $pic_name = $path.$pic_name;
- b5 Z' J4 _7 b6 w) v' i0 Y - echo $pic_name;5 Q" }: z) q1 E: y
- echo "||";+ E4 ^# ]) x8 I( {( M
- error_reporting(0);
# S4 {* T7 _6 R* w& `8 g& G, n - mkdir("images");! H& ~% F D* x& v
- $handle = fopen($pic_name, 'w');6 s) L* C, |( f- f9 O& w
- echo fwrite($handle,file_get_contents($pic));0 e# A) [9 U6 y' I2 q
- }
$ ?( a, @6 @ U
( m, m$ m; x' K/ O- $title = $_POST["title"];
& {; p3 a5 N8 w/ T9 F - $content = $_POST["content"];; y6 O1 j# c% \1 A9 W. x$ x
- $tags = explode("_",$_POST["tags"]);
% c# Y( R" t! B - $cate = $_POST["cate"];! u# t$ D) J% D3 X1 }
- //print_r $tags;! q& w' M/ C& D0 ~; \
- //
. \5 m' G% J7 T: l8 Q - //3 ?/ _6 {0 I" }) E( Q
- $wp = new WP();9 _* Z3 L& v% ~4 i: ~* @1 H
- $wp->main();4 q t- \" ]" w
- //
) C$ V Y, L g5 q. e - $my_post = array();# b& s# F p& l! J4 d1 |; l
- $my_post['post_title'] = $title;: |9 e) b% Y% F! [
- $my_post['post_content'] = $content;& Z5 ?8 r# G& Q* Z+ \; u' S
- $my_post['post_status'] = 'publish';
2 G4 H% V5 l$ C1 p Y$ V - $my_post['post_author'] = 1;
. p4 m+ p& |9 @/ ]0 v - $my_post['post_category'] = array($cate);1 G( O' M: n# W2 N! A) u# \
- $my_post['tags_input'] = $tags;
! b d+ w* ?. z3 c/ [ ]+ c9 a! U - //$my_post['tags_input'] = array('tag1', 'tag2');
5 V! q G/ ?9 n5 Q* q7 s* u5 r" A - //$my_post['post_status'] = 'future';
7 `6 Y( a+ u4 T- g5 Z7 D4 | - ////$my_post['post_date'] = '2010-07-04 16:20:03';
# v; m# q, @) Z - % {. T" G$ W; ^" J- l) S1 A- O
- // Insert the post into the database" _0 e, U+ d! k. {, o
- $ret = wp_insert_post( $my_post );$ y: R& K, W& ?* Y* O9 I
- echo $ret;2 n' W0 a9 L e y3 S- p6 y, y
- ?>7 O) X; w3 s* o) l
复制代码 我自己写的4 r$ h% z/ u. i: f& I% R( `8 i) @
插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|