|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑
- Z0 |+ T) s' n! S3 w
: B7 q, G% U4 d3 S0 Z" D直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持
" a" D* R0 [# a' q5 N" O* g
6 `" ^4 `/ s/ B5 R8 H& P我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了
1 |; @' X5 \* o) ^" ^
7 B" j, n7 f$ z# Z方便实用 简单 何乐而不用呢
a5 N+ \: T4 y2 ~# V8 e9 H- <?php- M* M2 P5 ^5 ^. y
/ Z3 _6 r5 e7 t9 {9 M2 e! y! d- define( 'ABSPATH', dirname(__FILE__) . '/' );& x) f4 x5 A) P! ]& b9 G! @! ~. X% V; e
- require_once( ABSPATH . 'wp-config.php' );( W5 @ m; Z/ y
- require_once( ABSPATH . 'wp-settings.php' );( f) Y2 T- s0 ?9 @, A1 L
- require_once( ABSPATH . 'wp-includes/class-wp.php' );
7 y. [+ `2 D8 m6 a. g' k - require_once( ABSPATH . 'wp-includes/functions.php' );7 e0 z2 M# {: G3 A0 l
- require_once( ABSPATH . 'wp-includes/plugin.php' );
" J6 C0 P0 C7 g$ t3 |$ a; W5 u - $title = $_POST["title"];
0 P3 w% q; B% [. B' { - $content = $_POST["content"];" y2 [1 |, |3 c( ]3 c' n6 H5 ^
- $tags = explode("_",$_POST["tags"]);2 }* z' k. d. I5 `! d
- $cate = $_POST["cate"];
% g# U% g8 L% n9 ]6 L( g5 R7 ~8 P - //print_r $tags;# N" l0 ]1 }2 Q5 g6 d/ O
- // {4 e5 |) \. H' h8 c9 E/ k
- //: M+ B! D! q% L! j6 t
- $wp = new WP(); ~. B$ [2 O1 X7 E4 E* H7 Z2 e6 q' l% @
- $wp->main();5 {6 ^# Q. V: T
- //
' v2 C5 l$ I( l/ v7 F' ` - $my_post = array();1 Z" U% P% h. T! ~( I( E# ~
- $my_post['post_title'] = $title;
1 [3 F/ `% w) `. B% M - $my_post['post_content'] = $content;
5 F& j# p" H/ S' W# Q/ m - $my_post['post_status'] = 'publish';8 m; V" A; l) S1 w7 Z4 d
- $my_post['post_author'] = 1;! J, n! p [& |' r' m6 b
- $my_post['post_category'] = array($cate);
* ~ b6 L3 `5 X3 H6 R# s9 y - $my_post['tags_input'] = $tags;
3 D$ T' N* l: @3 n" V/ H2 f - //$my_post['tags_input'] = array('tag1', 'tag2');" ^: X$ T: x4 U/ k9 R5 a; `4 D& t
- //$my_post['post_status'] = 'future';
" P* O4 m" \; S+ b - ////$my_post['post_date'] = '2010-07-04 16:20:03';
# ^; z5 d3 J1 E o; u - : V" {% Z: j( C( _3 a" K
- // Insert the post into the database Y8 z4 d3 c8 T5 P. J
- $ret = wp_insert_post( $my_post );
& ?! i) `1 H% u - echo $ret;, J9 D% k5 v6 _" n
- ?>7 T2 B* I, F; u
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气- " P, K+ I- I+ q# S& v' l
- <?php
I2 r+ Z) {7 n1 f# A* P, T$ { - define( 'ABSPATH', dirname(__FILE__) . '/' );
0 O x1 X9 q; p - require_once( ABSPATH . 'wp-config.php' );# K" h( j. i) |! `$ [* R- g
- require_once( ABSPATH . 'wp-settings.php' );
( O \' |2 M$ K. H. K$ | - require_once( ABSPATH . 'wp-includes/class-wp.php' );. Z0 ^& K( W+ F; T9 t
- require_once( ABSPATH . 'wp-includes/functions.php' );
; R0 L) o& C. A( X& k, p - require_once( ABSPATH . 'wp-includes/plugin.php' );3 G$ F0 d, Y# P; Z3 c
- $pic_name = $_POST["pic_name"];
4 a# S7 I! P( P, g# M0 P5 E K - $pic = $_POST["pic"];0 G0 F/ [% o7 r( Q5 `# I3 H
- if($pic != "" && $pic_name != "")
, C9 C# B3 m$ h) n - {1 ?0 t; i1 e. {3 \3 W
- $path = "images/";
) b/ t i6 {) E! r7 E( e2 d2 H - $pic_name = $path.$pic_name;
% v; v$ W$ w$ ]3 | - echo $pic_name;
. p$ e) E, O( e0 { - echo "||";; Y7 s* U8 x& K- I) K% `
- error_reporting(0);
4 x. S, a# Z' u" ] - mkdir("images");4 f7 ~! K7 a1 \; _5 h. c2 N
- $handle = fopen($pic_name, 'w');
1 R) G6 T8 N1 x" v; H/ @! ? - echo fwrite($handle,file_get_contents($pic));
1 w" S8 X# C& _ - }* b7 K! X. j( @& ~
- 0 i8 n; n7 s# o- ~5 [# z
- $title = $_POST["title"];
' _2 g4 k" s, X9 S- X' d - $content = $_POST["content"];; \7 `( ]: v4 J# b- ^# V3 a2 t
- $tags = explode("_",$_POST["tags"]);
& H3 T# R5 e! Y" Z" @1 Q) | - $cate = $_POST["cate"];
: n! ~# E* O# m( r: D8 R& p7 Q1 T - //print_r $tags;
6 j2 [7 b: a4 S2 @3 z" B - //
' i/ a7 F: T) A7 r - //
1 x6 n2 d: {4 ]" f2 f* F - $wp = new WP();# Q* e; a/ b3 q. `
- $wp->main();6 U9 ~$ {6 b5 G" c8 i
- //; H# E1 i6 P" b7 Z, {8 L
- $my_post = array();( u J7 K% @! q2 l, F
- $my_post['post_title'] = $title;
+ @7 i4 S$ I d C& j, H - $my_post['post_content'] = $content;
3 v# F) k1 B# b4 x' k - $my_post['post_status'] = 'publish';
8 x' f5 ~* K- B2 d6 ^1 j9 ? - $my_post['post_author'] = 1;! y0 ^$ v( a$ Y, K. I7 W2 T
- $my_post['post_category'] = array($cate);
3 A' \, r# u# B' b* k7 G! E - $my_post['tags_input'] = $tags;' A/ n3 x( f; v& G
- //$my_post['tags_input'] = array('tag1', 'tag2');, c1 L* } y S: h4 A& U
- //$my_post['post_status'] = 'future';
" Z3 l; b. d+ [( U - ////$my_post['post_date'] = '2010-07-04 16:20:03';
4 K% ]! M# Z* y9 J2 ~ - 1 ~) F& B+ B' L4 G0 N7 x
- // Insert the post into the database! T2 l# a: h* s8 U" e1 l
- $ret = wp_insert_post( $my_post );1 L% ^- d, _9 `& q6 {
- echo $ret;9 q; |+ Z4 u3 T# z" R( E% n
- ?>( r% y% z. g$ @: }2 Y" ~
复制代码 我自己写的
9 T6 o' F- }$ z2 w `2 I% n插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|