|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑 0 ]$ v0 D$ d6 y
5 s* e* K% c2 d- k. O
直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持0 ?' l8 @" @, X$ t8 j5 b
) M7 N7 A9 x2 R( f4 V0 J
我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了4 O0 `# V9 V: F5 |4 N" A% p" f
6 N- i7 H7 Z! c: v6 n3 _方便实用 简单 何乐而不用呢- ! |* {) \3 V* _$ A/ l
- <?php" C w$ a3 y) c9 m2 Q7 N
- 3 c8 q3 @- S% D/ H( H
- define( 'ABSPATH', dirname(__FILE__) . '/' );' B: }2 C7 B+ m- }, p
- require_once( ABSPATH . 'wp-config.php' );
# {1 H( m/ b- i; I" _0 I - require_once( ABSPATH . 'wp-settings.php' );/ K7 |5 t" b& X( j; G
- require_once( ABSPATH . 'wp-includes/class-wp.php' );
3 z4 R) e: s$ }- c- ~+ @2 b - require_once( ABSPATH . 'wp-includes/functions.php' );
, W5 c3 P1 v* t, F/ z - require_once( ABSPATH . 'wp-includes/plugin.php' );% C, K$ W2 C) R% k* W4 ^9 v
- $title = $_POST["title"];
( r. _( M- S7 m - $content = $_POST["content"];
, t$ h3 y8 z5 J - $tags = explode("_",$_POST["tags"]);7 V* ^6 ^* y8 ~
- $cate = $_POST["cate"];
; w+ v. h: A7 P7 M V - //print_r $tags;. J# X! K# [ ?* J8 ?3 l6 [/ x' w6 d
- //
& K, `( W" e, H2 V& ^$ J+ w. e+ S - //3 d" S6 d7 Z. C
- $wp = new WP();
I; @: t0 H \! n/ v - $wp->main();# z- j3 s2 q: H/ X
- //
+ M8 Y: r* c3 C# ? - $my_post = array();
4 ]7 v0 A; _& T0 g9 v0 x8 A5 r - $my_post['post_title'] = $title;
3 u6 j( J6 e- d! z, i# f - $my_post['post_content'] = $content;7 n C5 ~& w9 Z) b- x( m
- $my_post['post_status'] = 'publish';5 W. \$ Z" ~% j8 k4 Q
- $my_post['post_author'] = 1;
E6 a3 |- q" X& x - $my_post['post_category'] = array($cate);
- _. i/ I% U* Y/ T( }& Z - $my_post['tags_input'] = $tags;
: w9 a, s9 K( p+ |+ r! v# H2 _ - //$my_post['tags_input'] = array('tag1', 'tag2');
1 M. e3 s( B6 O% Y) P" m - //$my_post['post_status'] = 'future';
( M% y! v- V! z6 o - ////$my_post['post_date'] = '2010-07-04 16:20:03';
' I1 w8 ?; G" i% X$ I. U4 L
! W, U, R. Q4 A: P- // Insert the post into the database
6 ` y3 Z0 H* D, e3 _2 r3 l - $ret = wp_insert_post( $my_post );
2 B' C) S: Q) ]* f$ [ - echo $ret;
7 ^9 e9 E+ q7 z2 ^% X5 i! P - ?>
( O& E' t" H0 V, s
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气
2 h. u% a- m1 h- <?php
: ^! y( ^( u a2 f N" q - define( 'ABSPATH', dirname(__FILE__) . '/' );
) d" \$ T5 A9 T, u - require_once( ABSPATH . 'wp-config.php' );
- G3 {/ b" z2 H) p - require_once( ABSPATH . 'wp-settings.php' );+ H2 D9 J& t! r( y$ M9 g4 b
- require_once( ABSPATH . 'wp-includes/class-wp.php' );+ \3 F# V4 m* p3 v! J) s, Y7 O2 O2 ?* K
- require_once( ABSPATH . 'wp-includes/functions.php' );
7 O+ ~# F3 i. U* {/ X" Y1 A) c - require_once( ABSPATH . 'wp-includes/plugin.php' );
5 @; R5 A8 F/ d4 H3 ` - $pic_name = $_POST["pic_name"];
* u* }& v: x9 ?' c; o - $pic = $_POST["pic"];5 e j+ J* t, ]- Q/ I
- if($pic != "" && $pic_name != "")
: R- z n' M2 F8 C8 a' c- R - {
5 k% c$ s1 |* r5 v* t - $path = "images/";" c7 P& ?9 c/ @) }
- $pic_name = $path.$pic_name;
( n2 P5 y# n% P3 P - echo $pic_name;& ]# o1 ?* O1 |$ `: j
- echo "||";# V8 H i- W0 I3 w2 \" k
- error_reporting(0);$ C- D7 c$ z p8 b0 y" N* a
- mkdir("images");
8 Y- o3 V9 C& l; B - $handle = fopen($pic_name, 'w');3 t3 W& K' S `1 M/ k
- echo fwrite($handle,file_get_contents($pic));. A: Q, n; i" {4 @; B! Y
- }4 p i/ V+ T5 d# W
, X, k1 Y( [ b- $title = $_POST["title"];
) ]; Q9 v3 F' T3 T6 [% y0 G" K$ }6 | - $content = $_POST["content"];
9 j$ i( \8 q) `, R. m - $tags = explode("_",$_POST["tags"]);
( i2 d! L/ c5 u: Q! x5 ~ - $cate = $_POST["cate"];
' K+ P8 |# a+ g3 _/ [ - //print_r $tags;# O2 S) h7 _, X
- //
$ D8 _. Y F$ ]# c0 r - //
$ X4 M+ y" G& e o1 i7 z \ - $wp = new WP();
* x1 X: C G4 U: e; L& Z6 J6 H1 K9 M - $wp->main();* x8 M: M5 H% J4 W
- //
; p; @; D( [ r, b0 }- v4 V$ ] - $my_post = array();% H- k( p3 Y# D# a
- $my_post['post_title'] = $title;6 _) k- y; F- T7 y" M
- $my_post['post_content'] = $content;& Y" V- Z2 r) _) e8 {& g
- $my_post['post_status'] = 'publish';7 p) C) N) A1 J9 X1 Y$ }
- $my_post['post_author'] = 1;
8 k s1 r- V8 X - $my_post['post_category'] = array($cate);3 A' m" f4 v. E$ K. E% M
- $my_post['tags_input'] = $tags;
# L2 l( n1 N9 h# v - //$my_post['tags_input'] = array('tag1', 'tag2');
: \/ H) L; J9 Z! N% {+ V9 A - //$my_post['post_status'] = 'future';
6 y. J( T1 D! f4 V& D6 u$ C - ////$my_post['post_date'] = '2010-07-04 16:20:03';
) n4 a' e! c r" G' m
0 b" b X A. l9 @- // Insert the post into the database
4 s+ F/ \3 x8 h0 [ - $ret = wp_insert_post( $my_post );
$ z' L' I3 z5 f( G# A d - echo $ret;
' l; V( M8 y6 |- i b - ?>
, I4 w3 a+ J; z4 i5 g' o+ a
复制代码 我自己写的5 b8 L* d; x3 h: c3 Y) r! M- ^; Y
插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|