|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑 2 p' ^ a% Y% J/ e
; o/ W- J4 m) V* m) u' U8 i
直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持
6 H0 ^8 s# {9 y) @- Y' `1 Z
d7 ]# d: g" ?8 Z" y2 w( i6 t2 L D& j, z我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了
2 `. {4 d; N9 v' F" M0 D) v4 c* a! I0 j4 Y$ K, d G. r- y4 x. ?( y& ^
方便实用 简单 何乐而不用呢
+ {/ T% N4 g! C+ o" I. a- <?php
6 E; R1 }0 F% p* j - - {! P( I0 W C( f- p# {
- define( 'ABSPATH', dirname(__FILE__) . '/' );
7 L* S; l8 q2 l8 v% Z9 m+ q - require_once( ABSPATH . 'wp-config.php' );
# d* B0 N. ~4 W: p# I - require_once( ABSPATH . 'wp-settings.php' );, f! i/ B. L) {- d0 _# S3 B
- require_once( ABSPATH . 'wp-includes/class-wp.php' );
9 ~% ^5 g& H1 Y6 ?! S& { - require_once( ABSPATH . 'wp-includes/functions.php' );
9 G; V7 V# m N - require_once( ABSPATH . 'wp-includes/plugin.php' );
5 H# a3 _# X+ h+ t E - $title = $_POST["title"];
/ \+ S, `! R6 f' q - $content = $_POST["content"];7 m' ]! E+ j" S5 M" J% [" _
- $tags = explode("_",$_POST["tags"]);0 I, z0 t8 ~' o
- $cate = $_POST["cate"];7 b1 @8 W. O% Z' |9 Q4 ^
- //print_r $tags; w. K& E$ L& r, \
- //
' ?% |* O$ h& a- q% @ - //" Z! Q3 Q! v& f. ~9 E9 n7 i
- $wp = new WP();* g: z% c- l! K9 l/ ~1 N' _
- $wp->main();
. J s5 M2 |. _, | - //
. i6 M2 h" @" ~* Y- u# ]( a - $my_post = array();2 X" B; h n" u" n9 n
- $my_post['post_title'] = $title;# u3 R2 r U2 O( [) j
- $my_post['post_content'] = $content;
; Q* p3 \2 }+ T- k - $my_post['post_status'] = 'publish';5 p/ q$ w0 `0 t* ?
- $my_post['post_author'] = 1;
4 Y3 g: }, a- R! J8 @ - $my_post['post_category'] = array($cate);/ u9 L2 b Y, K5 o* n t3 r4 j$ z
- $my_post['tags_input'] = $tags;
6 T& U3 Z4 i* I - //$my_post['tags_input'] = array('tag1', 'tag2');
* h r* [/ L* {/ Z; {. x& ~ - //$my_post['post_status'] = 'future';
3 G& c0 y) k1 M* n, ? - ////$my_post['post_date'] = '2010-07-04 16:20:03';
9 o' z" _# P+ ? - 4 j* e S) l; [' d4 F3 b
- // Insert the post into the database
5 K4 z. r4 ~5 K5 U& j# h - $ret = wp_insert_post( $my_post );+ Y( r9 [) U h* ^& n- W3 M
- echo $ret; y! s B( {& F+ K ?# m! m
- ?>2 K D N# @# |* v
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气
5 }$ C/ A: m. s7 e% ^5 _: M- <?php
8 S% x" g9 Q' W. l p - define( 'ABSPATH', dirname(__FILE__) . '/' );
) J' ~5 c( M; f+ S4 b - require_once( ABSPATH . 'wp-config.php' );0 `% q+ _6 P/ l1 b) n$ b
- require_once( ABSPATH . 'wp-settings.php' );
, [8 x1 Z4 P7 R! ? - require_once( ABSPATH . 'wp-includes/class-wp.php' );
5 z( Z4 S: F) \# E+ A9 i! u9 b - require_once( ABSPATH . 'wp-includes/functions.php' );
' T' S# |/ r& ^$ l0 g D* Y - require_once( ABSPATH . 'wp-includes/plugin.php' );* V- I. T5 V& ^
- $pic_name = $_POST["pic_name"];. d7 }. T! U8 d
- $pic = $_POST["pic"];- \! o$ o% W6 A. Y
- if($pic != "" && $pic_name != ""): a8 C/ L" }- ^" K5 j* C
- {) i$ C7 B; u: T& Z7 H
- $path = "images/";
# q7 d+ X0 H( c2 @" x - $pic_name = $path.$pic_name;
3 {8 I0 Z4 v: x0 }& v* G- V& H; e - echo $pic_name;
( ]' ~1 `# N+ c ^& w p - echo "||";
# Y8 g6 z7 m$ O. G1 r( y; J - error_reporting(0);5 i" E9 B# f: {; l8 L5 M+ T
- mkdir("images");6 y% v3 p5 c- U' Z9 U5 j6 H, E( W
- $handle = fopen($pic_name, 'w');, H& N5 |/ r. f
- echo fwrite($handle,file_get_contents($pic));
! C' u' q' ^9 Q2 E - }1 m3 g9 _8 G7 D
1 c5 }' m# N- Y8 }# R+ U- $title = $_POST["title"];
; t( e* {( U3 t' ]7 j - $content = $_POST["content"];
& }, n2 W# d) Z( B; ?8 [9 q7 O - $tags = explode("_",$_POST["tags"]);
& B ~' Y- p" @7 p$ N# r5 X - $cate = $_POST["cate"];
- O& T M: t, z; s' { - //print_r $tags;5 x, x5 C( ^8 l7 C$ I; J$ H. a/ i6 {
- //: g# E% `2 U3 n2 @
- //5 ?' r+ K% a! D$ @8 \
- $wp = new WP();& p* N& ^) K9 C- @. v; T. S. ?( T
- $wp->main();" _3 i3 U! K( E7 n; T+ P) f
- //
K: L) X" v8 J5 y - $my_post = array();. ^( o6 C- R# m. e7 N& X& `
- $my_post['post_title'] = $title;
* p6 P: `/ e; c! {5 k1 _ - $my_post['post_content'] = $content;' R1 p: j7 N) X5 J
- $my_post['post_status'] = 'publish';, d2 |8 k4 G2 d+ i/ Z
- $my_post['post_author'] = 1;
" @8 A: R. k) O* h i - $my_post['post_category'] = array($cate);+ t& {" K, J, r' G' ^5 v1 \
- $my_post['tags_input'] = $tags;
4 J& i/ L8 E ?/ e5 ?: t - //$my_post['tags_input'] = array('tag1', 'tag2'); J; V( |2 {9 b2 C/ L. x- l7 E2 G
- //$my_post['post_status'] = 'future';
+ Z! a) b/ i& k' }; \ - ////$my_post['post_date'] = '2010-07-04 16:20:03';
8 A" B+ K2 q, s! r- H
7 E# W5 d/ D: s1 a* q- // Insert the post into the database# x/ S8 E1 t: K: p$ \! _
- $ret = wp_insert_post( $my_post );$ E% ^% q8 q4 L: U4 G$ I/ M( H
- echo $ret;
% Z! z: \% ~" v9 P' [ - ?>5 E J* P0 x6 h
复制代码 我自己写的1 H q- R% ^: W# d+ \! u; i
插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|