|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑
- K( F, }, B9 a) n+ c- j0 ]' k+ x0 q8 |8 R! n
直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持0 e" S: p' S5 m; J& s ~! b9 `
. A) q6 ?8 u# v5 ?$ B$ m$ M7 W我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了: Z4 o1 s: {- U4 j6 D" r3 a: p
' m- m( I( o0 W! H% v3 _
方便实用 简单 何乐而不用呢
8 H0 B" t: `3 @' @7 S5 m; ^& a- <?php/ d: l8 x0 R, R$ X
- # k$ y& s* t5 B: x4 s0 f/ M; A; y3 n+ A
- define( 'ABSPATH', dirname(__FILE__) . '/' );5 ^( w% V! L9 z) I8 V
- require_once( ABSPATH . 'wp-config.php' );
" ~$ N5 i# Y! t3 n7 T - require_once( ABSPATH . 'wp-settings.php' );
0 t9 r6 z& I, C3 i: v) f - require_once( ABSPATH . 'wp-includes/class-wp.php' );# ?) m8 a5 v4 }) w
- require_once( ABSPATH . 'wp-includes/functions.php' );" k" a* m! j* j& G) F7 x
- require_once( ABSPATH . 'wp-includes/plugin.php' );
B, Q- e) [- g. }; z - $title = $_POST["title"];
5 Y1 w3 Y n+ ?: K% b$ T$ e - $content = $_POST["content"];7 I+ W, _/ b& p) \# A
- $tags = explode("_",$_POST["tags"]);
1 H8 F6 T8 z7 V% u& d& m) X - $cate = $_POST["cate"];$ u9 f/ o9 G9 D, K
- //print_r $tags;
9 ?2 N) R1 p6 t% I8 m# X6 B( q; p - //
' @" L- _. @# L - //
' f. s O7 _$ b( a - $wp = new WP();" y) g, @4 M, {9 j# _' b
- $wp->main();
: x( S' H6 Y& H% x - //# b- M ^8 z8 v/ G+ d
- $my_post = array();0 r0 @2 ]+ d% X3 [- a" z, t
- $my_post['post_title'] = $title;4 b) ~2 [% p! z$ u9 m+ G4 [
- $my_post['post_content'] = $content;
( M) L! z1 b8 y; Z& ], i! ? - $my_post['post_status'] = 'publish';
c( B5 `. A- Y3 e& I P) u2 e; r - $my_post['post_author'] = 1;
) @5 b# w' Q! O/ K1 g9 \' v6 n - $my_post['post_category'] = array($cate);
! d' p0 }/ m3 O+ N- M0 t3 M - $my_post['tags_input'] = $tags;
9 W( N2 J! d% j8 J/ ?6 u9 {9 V - //$my_post['tags_input'] = array('tag1', 'tag2');
/ l, V6 R% }9 L4 T8 a2 a( f - //$my_post['post_status'] = 'future';, {) k v. S5 E# I8 y
- ////$my_post['post_date'] = '2010-07-04 16:20:03';( d, Q8 }; r, L: k' A# \
- : v0 b7 C0 Y) Q
- // Insert the post into the database6 A9 C# q3 n! B+ G; U$ R
- $ret = wp_insert_post( $my_post );7 {, ~5 T( p8 B. I7 a6 i
- echo $ret;) ]# H! D- P5 h7 e* [7 c4 K1 p
- ?>1 g' \/ N2 _, Q4 y- m
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气- ?3 _# h2 O- X' W, I7 {
- <?php
. W2 u4 ]) X. Z3 J - define( 'ABSPATH', dirname(__FILE__) . '/' );( d* p% r. ^/ n( h1 }. B3 g
- require_once( ABSPATH . 'wp-config.php' );" Z! j, V' ?8 n4 ~* L
- require_once( ABSPATH . 'wp-settings.php' );2 L8 T# H. P- @/ Z
- require_once( ABSPATH . 'wp-includes/class-wp.php' );
( _8 F" A5 J$ u0 ^6 x! m - require_once( ABSPATH . 'wp-includes/functions.php' );
7 `) |( N' U% B$ F; ] - require_once( ABSPATH . 'wp-includes/plugin.php' );+ U( g# \! u9 a/ ~) y
- $pic_name = $_POST["pic_name"];
2 |, E. O( @4 } - $pic = $_POST["pic"];
- E) _+ E2 W- M$ f s, J6 S - if($pic != "" && $pic_name != "")3 |0 P' y/ @; q/ g7 Z
- {# B1 Q; o/ f3 [- }. H% b" |
- $path = "images/";
- K' u1 Q' L& E6 X P; f+ a* ^! ^ - $pic_name = $path.$pic_name;
$ {- t. h# Y4 S' w - echo $pic_name;
8 C6 {) j, Z$ ~! @/ g& C; w - echo "||";
# y' X" `: b0 T/ T& q0 [# V2 p - error_reporting(0);
5 V% x& M1 v8 E8 U' W! L9 ]5 B - mkdir("images"); ? C! M" U. v3 d; p* e
- $handle = fopen($pic_name, 'w');
7 b& O' d! L% S0 w4 f - echo fwrite($handle,file_get_contents($pic));
, `6 s$ L, ~( n - }
0 v6 j) T; s% m/ d4 \
! a" h, P2 W+ P; z- $title = $_POST["title"]; L- R0 ^/ c8 r) K& Y
- $content = $_POST["content"];; r" E z. A5 L+ r9 x
- $tags = explode("_",$_POST["tags"]);
$ H: J" ]6 q5 c; U: P1 m2 P3 G - $cate = $_POST["cate"];
J/ @8 N% c$ P1 ^+ a2 [ - //print_r $tags;% |: ^# ], ^% Z. h
- //
# B5 B5 b7 _& N6 u1 ?: t - //
1 _5 y/ I1 X7 W - $wp = new WP();
* r0 O( V8 n# Y6 h( W7 L5 ^ - $wp->main();! L9 h' u$ d! E: C8 |9 R
- //( P- H' |/ h0 k! u
- $my_post = array();
/ o! M* |3 n' J) u1 l2 W: a( \' M - $my_post['post_title'] = $title;8 v3 ^/ ^! Q/ J8 Y: y6 L3 E+ ?
- $my_post['post_content'] = $content;
3 G) o( ^ K6 u - $my_post['post_status'] = 'publish';* t( B7 v d* n1 D! w& S3 T
- $my_post['post_author'] = 1;* ?- Y! `- O9 c8 w G8 ]
- $my_post['post_category'] = array($cate);" J) J- G3 S% t' q
- $my_post['tags_input'] = $tags;
. C! r, f/ e2 U$ I$ B* f - //$my_post['tags_input'] = array('tag1', 'tag2');
6 x9 V/ m+ N" `1 o# _8 U - //$my_post['post_status'] = 'future';; c& w6 W8 r& J0 n, h% E4 N
- ////$my_post['post_date'] = '2010-07-04 16:20:03';
O# D" a- R0 W: Y1 h) `% Z% m6 K
5 T7 }% I( {. C' b- // Insert the post into the database
, t" s3 Y7 d3 D/ f - $ret = wp_insert_post( $my_post );5 b& i4 y& s& x% p
- echo $ret;& }& s; }! v7 }1 s0 [0 e
- ?>
- Q2 v4 o. Z1 M# \7 T3 K7 _
复制代码 我自己写的6 d2 [: k e' d$ w! z% F n8 A
插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|