|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑 9 D! a# x& w' s5 Y
7 S& w/ g q. a1 S W直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持
& ~$ ?6 E; X9 P; H/ h
8 A/ i* K3 Q6 H. ?' _+ W- q$ |我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了
, y5 K# m: g* r$ v; ]+ i
0 c' y: ?) ]4 Q' r" ^方便实用 简单 何乐而不用呢- 4 x5 o- X, E6 i) u# q4 C0 c F! O, C- S
- <?php
) h( G+ [/ i# q+ Z1 @ - , }4 C: A5 W5 F# Y" E" U. h9 e
- define( 'ABSPATH', dirname(__FILE__) . '/' );; g+ R" ~9 ~- X$ Y0 @
- require_once( ABSPATH . 'wp-config.php' );
4 }5 D1 R4 w! _ - require_once( ABSPATH . 'wp-settings.php' );# I1 M7 [. ?1 E8 J( C/ ]& |
- require_once( ABSPATH . 'wp-includes/class-wp.php' );( Y* h( l% q p
- require_once( ABSPATH . 'wp-includes/functions.php' );4 M5 `, X) \# U
- require_once( ABSPATH . 'wp-includes/plugin.php' );, Q' ^3 K$ Z- v& l, N
- $title = $_POST["title"];& p& ^" M d. t; c6 c; c) Q; E8 J8 }
- $content = $_POST["content"];
8 h6 f) W( @& ? H - $tags = explode("_",$_POST["tags"]);
6 Y, @5 Y. T5 h5 {2 U* M( { - $cate = $_POST["cate"];/ }4 p( }2 {, Q3 k4 M- f* I, Q& P
- //print_r $tags;
9 R* q/ R, q! G- ?( V0 X - //# h" m2 k: F& B* Y$ H
- //
7 @9 o+ i' h$ d1 z$ Y6 T, G% x X - $wp = new WP();
4 x5 O6 h9 e) A5 e( ?; ^( c( S& R - $wp->main();
* T, L0 H7 [' }$ e7 d - //- _# o: p/ ^6 h# x% E2 k8 ]
- $my_post = array();; J ^8 t) n1 }: X
- $my_post['post_title'] = $title;' z" `! p* @2 A/ u, j4 u
- $my_post['post_content'] = $content;
8 s. H% X5 T! N7 P5 B/ v - $my_post['post_status'] = 'publish';9 z+ n' N0 C/ c8 Z9 F
- $my_post['post_author'] = 1;7 ~1 T' O& O5 L; a- D- h
- $my_post['post_category'] = array($cate);
0 g3 k. J* U2 R$ I6 E. o4 t( d - $my_post['tags_input'] = $tags;5 P4 z; |4 E4 |. }
- //$my_post['tags_input'] = array('tag1', 'tag2');
: W- r( Z; P* w$ J - //$my_post['post_status'] = 'future';: W5 V$ v7 i d) H* Z
- ////$my_post['post_date'] = '2010-07-04 16:20:03';3 z- \% I* @+ n& I. o3 H3 C; s) o. z
- 5 ^- U* w6 S9 t$ Z9 R6 e& ]
- // Insert the post into the database
( i3 H' h5 c: A - $ret = wp_insert_post( $my_post );
& h; R0 q+ t \& p, N - echo $ret;1 e' j- l/ D7 Y
- ?># U2 D/ F; [+ O5 p2 |$ H: V6 f
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气
2 E' Y; O1 h* Q, W) a) V* u' J- <?php
" P) F+ e( l& ^/ ] - define( 'ABSPATH', dirname(__FILE__) . '/' );
% u/ Q" f/ M0 a/ ~- e: l! R" | - require_once( ABSPATH . 'wp-config.php' );
M2 d" R4 S! P6 T2 c - require_once( ABSPATH . 'wp-settings.php' );
/ Y8 z0 b3 `1 @ - require_once( ABSPATH . 'wp-includes/class-wp.php' );0 A: I1 d- E1 j) K( x$ k
- require_once( ABSPATH . 'wp-includes/functions.php' );
" v% B% E6 O1 q4 N6 Q. n. J - require_once( ABSPATH . 'wp-includes/plugin.php' );
0 F9 n" p" [! m* i' _3 @) z - $pic_name = $_POST["pic_name"];# v' v) B) Y$ b! L3 n
- $pic = $_POST["pic"];
$ j& n( [0 [9 v- f9 R - if($pic != "" && $pic_name != "")% e/ i2 n; Z3 @
- {+ _. O5 Y$ E# V: h2 B, J
- $path = "images/";
! U# o0 s1 e1 ~ - $pic_name = $path.$pic_name;( ?* ~+ R) t$ j* L: [
- echo $pic_name;
- ?5 @4 g- P: L, c - echo "||";
7 D+ y( p1 F7 R" r6 @ - error_reporting(0);! l5 c9 w/ J# P( v- Q7 B! S
- mkdir("images");% _8 |, e# E. R5 |
- $handle = fopen($pic_name, 'w');1 P0 n; N/ `7 r6 C4 ?" q. V
- echo fwrite($handle,file_get_contents($pic));
8 ^; c2 K; s# X( q$ u3 I - }
, H: |0 V; v! N1 N% l+ j
: D2 A4 R: ]0 A: n/ [- j0 l- $title = $_POST["title"];
( Y1 U9 j2 A8 D - $content = $_POST["content"];
+ T% d' Q& L1 E6 z - $tags = explode("_",$_POST["tags"]);/ E% b1 i6 ?: W {% c
- $cate = $_POST["cate"];' F3 x& T( D. R" f$ A
- //print_r $tags;
" `2 B& I! l9 d - //
3 ^9 a; M0 E- k4 N2 | - //
^% H3 k1 u) y. N - $wp = new WP();
6 S' }$ E& W6 p. j7 ] - $wp->main();
3 E( Q5 }! f- A. V6 F* s2 r. h - //! Z; F6 \+ u3 x |$ J" o
- $my_post = array();
+ n- E4 a+ r1 y$ |; q4 _& q - $my_post['post_title'] = $title;, b3 S( n) [- m
- $my_post['post_content'] = $content;
2 |# ]8 `; y) |! ^, f; C' }' u - $my_post['post_status'] = 'publish';* t6 p* V% C/ s7 h9 l9 R
- $my_post['post_author'] = 1;0 C1 t) {7 S/ {! q( u
- $my_post['post_category'] = array($cate);: T) U+ W- D, ?- f5 S/ X
- $my_post['tags_input'] = $tags;
0 _/ C" x) a1 ], k* ]! x7 }* H; g: G - //$my_post['tags_input'] = array('tag1', 'tag2');) ~2 ~& S3 B* ~, E, Z! D, Z
- //$my_post['post_status'] = 'future';. ~+ \9 O; c" E8 c/ ]! q
- ////$my_post['post_date'] = '2010-07-04 16:20:03';
+ K' R/ k2 [4 p0 w
: y6 L7 h8 W& S7 @+ _7 [* K4 J- // Insert the post into the database
: N3 `4 `# Z$ A) N - $ret = wp_insert_post( $my_post );+ d9 p& q- u {5 J4 Z
- echo $ret;
, [" E+ P: K+ p! n5 Y8 c - ?>6 x6 T. s" K& T2 {
复制代码 我自己写的9 F( A6 E; X, s& q- N# A d
插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|