|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑 ( v, Q8 c3 {2 E& [6 M
6 d9 W0 x/ {! n, g% ^
直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持3 ~9 g$ l- j1 @, M
1 M# V- X" b" a+ y7 R+ x
我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了5 Z% e& M3 r/ F% x% D" L
. u; K! a9 L' _0 P( e3 s方便实用 简单 何乐而不用呢- 0 q3 N) u% ?% [% {
- <?php5 X# ], y/ D2 Q
- " A, C! q, V6 Q8 r9 H; S
- define( 'ABSPATH', dirname(__FILE__) . '/' );
4 _. g5 }3 v3 d8 w# O1 {( ? - require_once( ABSPATH . 'wp-config.php' );9 _& @1 i# Z d7 M
- require_once( ABSPATH . 'wp-settings.php' );. D) W$ N8 ^) R) D5 R5 P8 K
- require_once( ABSPATH . 'wp-includes/class-wp.php' );- L( n( h! _: ~" o8 j. V! O( T
- require_once( ABSPATH . 'wp-includes/functions.php' );
3 ]4 t6 j8 z& C. I# O( W3 ]( v - require_once( ABSPATH . 'wp-includes/plugin.php' );
0 ^( W2 b! u0 } - $title = $_POST["title"]; b1 ?0 X' }& t; {1 v; m& F
- $content = $_POST["content"];3 T$ e! i: }5 p5 A$ o
- $tags = explode("_",$_POST["tags"]); I' b, n+ `* R: o/ v _, M+ f
- $cate = $_POST["cate"];
/ ~( k0 e' }$ @1 O - //print_r $tags;; k; a; @8 m; q& o! H4 z, s2 W( t
- //- v) z; T. a8 o; E; R
- //5 u1 I- c& i/ T( J/ I! T
- $wp = new WP();
" h5 h* r; M% ^. g - $wp->main();/ O' x# x8 d5 @ w O6 u$ j
- //
+ K- G- |: i% T* w5 N X - $my_post = array();
( W! N9 y2 J# g. c4 D0 }* E$ Q# F - $my_post['post_title'] = $title;
4 t6 R! m* W* L6 Y! r: E7 ? - $my_post['post_content'] = $content;( ` i, ^: L) i8 I' c
- $my_post['post_status'] = 'publish';
7 O' r B* B+ n9 D) ]% a' V - $my_post['post_author'] = 1;; o0 p0 e' Y M8 b$ q+ n# \
- $my_post['post_category'] = array($cate);
6 U4 w" ^' }+ M4 l1 ] - $my_post['tags_input'] = $tags;
. c6 q( B6 E. @. o1 y - //$my_post['tags_input'] = array('tag1', 'tag2');
1 B( r7 c2 [2 d* F+ }4 j/ \+ y G - //$my_post['post_status'] = 'future';
: l$ r( S4 `% H - ////$my_post['post_date'] = '2010-07-04 16:20:03';$ q2 H; `% Y r
- % o; h* M C5 E; v" ]% [3 R: `
- // Insert the post into the database
! I$ b7 O( ~# A! H6 X - $ret = wp_insert_post( $my_post );! C/ k/ Q5 t* N
- echo $ret;- y% }. ~* E6 {; k- V
- ?>' j1 c/ z# X( H; [" u0 k8 i
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气- / F' O/ ]; {: m" n
- <?php$ O7 X0 g2 B+ @" I# w2 t# t
- define( 'ABSPATH', dirname(__FILE__) . '/' );
5 r/ V: }) l. R. N" @4 S$ |6 {1 n - require_once( ABSPATH . 'wp-config.php' );
& E# j9 J+ d0 P" {, k" m% U0 z0 P - require_once( ABSPATH . 'wp-settings.php' );
9 W+ u$ B) q- [. l- V- f* f - require_once( ABSPATH . 'wp-includes/class-wp.php' );! q( g: O; o# a( Q3 y; b
- require_once( ABSPATH . 'wp-includes/functions.php' );
0 w( k' T" n9 ~( O- r - require_once( ABSPATH . 'wp-includes/plugin.php' );
% x( f+ c& W! l5 L - $pic_name = $_POST["pic_name"];
8 E: e3 M0 M2 R$ E) l3 K - $pic = $_POST["pic"];
t6 Z+ |9 s! X k" e ^9 N - if($pic != "" && $pic_name != "")
3 |' p8 u* E2 N. S0 c3 a7 c - {
# V* V+ @( i2 Y( r3 V - $path = "images/";
$ r* `7 Y& F: J2 p9 M - $pic_name = $path.$pic_name;9 N# Q' T m: d0 w$ Y. t8 P' U
- echo $pic_name;0 L r6 \& }/ K8 R: K& u j3 Z/ \
- echo "||";
' H) O& L$ p/ Z; q) H5 E$ d b$ r4 f - error_reporting(0);( u' l. L9 j" b4 }4 l$ k' K6 R
- mkdir("images");
, x; p; p/ ?3 a% m - $handle = fopen($pic_name, 'w');/ _+ W$ W. C+ |6 f) V( A2 J2 t) e. d' M
- echo fwrite($handle,file_get_contents($pic));
! s# b3 h1 m) v9 q) q0 Y7 W - }0 {: O2 Y r3 N$ C' P
- ' Z' d0 J0 f" w1 F# m
- $title = $_POST["title"];
0 \' l/ }9 S; C. C) E3 E - $content = $_POST["content"];7 q7 [& t \8 M8 ~5 ?
- $tags = explode("_",$_POST["tags"]);$ m& D7 Z7 e9 B/ j9 A$ R% j
- $cate = $_POST["cate"];% e, Y+ Q! E+ L9 }
- //print_r $tags;
; s! h4 X' `! T& @+ { - //' d6 e/ _! S4 Z2 D. ]
- //8 h. O- m: O1 V) @1 N% O$ e
- $wp = new WP();" f7 T3 i3 F# y4 Z) t
- $wp->main();6 \% C) Y7 r* d
- //, Q2 v! F) J! |/ o
- $my_post = array();' s; R7 L7 O9 {2 u- C
- $my_post['post_title'] = $title;1 v, B/ y( Z- d: ?3 Y
- $my_post['post_content'] = $content;
: M" \) I# ] E" G - $my_post['post_status'] = 'publish';) Z2 l4 w$ \' H- P
- $my_post['post_author'] = 1;" C; |- T W* m& Y
- $my_post['post_category'] = array($cate);! y2 i8 v6 c2 b; g" T' I
- $my_post['tags_input'] = $tags;
2 s( O% z; d+ M+ j2 v - //$my_post['tags_input'] = array('tag1', 'tag2');
3 y% v7 C' T% y' x! M - //$my_post['post_status'] = 'future';
' s9 t# @3 v6 T$ j4 k: D( u+ J - ////$my_post['post_date'] = '2010-07-04 16:20:03';" A# |0 z6 Q, d1 M a' `
u9 b" u8 `; d: y- }+ Y# m! n M- // Insert the post into the database5 p; i: h6 n @$ w; R' s; q
- $ret = wp_insert_post( $my_post );
3 u$ l+ {4 T( X - echo $ret;
8 K# v |* h% z - ?>
2 t# `/ l& X2 Z) F, T6 D. R
复制代码 我自己写的" a/ Q; @/ }7 |- B4 Q$ ]9 B
插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|