|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑 : ]1 A$ Z1 D6 U- S" e$ r2 f+ Y) s
6 n" w& j8 ^8 U& i
直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持1 V C- b7 [ T1 F. R- X6 [: @
* b& ]/ w. l' M# W* u) P* z- ^* Z- ?
我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了
4 u7 ~' A1 P6 H! l/ o* a. g2 ~7 z& B; _
方便实用 简单 何乐而不用呢
3 J( m( g/ A' B5 M+ @+ w- <?php
1 s& W$ @% @9 P; X - % O8 r& n2 B: r
- define( 'ABSPATH', dirname(__FILE__) . '/' );, D% x4 n* D" n& U7 `' K7 Q% S2 ?& M
- require_once( ABSPATH . 'wp-config.php' );& h- F5 }, s* g q7 F2 s' u( d# j9 [
- require_once( ABSPATH . 'wp-settings.php' );9 l2 a( }9 ` u: Y; M$ ~1 m
- require_once( ABSPATH . 'wp-includes/class-wp.php' );
9 s+ `* o) D( j' N - require_once( ABSPATH . 'wp-includes/functions.php' );
0 ?$ M) A# F$ h( W, P7 M* H - require_once( ABSPATH . 'wp-includes/plugin.php' );
/ Q& W. _2 N/ ~ - $title = $_POST["title"];
8 h$ j+ }: D" j, Q - $content = $_POST["content"];
4 \% X; R9 C0 q; |. z- D. [ - $tags = explode("_",$_POST["tags"]);
" c2 u( _4 U+ u3 N* s) a ?# z - $cate = $_POST["cate"];1 |! \" ?! w) \" ~& F
- //print_r $tags;
* t8 H7 D6 y4 V7 w: f d4 z - //9 m1 \" q2 e2 \, c4 Q
- //
" B& }$ F: `3 r. g - $wp = new WP();
R8 d- v" w; h& e1 z N- w - $wp->main();- k: u7 M5 ?0 D; e3 ?% g4 L
- //
7 Y s) \) H' d - $my_post = array();/ Q- g4 `1 p3 ~- ? I( {& ]- g
- $my_post['post_title'] = $title;
4 P3 M% n5 _3 y& C - $my_post['post_content'] = $content;' r; ~1 f, Y* i+ C/ ]
- $my_post['post_status'] = 'publish';
9 s9 c8 V& o! |4 ~& u - $my_post['post_author'] = 1;
- j9 y: l# s; ^$ B - $my_post['post_category'] = array($cate);" n% m# T0 U1 o
- $my_post['tags_input'] = $tags;
$ C7 {; M% t5 f5 t$ ^! g, | - //$my_post['tags_input'] = array('tag1', 'tag2');! R( X! X+ q0 w
- //$my_post['post_status'] = 'future';
' d/ W, n) M# L0 c, L9 ]; P* {' U - ////$my_post['post_date'] = '2010-07-04 16:20:03';/ K- Y8 T% Y/ E: c- C" `% k
- 7 l: g4 g: |+ m$ z6 `
- // Insert the post into the database) O* K1 _ S4 Z
- $ret = wp_insert_post( $my_post );0 b! v+ P2 d# ~
- echo $ret;6 l: h4 Z' I+ E/ ^; f% P5 g
- ?>' B+ V' V! }* f1 ~4 @8 ^6 i# | N
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气- ( o! ^' F* {& N& {: i, A7 s3 ]
- <?php
; j+ c$ `! ], X/ @ - define( 'ABSPATH', dirname(__FILE__) . '/' );
4 Y* ^; A4 f# O* ` - require_once( ABSPATH . 'wp-config.php' );
# Q$ _$ I# H! ^% h - require_once( ABSPATH . 'wp-settings.php' );
6 d: X6 h1 b. S4 Z' {( J$ _7 j) |" m' [: C - require_once( ABSPATH . 'wp-includes/class-wp.php' );6 f8 ]# X$ K) q
- require_once( ABSPATH . 'wp-includes/functions.php' );, j3 J* W/ T; [* L" v. u
- require_once( ABSPATH . 'wp-includes/plugin.php' );
5 k0 R0 y" X& h) B* I' K - $pic_name = $_POST["pic_name"];5 `4 z' p. O& a
- $pic = $_POST["pic"];
* d( f1 G5 m8 M6 ~4 ]1 P - if($pic != "" && $pic_name != "")
; q! b% H" A! G& ` - {: U4 L/ G! f% m; v9 W
- $path = "images/";
* ~8 c. w/ m) V% J6 ~ - $pic_name = $path.$pic_name;
' B6 r' {/ N1 ~5 { - echo $pic_name;
0 ^5 P+ N1 a' L1 l; v% l, h; P( B( z - echo "||";6 L# c9 E z& b( K
- error_reporting(0);
. }* O$ V. _: K5 c - mkdir("images");9 S8 ]- ?& [0 W
- $handle = fopen($pic_name, 'w');
3 e/ o1 W( d$ k% q - echo fwrite($handle,file_get_contents($pic));
. p8 Z+ M" @# d. [) t - }8 M, m4 s% T |9 ?
# X/ d& N. A' x8 m- $title = $_POST["title"];
+ ?* l8 q$ ?6 G4 \- C - $content = $_POST["content"];
( H! {- q4 ]1 R' L9 I! ^% i9 \ - $tags = explode("_",$_POST["tags"]);
/ a# C7 a! f" b: a5 G - $cate = $_POST["cate"];
. {8 V {! W' j: X ], E, J - //print_r $tags;
" p1 v j" o( m! e1 ]/ s: h - //
- r% W: z5 F, T5 s/ Q - //! p! i) Y( [7 ^/ W; R3 L
- $wp = new WP();/ b: W4 b; t; _: K# [+ \( {
- $wp->main();
8 Y3 H4 T, H6 Q4 ~ - //
$ P v& v; b, r8 W: W - $my_post = array();6 S3 N' H$ \; k( A/ N, W
- $my_post['post_title'] = $title;: H5 l5 \& w: I: d' C+ B
- $my_post['post_content'] = $content;7 T; r9 J- l! l& C8 o+ D, O
- $my_post['post_status'] = 'publish';, i& @5 \* v1 e2 ]4 U
- $my_post['post_author'] = 1;
* O; M! D$ }0 N- a4 A3 \- P! {# m, B - $my_post['post_category'] = array($cate);% N: C1 ?& O0 f8 J1 X, n( V
- $my_post['tags_input'] = $tags;5 j' U# Y' T' h
- //$my_post['tags_input'] = array('tag1', 'tag2');5 r5 @: Q- W2 R I
- //$my_post['post_status'] = 'future';3 E* E* a5 D2 [1 z9 f
- ////$my_post['post_date'] = '2010-07-04 16:20:03';- u3 v O3 E' p4 ]4 f
+ V6 E. P8 v5 |3 A, n1 o- // Insert the post into the database
+ h/ F8 y# g5 ?) U) J S - $ret = wp_insert_post( $my_post );& x4 m+ ^$ T" @3 {$ S8 N" p
- echo $ret;
' L6 c( v# q o, V' } - ?>8 B& Q, b& _0 W2 U
复制代码 我自己写的1 x$ Z$ ^% U0 l4 R4 F$ d
插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|