|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑
( O4 B: A% O: O7 X" p m
7 f3 E2 _% P3 _7 F5 p% v0 |, N直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持
* \" k! _+ V' H! C! ^1 i
9 s; C% S, Y1 c. m我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了+ B0 E" K/ `! C
+ O. [2 d( f8 A, D+ ]方便实用 简单 何乐而不用呢
1 d. J5 M& P) A9 F& A; D& t( V5 p- <?php/ T$ }! \ ^8 s% [) r- a
- 9 z6 U! \3 N2 W; p; o1 M
- define( 'ABSPATH', dirname(__FILE__) . '/' );% y9 Z5 b& b; F9 [# x, P
- require_once( ABSPATH . 'wp-config.php' );
1 {/ h5 f; Q+ m& E { - require_once( ABSPATH . 'wp-settings.php' ); {+ ]- C# z. U
- require_once( ABSPATH . 'wp-includes/class-wp.php' );
. c+ U4 Q9 ^5 q! q, `0 X3 _& d - require_once( ABSPATH . 'wp-includes/functions.php' );
; S8 ?2 G/ @; d/ @0 P% T - require_once( ABSPATH . 'wp-includes/plugin.php' );
5 Y2 q: U% E# H" w; Z Y; X - $title = $_POST["title"];
; S1 `7 Z8 k3 X7 ^ - $content = $_POST["content"];
' n1 k6 {! i5 a1 ~0 ^* g- B0 K4 c+ M - $tags = explode("_",$_POST["tags"]);$ q6 E5 {; J7 H( H/ A7 E6 V
- $cate = $_POST["cate"];. v$ E6 {1 c5 H, s8 p
- //print_r $tags;9 |: X& {; V8 i" n
- //
( k, X2 S6 k6 o - //& c" {! s+ f9 P# c H
- $wp = new WP();1 J0 O4 u) `+ ?
- $wp->main();
; U% {0 O9 o6 @8 Q6 u/ v d - /// N1 c- N. \, {9 D# R7 G
- $my_post = array();
% f5 m) W# Y) s8 n - $my_post['post_title'] = $title;
: w0 d& v' V9 u - $my_post['post_content'] = $content;
% Y& V5 Q" P* \3 O' m - $my_post['post_status'] = 'publish';
. m* m! }" g2 K! M+ P - $my_post['post_author'] = 1;
& D' B- t; p! M+ U - $my_post['post_category'] = array($cate);
. e9 G8 N& `, p2 h# K- o - $my_post['tags_input'] = $tags;( \& n ~. w" K( \: _% ]$ C
- //$my_post['tags_input'] = array('tag1', 'tag2');4 ]6 Z$ @2 w, a) G& `
- //$my_post['post_status'] = 'future';- c/ m! w) v% }# O# o; @
- ////$my_post['post_date'] = '2010-07-04 16:20:03'; V( }) N. e! T. K1 | N" ~
- , ^3 P" e) z* C7 K
- // Insert the post into the database
$ B" |# x: O8 y& G v' ]5 e - $ret = wp_insert_post( $my_post );5 e* p" p& z; Z& F
- echo $ret;
. C: x+ W; p1 c' u - ?>
+ z. q# p! H5 @$ n, x2 n
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气
8 H7 G4 C1 x% B( u/ q9 K0 @- <?php8 u1 W. S9 I# ?0 s8 u
- define( 'ABSPATH', dirname(__FILE__) . '/' );' H# j$ I) |, L+ S+ ~3 f
- require_once( ABSPATH . 'wp-config.php' );$ ~3 f) r5 a$ Q- N% n
- require_once( ABSPATH . 'wp-settings.php' );
& d/ y! N6 }5 ^/ i - require_once( ABSPATH . 'wp-includes/class-wp.php' );
* \( S1 V6 q. q; N/ [# n! O - require_once( ABSPATH . 'wp-includes/functions.php' );+ S+ S" S; K$ A* a0 d+ Z1 }- P7 Z
- require_once( ABSPATH . 'wp-includes/plugin.php' );: ?! L& ]( S$ U4 G6 H
- $pic_name = $_POST["pic_name"]; `/ M4 h. g5 f( \( N, E; r
- $pic = $_POST["pic"];$ c3 }5 v" l6 e3 f3 b6 q
- if($pic != "" && $pic_name != "")
& M7 A6 F# l! H! _# Z* r - {
6 d- ?. K/ {- i. `$ M0 l, J3 t - $path = "images/";: q3 O5 i9 Q2 B
- $pic_name = $path.$pic_name;
# L0 ^6 t6 E1 i' ^. } - echo $pic_name;: L7 d/ u# M$ m1 h
- echo "||";
( L; o$ R: o' y# { - error_reporting(0);
7 x) b! D& f8 n! q. p - mkdir("images");/ {4 M. i H) J0 x
- $handle = fopen($pic_name, 'w');
7 |; d( n" g W P - echo fwrite($handle,file_get_contents($pic));+ I4 ^7 _# s8 C4 F1 u
- }6 ?! b V3 @( e3 n3 r2 c% \
- " ^- _" Q& s; k9 \( h
- $title = $_POST["title"];
& U& J1 c+ {- m5 W - $content = $_POST["content"];
9 f* f$ T- z. n$ h - $tags = explode("_",$_POST["tags"]);, Q$ B/ k& }- |. g" z. q6 y3 X
- $cate = $_POST["cate"];
, q0 o% c* b# U, `: O; k' e% P - //print_r $tags;
0 H& ^* E+ M! ?) x( @' O* F - //
# N6 z, f* H, L - //6 G4 g& ]+ @( i$ F, ?- u4 J, S
- $wp = new WP();
' x0 t% @* x8 v& r& J5 S& e4 K3 B - $wp->main();9 i% D; m0 M$ L* b4 \- Q
- //" ` K6 {- _6 C1 `$ x
- $my_post = array();1 r- ^5 N4 Q; ?3 \- O! H2 p
- $my_post['post_title'] = $title;& i: h' I F; G5 t2 G& y; P5 V
- $my_post['post_content'] = $content;
" x$ A' z- M+ a - $my_post['post_status'] = 'publish';2 m! E2 h4 s* f2 `* U( G/ X6 e2 p
- $my_post['post_author'] = 1;, t2 |0 p) F; r7 z9 T& L
- $my_post['post_category'] = array($cate);
- s, u) k- n( X" W - $my_post['tags_input'] = $tags;1 ?" n+ S* g7 s& I: A$ B
- //$my_post['tags_input'] = array('tag1', 'tag2');
1 w: p0 V: ], l5 \- u) k, |0 P - //$my_post['post_status'] = 'future';1 [2 Y: j% B1 P1 r3 f
- ////$my_post['post_date'] = '2010-07-04 16:20:03';
1 N3 M1 W$ q0 |8 R( w! n - . @* \% ?8 T& o
- // Insert the post into the database
" h/ f4 e1 w |6 n - $ret = wp_insert_post( $my_post );% N! S2 c" z) F
- echo $ret;
+ ]/ C- N: K a; F, [8 ] - ?>, E" u2 I" a7 l6 O5 d
复制代码 我自己写的
( ~& G- P* m* P T4 I, G4 U5 F插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|