|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑 9 v( W7 A9 z% N( i6 s% }
* P6 F" m7 |0 Q" a直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持1 D( l* ?9 d) l' w4 B
A( ?% w2 m2 e; |$ B我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了
2 M* w1 h6 |+ R' I* ] w# @# L6 `: f( K& u
方便实用 简单 何乐而不用呢- 1 v( x# q5 V1 c! @
- <?php
% N, i2 i$ R' K- O4 D - 2 C% r9 q$ Y+ ?) y5 ~
- define( 'ABSPATH', dirname(__FILE__) . '/' );5 H& V% l1 }# P7 X8 Q
- require_once( ABSPATH . 'wp-config.php' );9 L. Z( w7 E3 L5 m, X! k
- require_once( ABSPATH . 'wp-settings.php' );
2 _$ o" u2 P4 O - require_once( ABSPATH . 'wp-includes/class-wp.php' );
9 Q# ^. R8 Q; x9 [4 D - require_once( ABSPATH . 'wp-includes/functions.php' );
& a) D d) m/ ~3 z0 u0 V! t! j - require_once( ABSPATH . 'wp-includes/plugin.php' );. J' h$ S; |7 s$ T+ F" P
- $title = $_POST["title"];. B" [4 t3 t/ }8 e4 l2 m; y
- $content = $_POST["content"];5 s i6 D! t) ^
- $tags = explode("_",$_POST["tags"]);
$ j* C) U5 Z8 ?5 B+ ?$ e - $cate = $_POST["cate"];1 b7 k: z2 r" r) o
- //print_r $tags;8 ]4 w7 m5 t. n
- //
- B$ h+ W5 L4 P, p4 y8 ~+ I - //! F1 ?7 G( e( P1 v5 j
- $wp = new WP();
, C O, R% L' o3 p1 Y; e - $wp->main();4 k" F! H: w% A; U9 p6 Q+ P
- //# ~. Y8 ~1 T- [5 l) i
- $my_post = array();/ D1 g+ \3 W- w7 _9 t* |. M
- $my_post['post_title'] = $title;
, u3 C" ?+ \; K$ S8 a1 k1 B - $my_post['post_content'] = $content;
# y3 @$ g; r I7 K. }0 ~+ S - $my_post['post_status'] = 'publish';
* x( j* L, B3 V - $my_post['post_author'] = 1;
$ `/ [8 ~; \* f - $my_post['post_category'] = array($cate);( Y' I4 O A: T5 d' o
- $my_post['tags_input'] = $tags;3 `$ y7 r" [3 q& s' v4 h4 W+ K* c
- //$my_post['tags_input'] = array('tag1', 'tag2');; p3 G) D/ U9 n# g- R
- //$my_post['post_status'] = 'future';
& f$ z& {( B0 f. a - ////$my_post['post_date'] = '2010-07-04 16:20:03';) w2 c8 t2 `2 A$ {- ?
" \. Z0 q0 G' p7 ?- // Insert the post into the database
* J, A# \5 ^" P& {. l2 y6 x2 U - $ret = wp_insert_post( $my_post );
$ D) D. \+ _9 j& C: h2 c2 Z - echo $ret;
( S* m- o7 N N/ _ V: k - ?>6 d) P5 V6 t9 |4 E7 z2 q# c' Z
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气- 3 g' P8 u h3 F6 o& n. J
- <?php
/ s9 |& c; s; m4 K4 n2 s4 e - define( 'ABSPATH', dirname(__FILE__) . '/' );- X) [8 z7 n7 X+ U" C9 N D7 r; G
- require_once( ABSPATH . 'wp-config.php' );
, h* l) B8 B# @9 b W3 q) ~ - require_once( ABSPATH . 'wp-settings.php' );
7 c, {7 e% G, o. z - require_once( ABSPATH . 'wp-includes/class-wp.php' );
0 Z# Q' W# i# _; i$ p" \. Q - require_once( ABSPATH . 'wp-includes/functions.php' );% v2 s! ~' ]4 k( Z( k! p$ F
- require_once( ABSPATH . 'wp-includes/plugin.php' );3 b8 [0 r+ o$ x) H1 k+ q
- $pic_name = $_POST["pic_name"];3 D5 a$ U/ O1 _
- $pic = $_POST["pic"];
# v) g# D9 B9 C4 D. j3 I' M! U8 n - if($pic != "" && $pic_name != "")
( _% [" G% u) f - {
' m/ \4 L) M# u! k9 r& X. c) [ - $path = "images/";% v2 v; h* q9 `! G
- $pic_name = $path.$pic_name;* U+ N" _9 z. d' B
- echo $pic_name;3 y% z- R; c! U: A
- echo "||";' l' ]- ]9 Z6 S# \+ w* b
- error_reporting(0);
/ m; E/ s5 X7 h# k( }; \3 ]' n - mkdir("images");
' {3 O' x* E8 ~ [9 } - $handle = fopen($pic_name, 'w');
$ Q% i: C' D5 {% H" x - echo fwrite($handle,file_get_contents($pic));8 o ?8 f' `2 r1 m5 a- q) O
- }1 O% J- R, d( y( r( b7 ?
) L4 C1 z: v/ P- o1 ?- $title = $_POST["title"];6 ~* {! K7 @& H- x, s( Z8 B
- $content = $_POST["content"];' i5 c4 t5 ^; W% C- t6 \% [
- $tags = explode("_",$_POST["tags"]);5 O# d4 L. w* l
- $cate = $_POST["cate"]; ^$ M1 P. ^) V" f; d+ \9 n5 }
- //print_r $tags;& E' U' V9 h5 R: d9 v
- //
, t8 g. P. i. y: v9 M7 l - //
- ]) `2 X; A5 \7 P/ B; N - $wp = new WP();2 f$ E! i" F/ {: O9 h
- $wp->main();( s2 h/ q& i: S* K$ k
- //- S) G2 F! @! M5 s
- $my_post = array();* d% G8 G* w0 y
- $my_post['post_title'] = $title;. l: s! [! w; \! L- `
- $my_post['post_content'] = $content;+ w$ W$ w. |+ e/ v
- $my_post['post_status'] = 'publish';) y* i& B, p, _1 g9 L, [ f
- $my_post['post_author'] = 1;0 h4 Q8 R) X) m6 w
- $my_post['post_category'] = array($cate);% u, d) K0 Q# [ b O0 j
- $my_post['tags_input'] = $tags;/ k& h# r" G1 u9 `/ o$ J$ W% ^
- //$my_post['tags_input'] = array('tag1', 'tag2');7 L/ o0 L$ z2 ]' A c
- //$my_post['post_status'] = 'future';
1 z+ s Z$ `/ v9 H) D - ////$my_post['post_date'] = '2010-07-04 16:20:03'; @- \' ^; j! O1 @! F' Z4 m6 i4 r
7 A! r* c! S2 W, ^! f' W- // Insert the post into the database7 [3 t8 H# g6 }$ \
- $ret = wp_insert_post( $my_post );: b. @2 ~$ }; W2 _$ k; d
- echo $ret;1 ~$ g; V* Z) X/ R2 C O& g% G: a
- ?>
! f6 I# d- C; R6 S, i7 M$ Z! L& b
复制代码 我自己写的
# u4 w& {# m) B插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|