|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑
! d& Y$ ?6 g" F7 [: X8 B( U% t( u8 r- o' ?
直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持+ Z0 w1 Y1 X9 `$ h& `
" I0 }* j; k& ~* D) O$ Y我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了' [) t0 S M3 J/ {; w) O! E; m
( f2 i, V7 U1 h8 t1 }方便实用 简单 何乐而不用呢- * j8 }" O [ h, l
- <?php
, h' @5 r2 D. @) l" m- F$ b) y
( f x2 ~" f6 J- define( 'ABSPATH', dirname(__FILE__) . '/' );! ~9 U+ i6 X: I( O/ R8 t: n
- require_once( ABSPATH . 'wp-config.php' );; j* w5 F. i" f- ?+ ]# [7 v
- require_once( ABSPATH . 'wp-settings.php' );$ C2 _$ H8 {, O9 z+ t
- require_once( ABSPATH . 'wp-includes/class-wp.php' );
8 ]3 f0 Z$ n+ W- v. Z - require_once( ABSPATH . 'wp-includes/functions.php' );5 |+ l+ v# P" t
- require_once( ABSPATH . 'wp-includes/plugin.php' );; V# t' }& D- w# ~$ Q( ^" K
- $title = $_POST["title"];% M; T0 k! L5 {
- $content = $_POST["content"];5 D) m5 g- m3 X
- $tags = explode("_",$_POST["tags"]);
. @" z# \% f! b6 l6 ?! Q - $cate = $_POST["cate"];
# ~# x8 h- N, R% s% F - //print_r $tags;
% C6 p" r3 M1 i6 Q' u. M - //
7 W2 ~) y K' A' U: |% { - //% y( G l& [- y- [8 Y4 ]
- $wp = new WP();7 G; E/ ]+ W- b2 ^
- $wp->main();8 { z) a( A. ^' r
- //+ q- y2 c5 c: q" Z7 G
- $my_post = array();" q: Z" h. J& o/ g6 M4 x" I
- $my_post['post_title'] = $title;: p% `0 f0 l& [( s( H4 @
- $my_post['post_content'] = $content;
) v8 b: c) ~8 M! [2 X3 W - $my_post['post_status'] = 'publish';7 h/ q( B* G, n/ n, q
- $my_post['post_author'] = 1;" t% b$ \6 o& u' z5 g1 O
- $my_post['post_category'] = array($cate);; A y) I0 E4 f7 d% j
- $my_post['tags_input'] = $tags;, [6 U1 t# ]! q, U8 s
- //$my_post['tags_input'] = array('tag1', 'tag2');! w4 P) i" K, E8 K( b( p" J X4 ~" I
- //$my_post['post_status'] = 'future';" |7 ^" n, s- T1 d( U$ n: X3 I
- ////$my_post['post_date'] = '2010-07-04 16:20:03';
3 d Y$ j% ` T2 l. w - 3 V( I ?$ w* B# E
- // Insert the post into the database& K+ h3 a: E& s Z0 K6 P3 b$ T
- $ret = wp_insert_post( $my_post );
1 A& d$ T' m3 k - echo $ret;) @& X" D( Z6 x& c) [
- ?>2 m6 A* T: N. b$ X9 L
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气
. B- x" D B, T1 M- <?php: _# |, v+ U6 Y$ ^+ K! Q
- define( 'ABSPATH', dirname(__FILE__) . '/' );
* e9 i' E8 t6 h b - require_once( ABSPATH . 'wp-config.php' );
0 M7 l3 _7 K6 t8 o0 ^ - require_once( ABSPATH . 'wp-settings.php' );3 w/ Q( c; V' z
- require_once( ABSPATH . 'wp-includes/class-wp.php' );
8 H1 n3 w! @' s* b - require_once( ABSPATH . 'wp-includes/functions.php' );
& b$ q, g. p, t2 B: p) v" R! F& ^; j - require_once( ABSPATH . 'wp-includes/plugin.php' );
* {; m }* R3 N/ E - $pic_name = $_POST["pic_name"];
/ o" @0 x2 v* E3 i& K4 L - $pic = $_POST["pic"];
' J" V# P/ h1 l. v - if($pic != "" && $pic_name != ""); T, h# h1 o/ S/ @
- {. R; K! u, B: Q! D% n5 I9 g0 F# x
- $path = "images/";
& }/ N) P3 s7 j2 r9 o+ E - $pic_name = $path.$pic_name;
# w) [) G) z% h/ {! a" E o4 Z" V% w2 S - echo $pic_name;
1 [+ ~) O' W" o9 v$ C; L: W9 ^6 {* a - echo "||";$ _2 @4 w8 s' Y6 `
- error_reporting(0);
- H. J2 E) l: B# H0 u - mkdir("images");9 G. _: n/ p1 T& r
- $handle = fopen($pic_name, 'w');
( P) A5 ~7 t% [ i \0 O - echo fwrite($handle,file_get_contents($pic));
( ^ H. g; ~; y5 N, |# E - }% o7 T" N1 s" p N. q+ g+ q
6 z! a: O5 ?6 [+ [: x9 u4 v- $title = $_POST["title"];
, l; |# b& m; m$ m$ u - $content = $_POST["content"];
2 d1 S8 x( o8 h1 L' d: q - $tags = explode("_",$_POST["tags"]);- n9 f2 g$ C9 D3 o1 u+ w! ]
- $cate = $_POST["cate"];
A$ j& n; O# y - //print_r $tags;
: M3 h: I& ?$ [9 Y q1 V - //
# E: a$ P8 I: ^ - //
) m* j1 N j' h. H - $wp = new WP();
5 m. v0 ~0 }- E4 i2 a$ v5 d: e, T - $wp->main();
( r2 ^9 {; M" \9 l - //
9 W3 Y3 B8 M$ [ - $my_post = array();
# U/ k1 f' ~/ f+ O8 o6 _* v - $my_post['post_title'] = $title;
. j: x- {. T$ O4 R" O - $my_post['post_content'] = $content;8 {1 d3 H; [6 N8 I8 K7 A" V! X5 y
- $my_post['post_status'] = 'publish';7 K! G* ~ y$ g, O4 J# x% F6 u; b: d
- $my_post['post_author'] = 1;0 v3 j( W0 X5 G" \
- $my_post['post_category'] = array($cate);7 O) ]* U* o; M$ x
- $my_post['tags_input'] = $tags;
( _% R4 B" {' O$ Z, y: n* Z# t - //$my_post['tags_input'] = array('tag1', 'tag2');. q6 ]4 f" s" l& M
- //$my_post['post_status'] = 'future';% ~0 [& _8 y# D2 e, q
- ////$my_post['post_date'] = '2010-07-04 16:20:03';
2 T) ~/ R7 a+ ` q4 Y - 6 y( z6 z9 ?/ n+ |7 J
- // Insert the post into the database- Y$ x0 I! F% r: Y& m, [/ C1 a+ D
- $ret = wp_insert_post( $my_post );# _7 O: U1 M, _9 r6 A2 z
- echo $ret;
0 z* w+ s+ W3 _9 I' P: \ i - ?>
$ Z# C3 R" `. t p# k
复制代码 我自己写的
: K$ Z# w N9 X# H+ } g2 }0 V插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|