|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑 2 I6 ~1 o( X# V2 p- s, x" j5 k
% D7 |7 b4 T. V( l5 G L' e- ]直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持4 Q9 O+ b) L+ h' _
+ l- i; t) J' N8 M a9 W
我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了
' }8 ^# }1 f L1 L- l6 {4 U
! k3 a6 [& }6 K+ b6 O: X4 e方便实用 简单 何乐而不用呢- 9 N$ D- }. ?9 ?2 O, D0 G( b
- <?php
2 @3 F( N$ ^6 s) W - ( u% @. C* [+ y- M
- define( 'ABSPATH', dirname(__FILE__) . '/' );
, N. q2 d" R# A, L/ Q+ D - require_once( ABSPATH . 'wp-config.php' );- u2 _- z o& u; W2 \) {+ p+ i
- require_once( ABSPATH . 'wp-settings.php' );
6 O0 x* J, _- y9 X( e - require_once( ABSPATH . 'wp-includes/class-wp.php' );
- m& O3 ]4 A) ^4 L/ M - require_once( ABSPATH . 'wp-includes/functions.php' );% L' k$ }+ n$ L* ]- [5 L0 i# _8 j: H
- require_once( ABSPATH . 'wp-includes/plugin.php' );4 \. y9 r- v& q2 m
- $title = $_POST["title"];: \& w/ v; A" H: H
- $content = $_POST["content"];
& n; g; w2 r1 a* m- Q( S - $tags = explode("_",$_POST["tags"]);
" |! T2 ?3 D8 g2 `3 z - $cate = $_POST["cate"];, E& t* P+ \5 \6 f4 ^* D
- //print_r $tags;6 ^( H* i) d' u
- //8 b/ h% v" ] e& Y" K
- //1 o. T) O; S; R8 F) [
- $wp = new WP();9 K; W. V: S7 [# q$ X$ B0 ~/ B
- $wp->main();
6 _. }+ `& z& U& L, t# I; a - //
- F+ l1 I3 g. s1 b - $my_post = array();
- |0 j- t* y2 v% F( p - $my_post['post_title'] = $title;
1 S. s( V& E8 g - $my_post['post_content'] = $content;; ^" F( W4 G4 x( i: |7 e
- $my_post['post_status'] = 'publish';( _; w1 J7 v; W! U* j3 s
- $my_post['post_author'] = 1;; j' O% ]- D$ h1 Y
- $my_post['post_category'] = array($cate);
) X! N2 F" F% P- X* l7 |& v - $my_post['tags_input'] = $tags;
l' c1 l( G! E2 l0 V2 D" V - //$my_post['tags_input'] = array('tag1', 'tag2');
/ J8 Y/ `+ J0 y6 {& P# r# \. h' { - //$my_post['post_status'] = 'future';
9 |' l2 k9 D8 _' {0 R - ////$my_post['post_date'] = '2010-07-04 16:20:03';8 A, ]* S" J! T$ s3 ~
2 S7 N( u( r0 A$ L' |4 H! {/ N- // Insert the post into the database
! ^% W( m: @& I - $ret = wp_insert_post( $my_post );
# L& a" h3 x4 Z - echo $ret;4 u" v2 Z+ H8 D& s- E
- ?>
# L) h/ y9 o9 C8 W5 a8 G2 f( {
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气- 3 g& d- ?% t0 J5 ^: K5 a% A" x! S
- <?php& I9 t! x; B9 b5 d
- define( 'ABSPATH', dirname(__FILE__) . '/' );" v1 t+ q ^8 e$ l* t+ p. ]3 U& N
- require_once( ABSPATH . 'wp-config.php' );
1 }; R; U- l% k1 L, m) p7 o - require_once( ABSPATH . 'wp-settings.php' );
/ _7 n! i5 }0 q2 A$ W& N+ c - require_once( ABSPATH . 'wp-includes/class-wp.php' );
% N# w8 K1 k" T. ^& v3 ^% I5 Z - require_once( ABSPATH . 'wp-includes/functions.php' );
4 O p- v4 F$ p3 i. E6 F9 i - require_once( ABSPATH . 'wp-includes/plugin.php' );; E7 K& q# l% T
- $pic_name = $_POST["pic_name"];
/ I5 `& M$ N! P9 h' I5 b: B - $pic = $_POST["pic"];4 d+ \! e4 c% v+ E% |
- if($pic != "" && $pic_name != "")
5 J4 c# y. J$ z0 B - {
7 J0 V6 p/ h* x M. ]7 s - $path = "images/";
0 G4 ?& S+ _* ^" ` - $pic_name = $path.$pic_name;
* M3 C8 L2 Y/ ^/ W6 } - echo $pic_name;
6 k/ a: @5 l* q1 J. w. q - echo "||";6 X% \* u5 U5 A j
- error_reporting(0);2 a" ~, {/ K4 Q6 N) E6 e" n
- mkdir("images");
, W5 K5 k. E2 F; X - $handle = fopen($pic_name, 'w');' D7 x: e8 ^. X( V$ ?7 F5 o3 c
- echo fwrite($handle,file_get_contents($pic));& \# w8 P$ W: n" J9 \7 `& `
- }6 x8 a8 u5 a" Q* }: |
- 0 J8 S9 v0 ^8 |$ r8 t8 t. p# V
- $title = $_POST["title"];1 b! T2 k! D( p" V- Q$ ]
- $content = $_POST["content"];
+ d! ?6 m( C- _& M( a - $tags = explode("_",$_POST["tags"]);
1 Z- ?4 @' g9 z) Q* {' t4 a* `" J - $cate = $_POST["cate"];) h+ |; ~: }& C1 k" ?8 ~
- //print_r $tags;
6 g( p X2 S; f% e E( g/ u - //# y, y* q9 s( v. g6 P' v
- //5 W' c0 N5 @2 f4 V
- $wp = new WP();6 ?" m. ~' z0 b* p/ C
- $wp->main();# w7 U7 L$ D- G, j& m
- //
; W8 l {' L' n% Y - $my_post = array();$ z: U- j; i. {* j/ ]) L4 n: y
- $my_post['post_title'] = $title;
" e; M/ w, k9 B, x3 D - $my_post['post_content'] = $content;) R; C1 ^& o- q6 G" p4 P/ n
- $my_post['post_status'] = 'publish';
! E6 \% z! r' t8 o - $my_post['post_author'] = 1;( V9 Q+ b0 _8 r6 N- V/ E9 a) Y
- $my_post['post_category'] = array($cate);
$ A5 a9 h. }% N - $my_post['tags_input'] = $tags;
4 W+ @- C; H8 r. |8 z: @5 ]2 y+ ~1 _$ Y - //$my_post['tags_input'] = array('tag1', 'tag2');, p! \) ~4 z& c6 h! m3 l
- //$my_post['post_status'] = 'future';
5 Q3 X `# m) E; |/ \, q' C - ////$my_post['post_date'] = '2010-07-04 16:20:03';2 O: Z$ F) N0 v8 V& ^1 d
1 H' f8 H% |# b8 V0 B; V- // Insert the post into the database0 x% V! K! O; Y- V9 O7 d
- $ret = wp_insert_post( $my_post );
% |: w4 W) @# @7 A( z - echo $ret;
' g1 ~1 I# c. S& T3 X - ?>
4 I$ J9 v, z6 G( [# }0 p
复制代码 我自己写的
1 D8 y9 y3 t8 m+ {. g插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|