|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑 9 t: v) E1 [+ H
: p' U& j+ s+ ^; ]
直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持
( s" r* R6 a8 s" k* z2 b2 Q/ u+ Y6 j! ^1 Z" h7 `+ i
我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了
- t1 O' _/ P! A, C5 y2 k
, ]+ k i9 }1 j9 @2 l* m6 Z方便实用 简单 何乐而不用呢- 3 u( Q2 y( H# _ w* q; e
- <?php5 ?6 M X9 n8 m: e! M- m7 t
: R" Y0 b# `" Y5 T& n# R; x- define( 'ABSPATH', dirname(__FILE__) . '/' );" r9 v/ Z3 P' H9 _6 y3 b
- require_once( ABSPATH . 'wp-config.php' );2 W) E M" |) y' u* V! {0 O
- require_once( ABSPATH . 'wp-settings.php' );
" N: B) ~% Z0 ~/ c7 ? - require_once( ABSPATH . 'wp-includes/class-wp.php' );7 G. S9 m2 B% `0 v" O
- require_once( ABSPATH . 'wp-includes/functions.php' );
% ~2 ~+ e* h: { - require_once( ABSPATH . 'wp-includes/plugin.php' );& b1 W. i* S6 n& @3 l
- $title = $_POST["title"];: K p( v8 X# W8 [7 Z
- $content = $_POST["content"];
+ j: ^ @$ x M1 M - $tags = explode("_",$_POST["tags"]);
0 Z" K. P# B5 V. K8 L$ F - $cate = $_POST["cate"];. J- N! r3 |% s3 G
- //print_r $tags;/ J' H k4 ^7 P) Y% a3 v+ i9 F
- //
5 }8 j2 P* o* @* [, T+ z0 b - //
6 ?7 ~. H3 V, B" P) N2 R - $wp = new WP();5 L& I8 G/ o Y2 x3 T: ^- O
- $wp->main();
1 Z8 U; L2 j, V: u. O/ ~* X - //8 c0 \# }9 K2 Y# J% ~1 ?: q
- $my_post = array();
$ |( i1 B- b3 G; v! Z- K* H9 p - $my_post['post_title'] = $title;
' g/ N& \1 w% G5 e S; t - $my_post['post_content'] = $content;8 C. S' j1 y( ]. r) t8 R
- $my_post['post_status'] = 'publish';
( Y! \; I) y6 w; o1 c) T5 W8 P - $my_post['post_author'] = 1;
% _/ j0 C0 b$ L4 c, Z" G - $my_post['post_category'] = array($cate);" d: ^* W! e* R# |( P
- $my_post['tags_input'] = $tags;
4 ]& R8 e) j& \; r$ W) L6 T - //$my_post['tags_input'] = array('tag1', 'tag2');; a1 q: L1 u& m3 i8 A( p5 {
- //$my_post['post_status'] = 'future';9 y# B; l& _$ m+ ^
- ////$my_post['post_date'] = '2010-07-04 16:20:03';- B( _* w/ P9 G! Q
- / m8 X; V8 Z0 o
- // Insert the post into the database
" ^2 R" f+ V9 Z' D/ Q7 M% u - $ret = wp_insert_post( $my_post );) [' K% g) g+ K) H( k+ }
- echo $ret;
0 \# P9 k$ a: [$ M - ?>
! `4 Z0 x- L6 _* l
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气- 6 b7 ], a8 R1 O
- <?php8 H5 W9 O: y: k4 j5 m; q7 z6 i: ~
- define( 'ABSPATH', dirname(__FILE__) . '/' );" ]5 A {" h! G! R, ]
- require_once( ABSPATH . 'wp-config.php' );
, v/ M. M* @9 Y; }& c - require_once( ABSPATH . 'wp-settings.php' );
! D h9 A% f9 H3 E! R {: z | - require_once( ABSPATH . 'wp-includes/class-wp.php' );: T5 u; E% K9 P4 K8 [! Q
- require_once( ABSPATH . 'wp-includes/functions.php' );9 j. v3 m' l3 z4 `3 n [" \
- require_once( ABSPATH . 'wp-includes/plugin.php' );
1 y2 |/ ]4 T# Y+ s: V - $pic_name = $_POST["pic_name"];0 `5 f9 |2 h! A6 s& N! B* d
- $pic = $_POST["pic"];: c$ A* o ?4 f9 k& I7 K
- if($pic != "" && $pic_name != "")$ l" k: K" r7 [/ r+ V: P: p
- {
: J+ K& C3 L5 f' H3 ? - $path = "images/";
6 k) Z4 ~2 r$ L5 _3 p, z# a" n - $pic_name = $path.$pic_name;
& g; F% z; O' n8 { - echo $pic_name;
+ v( W8 q9 _# S% w* f' Z$ Q - echo "||";3 t2 [8 z9 ?5 n$ d: F" R2 ]
- error_reporting(0);
) f+ n1 s+ p) O% l! Z, S) o - mkdir("images");
/ Q5 g& m/ B k8 f7 |. u3 T% J - $handle = fopen($pic_name, 'w');- x5 c3 E+ v8 O& D7 Y0 [; v9 Q6 r
- echo fwrite($handle,file_get_contents($pic));+ T- D3 n' k9 Z8 p+ p; M- Y2 y
- }
. A/ g0 I) K: R- E
" j3 X R+ F( E" p- $title = $_POST["title"];
" o, E: ~6 l( A8 ^ - $content = $_POST["content"];
$ f e+ N! u7 ^2 C - $tags = explode("_",$_POST["tags"]);/ L5 l& I6 D" n1 R/ n4 ?
- $cate = $_POST["cate"];
, @- f! ^; r% H% m% K - //print_r $tags;0 o2 E( ?; w' a: U, X( Z
- //: L4 w- B# B4 _* A: o0 ~$ y
- //+ Q" W3 |0 G3 p# m
- $wp = new WP();4 T9 Z5 Z% y% Y1 @# @" E2 g
- $wp->main();+ Q/ l a# A) [1 z# q! o
- //8 p; u' c3 P$ e8 j, a
- $my_post = array();. y7 ], v3 ?% @$ S I0 Z4 A+ L
- $my_post['post_title'] = $title;
1 M9 b0 W) s5 Q) N! c% q) `9 w - $my_post['post_content'] = $content;2 k9 i+ u$ h' W# {% s
- $my_post['post_status'] = 'publish';
: H' y6 y- v8 L% a$ C - $my_post['post_author'] = 1;: f% G- c" K# M+ O4 t
- $my_post['post_category'] = array($cate);& V% l s1 s# ~# j" q! b
- $my_post['tags_input'] = $tags;
5 B b. i; {' F) l. G( ] - //$my_post['tags_input'] = array('tag1', 'tag2');7 ]* @5 i& n) B5 [; A- z
- //$my_post['post_status'] = 'future';
! w0 Z0 q$ Z; y/ B, r; j- Q0 v - ////$my_post['post_date'] = '2010-07-04 16:20:03';3 S6 c# B: g4 n1 R# ^
: i/ u2 A% ?) X/ [8 u1 p5 p- // Insert the post into the database
5 b0 S- W/ Q4 e# i - $ret = wp_insert_post( $my_post );7 W& v5 Y0 L$ Q( @
- echo $ret;5 C3 y0 m! M: V( ]5 @
- ?>
- t- q$ [, Y& C5 L; s( ]% L& a
复制代码 我自己写的
O/ U! [9 t7 e" b$ ]插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|