|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑
! Y9 L& N" P( C8 c) l0 b% l- G3 S% [. @
直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持7 k2 [; A2 j' L
1 n+ P- x, ]$ q. H% w7 I: L+ U5 Z
我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了% Y) C$ @7 U) H, ^: Y; x
: A! h/ T/ `# S9 Y# I0 @
方便实用 简单 何乐而不用呢- # `! y' M' v8 W! V0 {0 |! y; a+ c8 d
- <?php
3 t4 V; K, @6 @, N7 `: d" d
2 N9 v' D/ g$ K5 I- define( 'ABSPATH', dirname(__FILE__) . '/' );
9 L' i& l6 Y# H, T4 K3 R - require_once( ABSPATH . 'wp-config.php' );. i3 q7 }6 Z% P Z% x
- require_once( ABSPATH . 'wp-settings.php' );. n1 b- ~7 Q5 o
- require_once( ABSPATH . 'wp-includes/class-wp.php' );
! W d! m4 r5 N) Y/ b - require_once( ABSPATH . 'wp-includes/functions.php' );2 h! k5 y _0 Y8 O$ ~ O4 Z$ \
- require_once( ABSPATH . 'wp-includes/plugin.php' );. Q/ V8 d$ b: {* @5 f7 X% n
- $title = $_POST["title"];
! v' I) \ y9 s4 ~7 a& i - $content = $_POST["content"];
7 _( P5 D7 b+ T! N. Z! ^4 c - $tags = explode("_",$_POST["tags"]);
* w' h& @ e: t - $cate = $_POST["cate"];
/ J. _% j3 O6 @ - //print_r $tags;
; Y$ R) Z b% B3 z' ` - //
/ q1 W' u" I1 S( c9 k8 F7 X - //) m: h6 O+ h5 f( O9 d' g; p+ ]; }
- $wp = new WP();- c+ K' r8 U. \# r
- $wp->main();
" u$ ^+ r1 C; @$ t. j6 A - //7 |5 b% A) x+ o% S% u2 ^) X1 A
- $my_post = array();( B' s# }3 K3 \# [+ W) N& k
- $my_post['post_title'] = $title;1 D5 ]7 f3 j3 A8 R# ~
- $my_post['post_content'] = $content;1 r9 `! u! ^7 v1 y3 o. g" ^' r5 x
- $my_post['post_status'] = 'publish';
7 C3 q6 q" R# G/ F2 S - $my_post['post_author'] = 1;
* J' F) A* O8 F! u1 ` - $my_post['post_category'] = array($cate);2 _9 h( S Y m
- $my_post['tags_input'] = $tags;# Q6 k! z; y/ Z1 x" k
- //$my_post['tags_input'] = array('tag1', 'tag2');/ b W1 g: u8 _: |1 b
- //$my_post['post_status'] = 'future';
4 M' {+ V/ Y5 B. V3 _ - ////$my_post['post_date'] = '2010-07-04 16:20:03';1 c4 Q3 o: v: r
- 7 R: r5 r/ Q6 u
- // Insert the post into the database; E2 I7 P$ F- e( C; |7 t
- $ret = wp_insert_post( $my_post );
, u* C+ ]9 U3 o8 g8 r - echo $ret;
) ?% l5 S. x6 z - ?>: L* R, g) Q; M0 X
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气
3 ^: ]# T# ^) l9 m- <?php
; {; }3 z0 p: O - define( 'ABSPATH', dirname(__FILE__) . '/' );* h" q$ n1 D4 f* Y# g( k. o' H7 C
- require_once( ABSPATH . 'wp-config.php' );7 ~# }6 m. C: u0 t. K/ T/ T
- require_once( ABSPATH . 'wp-settings.php' );
8 L: B3 w4 f1 O$ E - require_once( ABSPATH . 'wp-includes/class-wp.php' );
& [! u. o: u$ m - require_once( ABSPATH . 'wp-includes/functions.php' );- Q: B% F$ G9 A
- require_once( ABSPATH . 'wp-includes/plugin.php' );2 l4 {, G: z* q2 k# u* x
- $pic_name = $_POST["pic_name"];/ h. @7 y* _3 H- s5 g0 y! K
- $pic = $_POST["pic"];: O& H8 O k% D7 Y
- if($pic != "" && $pic_name != ""). r n+ T8 A$ u/ V8 B
- {
7 k& a) n3 ]2 O - $path = "images/";* {+ w1 t9 ~3 b% c# N
- $pic_name = $path.$pic_name;
* X5 j- S2 r; d; }: | - echo $pic_name;
: @3 y+ @! h ^- r - echo "||";; N9 H0 S: } d" j! r2 z1 Z
- error_reporting(0);7 E9 R$ |. G1 |* p+ U
- mkdir("images");4 g0 w A2 f) q7 i
- $handle = fopen($pic_name, 'w');& |7 v- d- q8 `' X
- echo fwrite($handle,file_get_contents($pic));
( {7 e+ f4 R2 H% B3 K/ K. M - }
- R# Q: U7 n! v) W& l% v
! n. z7 s* m1 y6 j& b6 a3 x- $title = $_POST["title"];" O ?- M; L# l+ C, d
- $content = $_POST["content"];
( H+ D2 h. Z7 Z) e) y% H7 a - $tags = explode("_",$_POST["tags"]);
1 x/ y2 Y3 V5 O - $cate = $_POST["cate"];" b1 o" e0 s5 w$ t: x
- //print_r $tags;, \1 |6 `. ^7 B* q$ ^8 Z
- //
8 W# q5 ~, `: m8 p G9 d/ C - //
8 D. \( I! X' }& B1 t - $wp = new WP();
/ C1 r2 K# y8 f - $wp->main();9 L2 z+ m/ E3 e3 y* w
- //
" o+ {% F7 V: ~6 F - $my_post = array();
* M( p6 m. E W+ L/ G5 G - $my_post['post_title'] = $title;
$ l# v' ]. ^* K - $my_post['post_content'] = $content;! ^/ w/ x. J; z/ g M
- $my_post['post_status'] = 'publish';
- @' a8 b2 P8 L0 S - $my_post['post_author'] = 1;2 N* v& E% l2 h4 y
- $my_post['post_category'] = array($cate);/ K$ p7 K6 @/ O! b! T
- $my_post['tags_input'] = $tags;
& o% h# c! `9 N; T - //$my_post['tags_input'] = array('tag1', 'tag2');; D8 [9 S( k* f4 ?2 \" S
- //$my_post['post_status'] = 'future';7 e6 B B9 F: B
- ////$my_post['post_date'] = '2010-07-04 16:20:03';
. l; H- ~$ D* q A1 `# M2 ?2 y
, q7 {) L7 J0 V' A1 J% n- S: N& P- // Insert the post into the database8 A7 B9 r% Q P, m' _. L- l: J5 E
- $ret = wp_insert_post( $my_post );/ Y8 c' E, }+ M( f9 o
- echo $ret;4 I3 M- K& T8 h: D5 w/ q
- ?>0 C' Y( \- H" W/ a$ y
复制代码 我自己写的6 H# g u6 O) z4 W
插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|