|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑 . L, Y" X, J* F( N' V5 B" U1 D: r
% O9 e( C/ W) @( H; Y! R
直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持6 m- L* f# w; n- Z/ Q% P0 h) v
( H3 C( R3 H4 f- S8 u. ^1 O. O我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了
' @" o! d( H& t: r8 n ]+ n+ r- ^) B4 \
方便实用 简单 何乐而不用呢
4 b6 M. v0 `3 Q: c8 `8 g- <?php
+ n) c$ d* t: V5 K8 _
, ?( {$ u: }* U9 o* x4 n6 x' t- define( 'ABSPATH', dirname(__FILE__) . '/' );4 [0 ]: \) w- _0 b; d. p
- require_once( ABSPATH . 'wp-config.php' );7 p' e8 V8 ?1 ]7 u( s
- require_once( ABSPATH . 'wp-settings.php' );
+ _0 B; y7 L2 @) K+ J - require_once( ABSPATH . 'wp-includes/class-wp.php' );: Y9 k- ]& n/ J" c8 d
- require_once( ABSPATH . 'wp-includes/functions.php' );% e' q; `) E$ U7 c8 ]3 X
- require_once( ABSPATH . 'wp-includes/plugin.php' );
' J+ [5 f5 {) C) a, A - $title = $_POST["title"];( `8 m X( ^0 b
- $content = $_POST["content"];
, Z$ M7 S r' k3 D+ i. v5 ~8 l" } - $tags = explode("_",$_POST["tags"]);
% C& }5 c- S9 X/ `3 I/ T9 v - $cate = $_POST["cate"];7 F4 H' A( X. F
- //print_r $tags;4 C- j5 }3 ]1 R+ ?
- //
6 }) A; m& ~7 j3 j( A2 b9 ], \6 U - //0 l4 D! o3 g0 g. k
- $wp = new WP();9 z, T) @6 Q1 j# J
- $wp->main();+ J8 e, K2 S5 P
- //# j" v2 r9 ~6 R
- $my_post = array();
3 S( J+ J% f0 `( M - $my_post['post_title'] = $title;
: b% a) G% o: e* D( y - $my_post['post_content'] = $content;5 ~! G; s0 ?8 ^) z+ y# p3 b
- $my_post['post_status'] = 'publish';: f0 ~1 }4 @" G4 w
- $my_post['post_author'] = 1;, C; x* I- Q0 O. I2 z; S
- $my_post['post_category'] = array($cate);" W( | `/ Y0 J) G6 D* ]8 d2 @
- $my_post['tags_input'] = $tags;) z! V! M3 |7 o0 d' m
- //$my_post['tags_input'] = array('tag1', 'tag2');
9 I6 t( M: M ]6 Y - //$my_post['post_status'] = 'future';1 t0 H/ B' y: |
- ////$my_post['post_date'] = '2010-07-04 16:20:03';% E' d, J# L, _7 c/ x/ n+ w
- 1 y9 [- A! J% }$ A( x2 t
- // Insert the post into the database
- D! g8 k" O& o, K9 s" U - $ret = wp_insert_post( $my_post );; I$ v: T8 j4 w0 B
- echo $ret;* O4 s- ]; ~) g
- ?>: M) G. G& @! l* {5 e$ u
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气- 3 c* f. ?' D' z4 a) @+ f5 r
- <?php
3 ^& }# ?( j) h1 V" e" i - define( 'ABSPATH', dirname(__FILE__) . '/' );6 ]/ n' Z* `; g, M6 Q/ s- l# _0 G
- require_once( ABSPATH . 'wp-config.php' );4 e7 Q9 c: R t" p
- require_once( ABSPATH . 'wp-settings.php' );
+ ^/ P- E& H$ m6 _3 D - require_once( ABSPATH . 'wp-includes/class-wp.php' );
, @! ~; G$ k+ S: | - require_once( ABSPATH . 'wp-includes/functions.php' );0 @! T: z0 M: l4 C
- require_once( ABSPATH . 'wp-includes/plugin.php' );0 \+ H8 h+ i# a/ M* w
- $pic_name = $_POST["pic_name"];3 i8 L0 }9 w, \/ N
- $pic = $_POST["pic"];8 M( u( q/ c1 }( n0 ?+ Y
- if($pic != "" && $pic_name != "")" y+ e8 ?! |) }3 `3 {* t2 f* ]
- {
5 ]9 C; `( z) l8 c+ ] - $path = "images/";
! ]# {9 L* \5 G+ X+ b - $pic_name = $path.$pic_name;4 L2 q( n# p7 o3 N# o2 \) k* r8 x
- echo $pic_name;
: t& m7 l- b9 _. H - echo "||";( Z2 x* F. c4 W& j2 z
- error_reporting(0);3 Z" i3 B1 s: a6 y* s: G, o. \& {, V
- mkdir("images");
; [3 `5 _$ z @/ k) @+ g4 k' w4 h - $handle = fopen($pic_name, 'w');+ I: y9 O- r7 s
- echo fwrite($handle,file_get_contents($pic));
2 p- b5 ]5 s' ]4 T3 b - }
l) F/ T: P: l3 u - * J E+ n8 X# B0 c" O& J7 a8 d7 F
- $title = $_POST["title"]; \2 J% u$ e" S% e' R
- $content = $_POST["content"];: Y& E2 z# u" s& E
- $tags = explode("_",$_POST["tags"]);/ ]/ K0 u# C) R
- $cate = $_POST["cate"];
! I ]- }! J! b& ?5 m - //print_r $tags;
/ W k! t& z9 F* b% G - //
! k* x- K0 {6 N! w8 D8 l - //
& K) h& O0 Q5 B0 V' E - $wp = new WP();
# T4 W% J5 o v' ~+ X - $wp->main();
9 {% e9 j7 d# f p0 [; b$ g, h - //5 {1 B8 Y. g* B" R* e; @* Z
- $my_post = array();) U' o/ u" k6 Z
- $my_post['post_title'] = $title;' @* L0 B+ z9 r' h9 e+ |* ^
- $my_post['post_content'] = $content;
& Q/ J; R4 a8 ~ l, G. o0 o - $my_post['post_status'] = 'publish';6 q! t/ s6 Y. l4 ^( p* K
- $my_post['post_author'] = 1;% e) l5 {1 R* W1 p
- $my_post['post_category'] = array($cate);
6 j2 a& Q- r: e8 Z2 J4 ~' E - $my_post['tags_input'] = $tags; G5 S% P \, \1 ?/ ^6 m j* L8 b
- //$my_post['tags_input'] = array('tag1', 'tag2');
9 |& ]' _% g. a/ \7 T# f - //$my_post['post_status'] = 'future';
) C6 g! M$ g o - ////$my_post['post_date'] = '2010-07-04 16:20:03';7 }5 ^6 a, \) n8 Z D
- # z2 \0 x! u+ h0 U/ l2 e6 M
- // Insert the post into the database
' _, [8 ?0 m/ H4 C8 g6 A0 f - $ret = wp_insert_post( $my_post );
$ |- |8 @2 H+ e; i! ? - echo $ret;
' y/ Z ]+ f1 A; _5 o - ?>- D7 q7 t5 A+ h$ S8 n( z
复制代码 我自己写的
, s: x# @" s! s插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|