|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑 1 E0 e8 _% m7 S$ v- {
6 [3 K7 R% n0 s( A% O0 G, p
直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持
3 W3 N. }. z4 P b! q1 g
, L' K( Q. G: Y1 V0 @" e我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了 Q2 N0 W4 R$ T3 S4 }! H% u
7 j) p3 e1 u, O% J3 M方便实用 简单 何乐而不用呢
* L R, X9 Z2 H- u- <?php
0 y2 F9 @ b* [( X+ u
- o" g6 r# m$ b) s( s. U- define( 'ABSPATH', dirname(__FILE__) . '/' );* E+ E! y, V1 E' X
- require_once( ABSPATH . 'wp-config.php' );/ ~* g6 k3 O0 w' g) O& `0 c- q. i: D
- require_once( ABSPATH . 'wp-settings.php' );
8 l9 i$ q" F* {& u! Q7 C - require_once( ABSPATH . 'wp-includes/class-wp.php' );
" h$ d0 n' b' w: X - require_once( ABSPATH . 'wp-includes/functions.php' );
3 n0 h; a0 J0 V - require_once( ABSPATH . 'wp-includes/plugin.php' );# {/ M* K1 Y# V+ I/ X
- $title = $_POST["title"];
: h2 |% q2 }# y! C - $content = $_POST["content"];
% b# { S+ n E- m3 V* z - $tags = explode("_",$_POST["tags"]); F. q$ N+ X! k5 S5 O: o/ R b
- $cate = $_POST["cate"];- K F; k% l" p: ?( Z7 v1 W
- //print_r $tags;
3 \& p2 a: @3 V - //, S! z/ B3 r" v4 M) a
- //
1 q' k6 M$ x7 L" q; t1 c - $wp = new WP();
$ |$ K- o& |& e$ r - $wp->main();' v7 H2 c6 C% o
- //, @6 z( x6 W1 N+ K. i% r1 b
- $my_post = array();9 w' I4 Q& Q6 u# n
- $my_post['post_title'] = $title;
& I D$ ?+ [, W% g; Z* |4 \ - $my_post['post_content'] = $content;
1 f! e9 @4 j# [+ T% D9 O, } - $my_post['post_status'] = 'publish';
, x8 n: P9 n% X+ {* U - $my_post['post_author'] = 1;
# w" w3 |7 t- | - $my_post['post_category'] = array($cate);
! |2 x2 z. ~, G - $my_post['tags_input'] = $tags;4 S1 Z! b5 d+ F( W9 `( H; n& N
- //$my_post['tags_input'] = array('tag1', 'tag2');
4 a( `3 ]8 `7 B `! O8 n - //$my_post['post_status'] = 'future';
1 x6 G, @& |4 e8 J: { - ////$my_post['post_date'] = '2010-07-04 16:20:03';
& t% y* f6 q7 F - ! ]6 R5 p0 ~7 k
- // Insert the post into the database1 S4 F3 O3 W. k$ G# c1 |% u
- $ret = wp_insert_post( $my_post );9 z3 U0 |7 J& o2 }! E, U
- echo $ret;7 w/ j3 ]- [! e2 ~
- ?> J! X. W' D: W9 u: t
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气- . n" {( S* \( E) @2 g6 S. ]! i
- <?php7 f! O( p+ |+ b& k' o w
- define( 'ABSPATH', dirname(__FILE__) . '/' );. ]/ E$ f4 G5 |4 r
- require_once( ABSPATH . 'wp-config.php' );
' S8 h5 ^) a2 t/ B, i7 _2 V - require_once( ABSPATH . 'wp-settings.php' );
9 R. M7 d4 j/ T3 d& D' j- j$ k0 r - require_once( ABSPATH . 'wp-includes/class-wp.php' );/ r3 w5 {, s7 h L$ s
- require_once( ABSPATH . 'wp-includes/functions.php' ); L5 i0 r# p4 F
- require_once( ABSPATH . 'wp-includes/plugin.php' );
+ @ x1 \! F' X3 k - $pic_name = $_POST["pic_name"];
- d, q3 s9 w7 F+ F# o6 j - $pic = $_POST["pic"];: D3 f% c1 m/ E/ D8 e: U7 b2 T) B [
- if($pic != "" && $pic_name != "")
) P0 i$ c/ i7 u - {
& e8 {7 L. [0 P$ S* [: ~, ^- P2 h - $path = "images/";3 H! L$ _* I! U: l8 p1 L
- $pic_name = $path.$pic_name;8 }% M1 v2 ]# W, J4 x. q* q0 F
- echo $pic_name;" j3 m' I1 P; `( D2 h# V; Q+ w
- echo "||";
- ]) r% M" l1 L8 A - error_reporting(0);
) _0 {6 `" v) n x - mkdir("images");7 l9 q" }' i2 ~9 y! s+ r
- $handle = fopen($pic_name, 'w');' V% {! {4 W5 N/ Y! E# \
- echo fwrite($handle,file_get_contents($pic));
) M* T( Y+ S3 z! k - }1 D( V4 ]7 J# B2 J2 O; d9 w
- ; ?; u i6 l# \& y- t
- $title = $_POST["title"];
9 [# C0 H2 e Q! K T* D - $content = $_POST["content"];5 V* S w- y9 o/ F$ }
- $tags = explode("_",$_POST["tags"]);* Q4 }( M9 a2 q% O( u8 s* d
- $cate = $_POST["cate"];
0 O0 V: d# B) A5 `( |; P - //print_r $tags;
; V4 L0 W1 v! G& E# J \" D, ~% l/ e - //) b, H1 K, r# J
- //
) v0 K# E% |# A6 y& @# v+ W - $wp = new WP();; m) }# A( ` Q) F! S
- $wp->main();" c' d* u0 l2 a M- {1 _% X
- //6 t( G2 k/ u. v9 N$ K' q/ e' Z1 J9 x
- $my_post = array();
; T1 J% Z+ Y2 t0 Q7 E% ? V& K - $my_post['post_title'] = $title;8 d& M1 l+ R* `; e
- $my_post['post_content'] = $content;' \* O, f4 l* T3 W! ^ |
- $my_post['post_status'] = 'publish';. A7 B4 a; x: ]2 ]0 R
- $my_post['post_author'] = 1;
- Z8 V- N- y% @$ b& i - $my_post['post_category'] = array($cate);
3 I% S* v* |3 R) F& r/ P: ] - $my_post['tags_input'] = $tags;
& B& C" {1 w4 c6 b' T/ _ - //$my_post['tags_input'] = array('tag1', 'tag2');
5 B" j0 t \7 c' | - //$my_post['post_status'] = 'future';
8 j- s' X* w7 a0 r - ////$my_post['post_date'] = '2010-07-04 16:20:03';
) X% V) d( y0 K& V1 W - # w! n: z% [% s# [- Z; A
- // Insert the post into the database+ Q( z& f% e8 u. W4 f2 T# S
- $ret = wp_insert_post( $my_post );+ O Y4 P7 ^: q$ R( h( H' K
- echo $ret;
1 t, G0 W# D3 w - ?>" {7 A$ L2 s/ p% e
复制代码 我自己写的
3 g6 ^) o, ~ o插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|