|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑 ( t* f( M3 u( F8 i3 y2 g9 P/ y
8 N. t; R$ a1 j: \
直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持
, i9 \3 H1 I1 U2 }3 I0 {: q' a& E( [. I# A Q
我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了) f+ R3 }7 O& {; l9 i5 Y9 m0 \
2 p3 q2 p- z* Q- r7 g
方便实用 简单 何乐而不用呢- ) b m* J8 n# |% C
- <?php
* u/ f3 d; t" M. q& v
4 r* u8 c9 |6 K" Z" H- define( 'ABSPATH', dirname(__FILE__) . '/' );
: c2 R' }" U1 H - require_once( ABSPATH . 'wp-config.php' );: j) e2 U- T9 L
- require_once( ABSPATH . 'wp-settings.php' );
0 H7 \! z( V& E2 y5 B, Q- | - require_once( ABSPATH . 'wp-includes/class-wp.php' );- t# k. `0 ~( j! i9 d
- require_once( ABSPATH . 'wp-includes/functions.php' );4 X, B: i9 Z& D) e/ M: _- @2 l
- require_once( ABSPATH . 'wp-includes/plugin.php' );: E9 I5 K9 g6 A) S4 ^6 j& }1 U
- $title = $_POST["title"];
7 b4 L0 ]$ l9 c( w - $content = $_POST["content"];
/ a! V5 W( `% h7 C1 ^" l - $tags = explode("_",$_POST["tags"]);3 P( r: Z+ a6 Y0 N3 p/ \- N) R
- $cate = $_POST["cate"];
+ ^3 ]- `" ^/ r; G - //print_r $tags;& u2 d0 P- {& y) t
- //9 ~# w1 C* Q$ w R0 v$ x/ {4 i
- //3 c1 I$ m2 i; y
- $wp = new WP();
* m+ \' m4 G0 R9 J2 w - $wp->main();
1 y& L K. k. G7 ]: {' m - //
& Z5 w. p6 l+ t; M - $my_post = array();' \' s- D1 }& k. f
- $my_post['post_title'] = $title;# _1 i; U1 Q* T/ f# a+ b' v
- $my_post['post_content'] = $content;- a/ w1 {% R1 r5 @# d, y
- $my_post['post_status'] = 'publish';- Z) K) y3 C9 [8 F1 z) M
- $my_post['post_author'] = 1;
1 m! G O- I% w( A0 L - $my_post['post_category'] = array($cate);0 H! j3 w# n3 i3 s6 S4 ^0 c
- $my_post['tags_input'] = $tags;0 T4 ]& h1 Q+ V; T% q
- //$my_post['tags_input'] = array('tag1', 'tag2');
( i3 Z+ J+ F w A. m) x- S4 y - //$my_post['post_status'] = 'future';
% R1 I3 q) T; r7 L/ _* B+ C% [% u - ////$my_post['post_date'] = '2010-07-04 16:20:03';
7 c3 T7 k7 u8 R8 g0 u( W- A
9 g4 Q& t) I, _: Y1 D2 l- // Insert the post into the database
9 }4 g e7 C# c' ^: B1 A - $ret = wp_insert_post( $my_post );
( D* M5 ]8 q! {1 e" f( ^0 ? - echo $ret;
: d* c2 f' W, p( p - ?>4 N& A" P4 `) R0 i7 [, z; f% H
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气
( b6 i4 k: \* m3 q6 h( y- <?php: `* y' S$ @3 k: z5 S
- define( 'ABSPATH', dirname(__FILE__) . '/' );
: m9 p- P+ N% s4 \; B& S9 ]" j+ R - require_once( ABSPATH . 'wp-config.php' );% [, D6 Q6 l% R1 E1 s6 a
- require_once( ABSPATH . 'wp-settings.php' );
7 v+ a/ M! Y, l/ D8 S$ U8 l - require_once( ABSPATH . 'wp-includes/class-wp.php' );3 l: U2 E' n- s" m4 c4 ~
- require_once( ABSPATH . 'wp-includes/functions.php' );) t& Q+ f1 t- `) g( e M
- require_once( ABSPATH . 'wp-includes/plugin.php' );+ Y& f- p+ u: V5 ~; [, @
- $pic_name = $_POST["pic_name"];
% Y, X: I9 E, \; x - $pic = $_POST["pic"];* U8 y5 `3 \5 F" ^" |
- if($pic != "" && $pic_name != "")+ P+ i3 a0 y9 }* @" e
- {
* n& g+ F1 C& T" Z! \3 V - $path = "images/";
( m" T# y' p F9 B - $pic_name = $path.$pic_name;) u0 g4 s3 Q; L4 p8 N$ ?
- echo $pic_name;
1 A+ p/ @ L: z - echo "||";
; r+ I: \6 N, E! o1 C - error_reporting(0);* S( T( g0 y, m5 r* L
- mkdir("images");" O' }% o% `; o m- R& a
- $handle = fopen($pic_name, 'w');
) H9 C6 Y, ?# C5 A0 s1 Q$ v* M - echo fwrite($handle,file_get_contents($pic)); D) m8 Q+ c: C8 V, u' n
- }
4 v0 c5 j# r# ~6 _$ @# I - 8 ~0 h* h, K3 {- Y$ W3 X% I$ B, o
- $title = $_POST["title"];
) i, F% g) f: `' E; e - $content = $_POST["content"];
2 G# P" @" c* t: k" L - $tags = explode("_",$_POST["tags"]);
- l/ y+ P& w g6 c - $cate = $_POST["cate"];: ]" |9 ?* X D( _. t
- //print_r $tags;1 h- t6 X1 Z+ ^3 g4 Z* |$ I
- //
0 P/ u: t% V. T6 Z/ w - //9 x. Z1 {! ~+ n% n
- $wp = new WP();
( `/ o5 j; U; r7 V* Y6 S - $wp->main();1 X- j! F2 X4 d* j& D7 ^" d
- //4 E& t- N2 z- r/ D
- $my_post = array();1 \- E. R+ L* K) D3 v/ h* {/ B
- $my_post['post_title'] = $title;
3 g0 J! ?+ a/ [; ~ X - $my_post['post_content'] = $content;8 e% w& ~. i! y8 w
- $my_post['post_status'] = 'publish';1 G e8 B$ m' u4 c0 _
- $my_post['post_author'] = 1;
2 H) c/ l) p, |: D% |0 w( w, a - $my_post['post_category'] = array($cate);6 v) b7 X$ K7 h' Z! S4 h
- $my_post['tags_input'] = $tags;
" l. y e( c) W - //$my_post['tags_input'] = array('tag1', 'tag2');
# B* M. r1 V- G: A4 c) {( o o. t, l - //$my_post['post_status'] = 'future';
$ K& p0 [1 L, D r* m- F7 A' _ - ////$my_post['post_date'] = '2010-07-04 16:20:03';
. n. u2 W" M8 n. @- G1 n `" ` - 1 e8 ?8 W- I0 ]3 }1 b
- // Insert the post into the database, `9 H8 t9 ]2 t8 Y \, P5 E0 |
- $ret = wp_insert_post( $my_post );
5 v }7 U6 ~4 n0 @5 W. L0 [, Q. H0 g5 B - echo $ret;9 a! L! T- Z9 T/ \" H+ ~# Z
- ?>
% \$ U+ r. |* m+ o9 M
复制代码 我自己写的- w/ L5 Z* S6 N& J( K6 `! ^4 K
插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|