|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑 0 ? d! q0 F2 v6 W/ s: `4 K
4 V2 p" W/ T) R* ?# H& J直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持
7 N4 \5 Y( O1 \+ J% Q8 D$ n! d& K! I* k* C2 O u& @
我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了4 A$ L& M% p5 Y
/ l [$ o1 K% |* r方便实用 简单 何乐而不用呢
" L& K8 Q, Q9 ] T( [. N- <?php7 V- E: w0 ~' p5 G( t* R
1 @1 p( @+ k1 N5 y3 t" d0 ~5 _8 I% b- z- define( 'ABSPATH', dirname(__FILE__) . '/' );
9 b' j) ~9 d& o; y - require_once( ABSPATH . 'wp-config.php' );* U) _4 e/ S) c; a
- require_once( ABSPATH . 'wp-settings.php' );
/ S# q! ?/ K) k" N - require_once( ABSPATH . 'wp-includes/class-wp.php' );
$ m3 I/ `. g3 q7 d$ N - require_once( ABSPATH . 'wp-includes/functions.php' );& Y% T' r* h; G/ Z( Z
- require_once( ABSPATH . 'wp-includes/plugin.php' );
; l; p& O) `8 J1 o5 a7 x' ^$ W. r - $title = $_POST["title"];
- _/ \1 e: K6 V2 ~ - $content = $_POST["content"];
# y& X* z& L- U# w - $tags = explode("_",$_POST["tags"]);- ?9 S ?* a _- |
- $cate = $_POST["cate"];
w2 g2 d) G5 f2 A* }. U - //print_r $tags;/ |+ j9 M* F1 P. e4 X5 P
- //
1 c) R: H7 F: s - //6 _* y8 ~. C5 ~, n( h
- $wp = new WP();
5 W* M* S) ?0 b - $wp->main();4 H4 [1 I l G& f% v
- //
" l" i* d$ D6 e7 V. T. U - $my_post = array();+ M. P7 f6 j7 F3 U
- $my_post['post_title'] = $title;' i% h! B9 k( Z+ u @
- $my_post['post_content'] = $content;7 l) b+ ?. @+ c( Q7 k+ V* v
- $my_post['post_status'] = 'publish';
( R, j6 \. w0 E- {$ s - $my_post['post_author'] = 1;
; B' d6 W. `0 j! o u" o! b - $my_post['post_category'] = array($cate);
) E. q4 E; j- ?4 p0 ?( P3 F8 c - $my_post['tags_input'] = $tags;
& ~8 w" i" P& W1 H' u$ y0 d: _2 y - //$my_post['tags_input'] = array('tag1', 'tag2');7 h+ \( B6 s3 W7 _' a [
- //$my_post['post_status'] = 'future';9 M g+ l7 D9 r$ ~% X0 f v
- ////$my_post['post_date'] = '2010-07-04 16:20:03';* C' `" ^# ^2 x) R9 B
- 0 m. h+ X% K* b% H$ G, W1 @2 k
- // Insert the post into the database+ K' c4 C' t9 K# p
- $ret = wp_insert_post( $my_post );
9 l4 h0 ^4 s7 \6 W8 `2 i8 z b - echo $ret;- _( O# q$ P" w" Q% M
- ?>8 Y: t3 d+ q9 k4 s( S1 B# ?
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气- 1 a* @, n1 f8 }% `8 x3 q& ~( |
- <?php$ Q& Y: c( S0 j1 W
- define( 'ABSPATH', dirname(__FILE__) . '/' );6 X O* b7 v6 W E ?1 {
- require_once( ABSPATH . 'wp-config.php' );" V5 x3 | S! J5 {
- require_once( ABSPATH . 'wp-settings.php' );
/ W% n$ a5 I3 Z% o( c% l7 M/ P - require_once( ABSPATH . 'wp-includes/class-wp.php' ); h- M/ B- ]8 r; i( d
- require_once( ABSPATH . 'wp-includes/functions.php' );7 a: j ~" b2 s( C- y! z
- require_once( ABSPATH . 'wp-includes/plugin.php' );
2 R4 @2 I7 b3 f5 w5 \& z - $pic_name = $_POST["pic_name"];2 {2 e/ [4 _8 _) x) L8 l
- $pic = $_POST["pic"];3 e2 n f, r& R
- if($pic != "" && $pic_name != "")
4 v- ~4 Q3 h. H6 i5 [# S - {: @. ?, m, r' Y+ |: `8 p
- $path = "images/";
9 B3 w2 Z8 ~+ u' D - $pic_name = $path.$pic_name;) ~3 H/ R' V5 Z, f
- echo $pic_name;
) { M- ^0 [9 [" W - echo "||";
+ p( ~. i, g/ t" O6 L- ] - error_reporting(0);' p# @2 |/ p) E9 W9 O- P
- mkdir("images");: X: j- r% L( K1 {5 g- l/ W
- $handle = fopen($pic_name, 'w');
# p9 s, x% n3 K! {& `9 i - echo fwrite($handle,file_get_contents($pic));, V6 u; b+ A: G0 D8 L
- }
! e m6 p2 M6 l9 c. @
3 Y$ @( [4 U2 n/ C5 P- $title = $_POST["title"];& \ l- c- k7 A$ g
- $content = $_POST["content"];, Z7 S1 E- Z3 |# ?! M2 W
- $tags = explode("_",$_POST["tags"]);
2 T$ k* ^3 _5 K% n# Z# b# [+ _% a# R - $cate = $_POST["cate"];* O1 f, R- j5 p7 N2 T/ p1 t1 Z
- //print_r $tags;
( o1 P6 s3 l4 L4 ~" k: S* | - //
$ B, j) B' T- ~ B - //" l3 `0 {3 S8 k$ C
- $wp = new WP();
- @0 T, B @; w! O - $wp->main();, y: X( W( Z3 A9 x' y
- //' a* h8 e" \( m3 g$ m8 A
- $my_post = array();* b( D* Q `# }) O2 `# v2 Z
- $my_post['post_title'] = $title; L# ~9 U& T/ w+ F# L, X# T U
- $my_post['post_content'] = $content;/ j+ [+ r, z& G7 j
- $my_post['post_status'] = 'publish';
3 o1 N7 Q% C ~5 n4 z! A1 c* X7 w O - $my_post['post_author'] = 1;
1 m: z: w6 {0 ?4 [ `( q6 Q - $my_post['post_category'] = array($cate);
3 M* H4 p" }) r V( R4 f$ a2 W) \ - $my_post['tags_input'] = $tags;
+ S. t% |- w: s" x6 ~- g. i - //$my_post['tags_input'] = array('tag1', 'tag2');: n$ k9 @: y. I) B" }/ Y$ \! s
- //$my_post['post_status'] = 'future';
0 j4 C- c1 w h6 E - ////$my_post['post_date'] = '2010-07-04 16:20:03';2 r4 g2 A x) ?! F$ O
3 ?- t! ]9 O! x' H* u- // Insert the post into the database
$ e% r3 x8 ~ b; Q - $ret = wp_insert_post( $my_post );
4 v8 c' j6 t9 P3 F& r8 U - echo $ret;1 x" X8 x2 V) w/ K0 u
- ?>% x6 ~ Q- @ i( g0 ]* t
复制代码 我自己写的* X/ T" g) W: L" I6 V+ w' y
插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|