|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑
4 n. z8 `, v; d& v2 C. }8 g5 h
. A6 I/ Q; _ Y; N直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持
6 Y& P/ R, J$ a. y. A) u" P& X* x/ }& u$ t3 Q! [: j
我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了
/ ]' E/ x( W( U' Y( u% b9 b3 x& P# ~! `9 \ W- x
方便实用 简单 何乐而不用呢
" v+ H+ a F5 d( u3 B$ k( G- <?php. a2 k4 ?1 w3 i* @# j1 J
( n$ f; b, F% d) c- define( 'ABSPATH', dirname(__FILE__) . '/' );; D$ q3 P6 ?6 h' B2 J& K
- require_once( ABSPATH . 'wp-config.php' );) X) z8 q: f) a! k, x4 J0 Z
- require_once( ABSPATH . 'wp-settings.php' );
3 \# G+ T7 I" U: g - require_once( ABSPATH . 'wp-includes/class-wp.php' );
) O/ S6 m" L0 d" g - require_once( ABSPATH . 'wp-includes/functions.php' );0 g* M: N. j: Z1 O& c: j
- require_once( ABSPATH . 'wp-includes/plugin.php' );
! z4 b& E. d# x% x/ x - $title = $_POST["title"];' T4 e z2 R" P' g% ]6 c
- $content = $_POST["content"];5 d/ ]$ c( U* g z# `
- $tags = explode("_",$_POST["tags"]);0 P6 x1 t: F4 W% W& X0 w
- $cate = $_POST["cate"];
1 A ^4 W' _& R; h; s5 A8 s4 c1 V8 V - //print_r $tags; V* |! A$ a- r# j; y# k
- //
& C: O% b( |" r+ b1 |" Y { - //0 l2 H" ?$ V' X9 j) [5 ~7 y; P
- $wp = new WP();0 ]7 W- Q8 \$ @2 c
- $wp->main();
0 l/ W- n1 G. s, B" x$ z+ R - //
# k2 ^7 d |4 J9 v - $my_post = array();
2 O; o3 W% \( y6 c" H- m - $my_post['post_title'] = $title;
7 O |; B9 j" L+ z x - $my_post['post_content'] = $content;
$ d7 a4 I: T) q, R+ i; V - $my_post['post_status'] = 'publish';
+ @8 h2 O# D4 V: }+ e- \ - $my_post['post_author'] = 1;3 `3 P9 _# @, l f8 c
- $my_post['post_category'] = array($cate);
i! I3 x+ g% T5 R. G - $my_post['tags_input'] = $tags;- i' ~4 H E$ E7 ] y
- //$my_post['tags_input'] = array('tag1', 'tag2');' Q$ \3 ?5 O+ w( x) K. o: a
- //$my_post['post_status'] = 'future';( ^# S" F1 E0 p
- ////$my_post['post_date'] = '2010-07-04 16:20:03';
% _2 I: [* V- \% b$ o# L2 h - " B9 H- q* m, @. T2 }, H9 U0 `
- // Insert the post into the database) o9 }, q5 w+ B8 T
- $ret = wp_insert_post( $my_post );, n( V3 |7 R/ C6 ~1 y
- echo $ret;
3 T) j, U7 O7 x2 k* W - ?>5 ^: d5 r( g r# |% ?0 R) y+ O8 o
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气- " y$ ~" @, ]) t- _
- <?php
2 J' M- n8 d3 C6 O; r v - define( 'ABSPATH', dirname(__FILE__) . '/' );
( I. S0 ~+ O2 ^" y1 {" D, h1 x - require_once( ABSPATH . 'wp-config.php' );* F& p; J. N- G. G) N) Z6 z' e
- require_once( ABSPATH . 'wp-settings.php' );/ o( I! X% m! ?
- require_once( ABSPATH . 'wp-includes/class-wp.php' );
& z0 V6 E# n8 {6 U6 L, b - require_once( ABSPATH . 'wp-includes/functions.php' );
6 }4 _' i# @% Z) |' w) |% u& E - require_once( ABSPATH . 'wp-includes/plugin.php' );( q3 k- w* r9 `% f* `$ P
- $pic_name = $_POST["pic_name"];
9 I! D5 b+ }9 m; n* W - $pic = $_POST["pic"];2 L! d. h; E, C) \5 E
- if($pic != "" && $pic_name != "")
& M+ K* g" m; v( E3 n - {
9 v* w! e. m! G; _5 w - $path = "images/";
* w. ]3 M' i, X8 B/ \ - $pic_name = $path.$pic_name;9 N+ d3 u2 h; t M) n \& r+ f: {# c
- echo $pic_name;6 q& g8 e5 I4 h2 ~* p6 {5 N
- echo "||";$ z0 E9 x# C- [: a8 p5 M7 @
- error_reporting(0);
- T" q5 \/ w8 ^5 y9 Q/ ^( x8 U - mkdir("images");4 ]( j, I% j9 r: s- d& c9 d
- $handle = fopen($pic_name, 'w');
- x8 i/ M' ^/ L6 G; u - echo fwrite($handle,file_get_contents($pic));# ^" I; c& y: e4 z2 d
- }$ s2 e% @6 @: m2 T6 @- x
* |. Q, V+ g$ E4 {) Q# w- $title = $_POST["title"];
+ ~. x# [& X( x6 {0 ^ - $content = $_POST["content"];1 f f- J u+ Y" M
- $tags = explode("_",$_POST["tags"]);
1 s: N4 d* I( _. S) { - $cate = $_POST["cate"];# E; y. V y. n9 z+ Q
- //print_r $tags;
6 J9 Q: K/ w) h2 q - //1 p% t: h( m! n @# K$ h: T
- //+ |+ |; w, u& |" P0 I n9 G8 D
- $wp = new WP();9 `; W+ n5 e/ H4 d# u5 y+ \
- $wp->main();: d$ D: r' C& T( N; v% J
- //; S/ V# D- l* n9 I' n
- $my_post = array();
3 X3 {0 h( h" i& l) ^. X4 t1 T, z( ?+ ~ - $my_post['post_title'] = $title;8 X% J) w+ l6 k6 w" l
- $my_post['post_content'] = $content;
- v" N+ v n3 J( N' _$ a - $my_post['post_status'] = 'publish';
/ z9 v+ O. F4 l, { r2 W - $my_post['post_author'] = 1;2 U: t. J* f; ]4 i9 y _
- $my_post['post_category'] = array($cate);
8 H0 s) _/ e7 e. L2 ^; K. ~6 { - $my_post['tags_input'] = $tags;% e& {) T+ d) r X
- //$my_post['tags_input'] = array('tag1', 'tag2');% ?0 E# ]: n4 O9 V0 D7 m
- //$my_post['post_status'] = 'future';
3 V Q! S& [! V5 g. t6 `; k1 q - ////$my_post['post_date'] = '2010-07-04 16:20:03';
+ i4 m/ I8 X- D0 d6 \" _3 e
7 y% w$ }2 f, \6 {6 |1 j7 A5 ~- // Insert the post into the database
P2 ~: [, ^/ L+ x, h - $ret = wp_insert_post( $my_post );
9 R8 ~- t* ]; e- T" i( y - echo $ret;
5 ^# t& r; o+ ?& s - ?>
' d& [6 f1 A4 r" B" k+ h. u: D1 J
复制代码 我自己写的
" W7 f7 [! G8 W o插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|