|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑 # k6 b3 A) K$ z6 w6 E2 m
2 A) Z( k2 g+ \+ T- ^直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持
" Q1 T- j0 r2 X& t: F3 q0 T9 J! Z
3 Q5 w: d4 s8 S我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了1 ]5 X: Z( L7 j- Q- w
5 F' m* R/ b) T5 q; C T方便实用 简单 何乐而不用呢- 6 j" u8 C+ ^. E; u: e
- <?php, Y% \. X# C) @- t( i& ^! B/ z
+ M' \8 Z2 n j( u6 U- a- define( 'ABSPATH', dirname(__FILE__) . '/' );
; Y; M J1 Z- w& G: L - require_once( ABSPATH . 'wp-config.php' );
; w, U+ J% J" y) K- x2 L2 M - require_once( ABSPATH . 'wp-settings.php' );
9 V, h: Y$ q; i Z5 l - require_once( ABSPATH . 'wp-includes/class-wp.php' );3 u* o1 I* Z# @3 d6 u
- require_once( ABSPATH . 'wp-includes/functions.php' );
0 e) H6 l2 L ?3 t) K) K& [9 W - require_once( ABSPATH . 'wp-includes/plugin.php' );
# C, p1 p) F, Q - $title = $_POST["title"];
$ R+ I* R3 H1 i0 n2 y7 J - $content = $_POST["content"];
1 L5 ]/ \+ ]5 J. q; N' x1 O - $tags = explode("_",$_POST["tags"]);
/ K( z% ?! q6 U$ {/ \# Y! D - $cate = $_POST["cate"];
, E3 R' w, ?. k1 ~) J) E" m0 u - //print_r $tags;
; b l1 C8 R5 t( ? - //7 Y8 x' R. I S* \
- //
) o8 R" V2 A" T5 ]# r - $wp = new WP();" [% K3 h: J4 d. \
- $wp->main();
, [' d: r( s7 p* i) o - //
& n+ J0 @8 f1 r, d' O - $my_post = array();2 }- r3 j" f) _4 f
- $my_post['post_title'] = $title;. }! l# E* @2 T/ G' m
- $my_post['post_content'] = $content;
$ @8 \2 Y* ?. v( H' \% z$ D& Y - $my_post['post_status'] = 'publish';: M" Y$ M k: o% d0 T
- $my_post['post_author'] = 1;; T7 i' z7 j O% v
- $my_post['post_category'] = array($cate);# C. B; @. w0 J* G) G
- $my_post['tags_input'] = $tags;
+ z- r: Q$ V5 j - //$my_post['tags_input'] = array('tag1', 'tag2');$ s3 E9 Z- i' s0 M; C5 S" J* l
- //$my_post['post_status'] = 'future';" l k# n5 P$ d
- ////$my_post['post_date'] = '2010-07-04 16:20:03';
' r. V$ ]! A/ j' T% e' k
; `. H5 W2 {0 q1 _- // Insert the post into the database! Q5 d$ Z2 o2 y8 O) o
- $ret = wp_insert_post( $my_post );
S7 v! ^6 y5 J& k O& R' j" Y# f - echo $ret;8 c6 G; a6 L! N T% O
- ?>
: _: X6 l y9 B( I
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气- 8 D. Z2 Y7 M! t0 l0 d4 H
- <?php
% f- }& _4 L" @) W+ h - define( 'ABSPATH', dirname(__FILE__) . '/' );
8 f. B4 w! b1 D' d - require_once( ABSPATH . 'wp-config.php' );
; A+ p% u0 Y6 c9 U; G- z* F - require_once( ABSPATH . 'wp-settings.php' );
, |: @' g/ |# R/ P3 P - require_once( ABSPATH . 'wp-includes/class-wp.php' );: Y& D' D& B4 B8 r& \4 @# x
- require_once( ABSPATH . 'wp-includes/functions.php' );* q; P; e4 Z& z4 K! q
- require_once( ABSPATH . 'wp-includes/plugin.php' );
# E& X6 v; k, P: b - $pic_name = $_POST["pic_name"];
8 X T' E2 T7 u3 c - $pic = $_POST["pic"];
9 z7 e& p$ o, f. [ - if($pic != "" && $pic_name != "")7 c* H1 ^" r& A+ T
- {
; V$ }7 K( ^3 e - $path = "images/";, C5 C% m& C" J1 y
- $pic_name = $path.$pic_name;
- W0 |8 B! t4 v4 Q0 t" Z) n - echo $pic_name;
1 i. W/ j; R8 r' A: K5 ]( Q - echo "||";
0 s- m" q" A6 ]5 p8 _( ] - error_reporting(0);1 ~3 A: A4 P. u# }" d
- mkdir("images");" T' t4 D& X# d! Q& m" X0 h7 r& f
- $handle = fopen($pic_name, 'w');
9 p; L0 d/ A0 M( f - echo fwrite($handle,file_get_contents($pic));
2 U! r8 v; o! a+ ~# {" E - }
! u' Y$ E& o4 c$ S* K# O8 v
7 v7 |. c5 n8 t# g5 ^- $title = $_POST["title"];
6 L9 G; T3 |& Z& p - $content = $_POST["content"];
) N6 {" E- K" e; ^6 \+ c/ I7 ` - $tags = explode("_",$_POST["tags"]);+ k) K; B! L2 E9 S6 l" [
- $cate = $_POST["cate"];; Z2 ?9 q! k& v3 \) @1 K( Y6 [
- //print_r $tags;0 E9 F G9 C% S5 Z; r
- //+ p& d$ |6 Y; {7 \% p2 {0 @7 U
- //
5 R4 i- f b! C4 R - $wp = new WP();
) r9 z& J" p8 |- r4 f1 D - $wp->main();. l+ i2 R2 p( S$ h' F* Q
- //+ E1 u+ g8 k5 j5 l) p
- $my_post = array();
6 M' K2 `$ Z5 i9 Y - $my_post['post_title'] = $title;
1 {# c& V3 N E4 w - $my_post['post_content'] = $content; K& [4 P8 A2 _9 l9 z$ y' \
- $my_post['post_status'] = 'publish';/ c! P' w: i$ b- D4 ~5 s
- $my_post['post_author'] = 1;
7 v7 Z+ j# Q: T* T5 Q4 ^$ E - $my_post['post_category'] = array($cate);, l: M( V9 t3 a1 w* j0 c
- $my_post['tags_input'] = $tags;8 _" I7 }) ^3 T$ Z
- //$my_post['tags_input'] = array('tag1', 'tag2');% e/ J4 G4 i2 z8 M
- //$my_post['post_status'] = 'future';
' M0 q* j' h0 Y& g; Z - ////$my_post['post_date'] = '2010-07-04 16:20:03';
; Z" |9 Z6 N; k+ J6 p - 0 W& _- M( s# R' i k! s. \) d+ ^
- // Insert the post into the database
" P1 R. k2 r' X- ?7 v+ \8 @. g - $ret = wp_insert_post( $my_post );: U3 b0 c q# ~. ]$ Z
- echo $ret;6 I$ _' p- g v& p$ }( f. O" [0 T1 P
- ?>
+ k) E% D3 y6 h" t# K& i+ B( k. O7 E* h
复制代码 我自己写的$ g% S3 P) ?* J# t
插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|