|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑
% t+ h' b+ Z2 D) w7 R1 X
/ i: [: ]% F1 e+ e, h直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持
- J# i8 d o; E- Y' P+ \# w4 `7 h; ?9 u" q
我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了/ ]) U8 Z/ X+ q4 P6 ^- T& v# ?
* Q1 R: s7 k9 I/ K方便实用 简单 何乐而不用呢
* y" D' r: x0 W) h$ L- <?php( U. Q c" t3 r3 B3 v
- . {; y) O. S0 c% r" U0 g- F5 C
- define( 'ABSPATH', dirname(__FILE__) . '/' );
% @5 C9 l, c/ F0 `/ ]9 n - require_once( ABSPATH . 'wp-config.php' );
2 ~7 W0 A8 q$ }& {9 e - require_once( ABSPATH . 'wp-settings.php' );
+ [) m. ?* s* {! ?$ S - require_once( ABSPATH . 'wp-includes/class-wp.php' );
, m5 H, {* s. L; A# ~6 D - require_once( ABSPATH . 'wp-includes/functions.php' );
4 I7 o+ y* b! F b/ u - require_once( ABSPATH . 'wp-includes/plugin.php' );
+ P6 ~3 C( C. Y; D2 m5 P - $title = $_POST["title"];# m; F, j. @, n, F |) B
- $content = $_POST["content"];# o$ ] B, k9 D7 j7 j m0 r
- $tags = explode("_",$_POST["tags"]);
( K7 s$ n: w1 C# f4 ?. J4 ?: V - $cate = $_POST["cate"];
1 Q) s$ a( c# ~+ x7 u - //print_r $tags;2 h8 c$ u& _2 t5 J& A
- //
( o/ z' p! w, l/ ` - //' |! ^! Z: P/ Z5 c/ [) W
- $wp = new WP();
( P8 _. E: g. c - $wp->main();3 A% y: [" ~" P! ]% q2 C
- //
8 g" t6 M6 \" u! Y5 Z& ]: s, \ - $my_post = array();" k$ Z6 u. B7 B% `% h5 E6 O' x
- $my_post['post_title'] = $title;# |$ U8 T0 n) n( Y# O/ B
- $my_post['post_content'] = $content;
" n! D( r5 h5 W" S - $my_post['post_status'] = 'publish';
/ g' @* u/ m( l3 E+ p ? - $my_post['post_author'] = 1;
: P! L+ z$ [4 Y$ F7 ]0 H+ [+ z) p# d - $my_post['post_category'] = array($cate);; I# y0 `1 u. D1 r$ ?4 k
- $my_post['tags_input'] = $tags;
$ Q# g6 F6 X+ }9 _% } - //$my_post['tags_input'] = array('tag1', 'tag2');
8 k) S8 u) q1 K, z - //$my_post['post_status'] = 'future';
8 k* M; h* t4 C - ////$my_post['post_date'] = '2010-07-04 16:20:03';8 y$ p0 l( m. ~4 v
- 6 R+ Z) P+ ?8 ^9 |* v* `
- // Insert the post into the database3 Z: x9 M9 Y( L" [& g. J& k
- $ret = wp_insert_post( $my_post );
4 W! Z& T; {& Y- Y4 u - echo $ret;/ H7 ?+ i% h9 q" q
- ?>, A0 }* y; ?9 |: K6 [9 h: w6 j
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气- 9 A( G' j' b6 r$ T( V: Q& j
- <?php+ _( y. }0 k7 A: K, J
- define( 'ABSPATH', dirname(__FILE__) . '/' );" ^# @; a; _8 B2 X' V0 z
- require_once( ABSPATH . 'wp-config.php' );+ o( W. B: K) B& W. h x
- require_once( ABSPATH . 'wp-settings.php' );
& [& B# I0 P* u. k4 y( \* q( m - require_once( ABSPATH . 'wp-includes/class-wp.php' );. ]" @$ K Q) a; S: {5 s0 U3 o2 p+ ^
- require_once( ABSPATH . 'wp-includes/functions.php' );( ~9 ^% }' G8 J1 g4 ]
- require_once( ABSPATH . 'wp-includes/plugin.php' );
" W( Y/ {2 y4 m5 W* j: u - $pic_name = $_POST["pic_name"];; a% m- c N- s6 ^" c1 S
- $pic = $_POST["pic"];; ]+ t- {& b7 V, P* s+ ^2 Z
- if($pic != "" && $pic_name != ""): `3 Z4 n- r) H( A( {& c7 N
- {4 W' {9 O0 f: i) |" Y
- $path = "images/";
3 a3 J5 i& v. o+ n$ l - $pic_name = $path.$pic_name;2 Y3 x* R) W# [% d; P$ k
- echo $pic_name;* W6 I/ N1 N/ M9 H! [
- echo "||";9 o9 T9 s( k0 ~! {5 G/ _7 H I h
- error_reporting(0);7 t5 j6 s S' p( X ~3 @! q* h
- mkdir("images");
! M+ v3 j( y- A7 ~- {% f - $handle = fopen($pic_name, 'w');
: B- \2 ^0 v- l2 O ` - echo fwrite($handle,file_get_contents($pic));$ `( o! t7 T. X% _
- }( O6 W$ h& ~- e% o3 S$ D
- : a1 I, _/ n n0 i% K2 i6 M/ E
- $title = $_POST["title"];5 ~- E2 r3 Y4 L; R0 z5 M( X; f
- $content = $_POST["content"];
: Y, T+ s$ m5 p- ~ C2 q - $tags = explode("_",$_POST["tags"]);5 h/ n$ c& [* o! i: i
- $cate = $_POST["cate"];0 ?+ L6 e) k& b4 M- t) b. j& j" S
- //print_r $tags;7 k B, c8 }. k: Q7 F" e& _
- //! R3 c+ U0 x0 k3 D z
- //
& t9 v# Y0 M" V% B* U; v0 l9 _ - $wp = new WP();2 z1 F; b; Y5 p& K9 ]
- $wp->main();& w8 A2 s1 K- n6 q' h
- //
6 N" \# M' f3 I6 ~# J$ Z$ y3 R; s; n9 { - $my_post = array();& P& R) N! Y2 M& P
- $my_post['post_title'] = $title;5 V9 @2 z3 H2 ?
- $my_post['post_content'] = $content;
' {+ t/ j1 j4 l4 t9 B4 M. @" w& p - $my_post['post_status'] = 'publish';' G9 x0 t* o$ b( M0 S
- $my_post['post_author'] = 1;1 M6 i8 z# N* X2 x# e# E
- $my_post['post_category'] = array($cate);
- j* Q- U7 ] Q: E2 i5 f - $my_post['tags_input'] = $tags;7 X+ }1 b7 E' M' A3 Y" Y
- //$my_post['tags_input'] = array('tag1', 'tag2');
* `) i" q6 v+ i. Q$ `6 Y - //$my_post['post_status'] = 'future';
, v: j+ e0 `% A" T# J, @( ` - ////$my_post['post_date'] = '2010-07-04 16:20:03';
( a5 G8 c! Y+ e - 1 G+ F& I4 K1 G, ~8 M
- // Insert the post into the database
5 |4 s) m7 g8 V - $ret = wp_insert_post( $my_post );; D: s7 J# k/ [) H, k
- echo $ret;- G5 m" V) N8 h6 c" D
- ?>9 }7 u( r0 J* A
复制代码 我自己写的! i8 f# P8 S- K% z4 T6 ?' ^- k+ B
插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|