|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑 ' _, L3 j2 Q s+ |& O$ n; g
3 d( T5 P& k3 b+ g. M直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持! N3 m$ O0 Y- n" f% a
' b% I/ t9 K3 g& k我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了" ]; d/ @; G/ `- P& ~8 l. D
% o: J0 y; J" e' `6 I/ X
方便实用 简单 何乐而不用呢- 5 p$ u5 O6 `% D- A/ |! Y
- <?php- t6 K2 k6 q/ M
- . y& Z0 u$ }/ x/ ~1 g" R1 G0 B/ u- t; x
- define( 'ABSPATH', dirname(__FILE__) . '/' );
+ j# e; |8 e1 o/ G9 w7 t - require_once( ABSPATH . 'wp-config.php' );
$ u* U- x* p# o - require_once( ABSPATH . 'wp-settings.php' );
. n8 E% M2 s+ n' @ - require_once( ABSPATH . 'wp-includes/class-wp.php' );5 |, a/ ?9 l( S' [
- require_once( ABSPATH . 'wp-includes/functions.php' );
3 F9 i4 u' f6 o/ y - require_once( ABSPATH . 'wp-includes/plugin.php' );8 w" Z1 Y" T% t6 K9 A
- $title = $_POST["title"];% y& J) R$ A7 U; y
- $content = $_POST["content"];; P" O4 l. c4 @) g' G2 E0 B
- $tags = explode("_",$_POST["tags"]);2 W# I8 }- w! k
- $cate = $_POST["cate"];
' g) i. t2 X5 ~- z0 u - //print_r $tags;
7 @# F' p. F! q: Z4 x2 j. I% j - //' I/ j2 s" w% e8 Y! \6 E1 e7 }
- //( Z' ~0 E" u* B& {8 c. S% g
- $wp = new WP();
& c/ M: L7 \& h5 m4 ^& f - $wp->main();) V) }( I! L% x
- //( g8 F9 T! o: [; G. `' p
- $my_post = array();
) s# e4 K3 T4 @3 U% @ - $my_post['post_title'] = $title;/ ~: g& \$ G* w! ^& y- L, U
- $my_post['post_content'] = $content;
! R6 L* J' {* a% _ - $my_post['post_status'] = 'publish';
& G" ^" p% `6 N% F7 E1 r% @( P - $my_post['post_author'] = 1;
3 H1 D* `. G% O5 I; g( ~/ \% Z - $my_post['post_category'] = array($cate);
( J2 R( t K. X - $my_post['tags_input'] = $tags;
X- N. V. U) l" g - //$my_post['tags_input'] = array('tag1', 'tag2');
6 e' q0 p1 k- i5 k$ X, V6 d4 ~ - //$my_post['post_status'] = 'future';- n$ U8 O/ j0 z
- ////$my_post['post_date'] = '2010-07-04 16:20:03';. R0 k: q: S' L$ G8 q( t, T7 } B2 P
8 c. E/ I; M+ n8 M$ ~% ?0 y- // Insert the post into the database3 l# h. }6 K W. p$ I
- $ret = wp_insert_post( $my_post );0 T) e- w5 L; W+ r: x
- echo $ret;. x$ f0 b) h. h( @% A7 b( o! s
- ?>" _, w, x4 Q( [( [
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气
9 H$ b: H. l1 Q4 O. E- <?php
5 \- a! g0 V3 {% q3 N8 X - define( 'ABSPATH', dirname(__FILE__) . '/' );
6 H0 @, u' A4 B - require_once( ABSPATH . 'wp-config.php' );
/ R% ^8 `0 [9 U7 C/ V - require_once( ABSPATH . 'wp-settings.php' );
: Z; R" g$ u2 [& K9 G - require_once( ABSPATH . 'wp-includes/class-wp.php' );- u7 j+ _- [ p* [4 H w6 j% D
- require_once( ABSPATH . 'wp-includes/functions.php' );
5 S7 c3 Y- d/ i5 C4 W" l. | - require_once( ABSPATH . 'wp-includes/plugin.php' );0 g& N- M( x8 t! C2 v7 w
- $pic_name = $_POST["pic_name"];. [1 r& c9 B$ R& [1 i
- $pic = $_POST["pic"];$ |8 T% `8 _: \7 ~+ F. g$ }
- if($pic != "" && $pic_name != "")( m7 } s3 @( \0 l- i( @
- {
h* D7 Z/ M: V# ]0 d5 K - $path = "images/";
8 B0 [% s7 `5 H U8 | - $pic_name = $path.$pic_name;( U2 l9 S! b. R5 f* `+ N
- echo $pic_name;5 }% p4 Z& a8 z. g1 A
- echo "||";
1 T0 o( T1 b# N - error_reporting(0);0 |0 y% b& n4 i
- mkdir("images");
5 @3 _. l) X; M6 J } - $handle = fopen($pic_name, 'w');
, i6 t/ O+ \# S+ b$ p: C - echo fwrite($handle,file_get_contents($pic));
/ p2 q: k* X9 @" u/ m" a/ L& Y+ K - }" r/ m: y, f2 N
- 6 q* J3 k; l, F
- $title = $_POST["title"];
7 N/ `7 U; U! M - $content = $_POST["content"];
4 Z8 K: Y S& z" Y d - $tags = explode("_",$_POST["tags"]);
0 {: z, s) g* {5 b8 m - $cate = $_POST["cate"];
, c! K9 S2 H( ]; m- Y( n - //print_r $tags;
; S; e4 s/ c! s6 T1 P - //$ U: T) ~) f$ V7 ~
- //
2 h8 `1 \8 T9 ` - $wp = new WP();; h/ a2 {: s& v3 B; P# c9 h
- $wp->main();6 A5 Z7 O! C) T
- //9 Y- Y7 [: R" i+ i. `( |7 T
- $my_post = array();
$ J" J, d! z& t r: m K0 m9 R - $my_post['post_title'] = $title;
" h, R o4 M6 a6 w' L# c - $my_post['post_content'] = $content;
3 M! }% A# \4 C+ D8 U. {5 m% ? h - $my_post['post_status'] = 'publish';9 ?, T8 h9 ^" J: S7 L0 H
- $my_post['post_author'] = 1;# z4 J5 _9 o" h# w
- $my_post['post_category'] = array($cate);
A0 t) g. O/ E8 d - $my_post['tags_input'] = $tags;: C8 b9 ?' j7 T! R- b
- //$my_post['tags_input'] = array('tag1', 'tag2');
4 u1 n" f3 J, l: U1 Z - //$my_post['post_status'] = 'future';
" `: N8 ^! Y" W( p; Q) P - ////$my_post['post_date'] = '2010-07-04 16:20:03';
+ c7 z! r0 [, q' c3 R
# ~$ _$ ]4 I0 t' _. I& `- // Insert the post into the database. ], i3 B+ X" n
- $ret = wp_insert_post( $my_post );
$ M. }/ ]2 K9 T* F( @% y - echo $ret;( B6 { i E+ H3 H+ r0 f
- ?>8 U" w6 O t7 ^
复制代码 我自己写的- A8 f f* @$ I. _" j
插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|