|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑
2 b7 a9 }; z) a2 k' V9 x) n) F# M
1 _; ]4 F; x" [7 W3 g2 ?直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持
; U, h# X) \5 r" d. C* h; E& g6 l2 ^
我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了
4 W7 N( Q* z* G" o8 @% T
% [$ S/ X/ z: y' A4 P0 A方便实用 简单 何乐而不用呢- 7 j, ?5 r# {+ p% L3 I
- <?php
0 D0 X2 J, u, u1 A5 r/ ?0 L - 6 x/ Z& m! c3 U. c! c2 G `
- define( 'ABSPATH', dirname(__FILE__) . '/' );
+ _( x2 d7 N. |7 s6 D5 P+ k - require_once( ABSPATH . 'wp-config.php' );
9 y }) b2 U2 @3 u0 Y - require_once( ABSPATH . 'wp-settings.php' );7 \5 Y& p: y6 e
- require_once( ABSPATH . 'wp-includes/class-wp.php' );
8 o. j" ?# G: s( K/ T - require_once( ABSPATH . 'wp-includes/functions.php' );% r5 R) f7 V5 x) T
- require_once( ABSPATH . 'wp-includes/plugin.php' );
& {4 I" k/ Z6 L! e/ R - $title = $_POST["title"];
1 s, J0 o9 H& o8 Z! r- g - $content = $_POST["content"];* \; r7 a7 G. q- G/ k* a
- $tags = explode("_",$_POST["tags"]);
2 _! ?7 j1 M+ Q# z - $cate = $_POST["cate"];, |' O1 s- O2 S B9 P. j
- //print_r $tags;
, r1 j# _' a0 x+ L9 o0 v: f' S - //
$ ]' B9 n3 F4 W, M9 \( M - //- M: ?0 `0 @/ I; ?! E' Y0 a+ f! G
- $wp = new WP();
$ k- k) A; {" {! [/ ^' c - $wp->main();0 [) N/ _ W7 ?) t1 Y
- //
8 V% h$ c' i/ f7 d) Q - $my_post = array();% ?! s! {3 E1 h; F
- $my_post['post_title'] = $title;
- s, E5 R% d. V5 p s - $my_post['post_content'] = $content;
7 L. m8 j0 U* S5 l6 D2 L: b - $my_post['post_status'] = 'publish';
- _( z+ l& N& c; A! |/ t - $my_post['post_author'] = 1;
2 p( T+ \2 k, |+ d6 L - $my_post['post_category'] = array($cate);
& [$ x7 t9 s! I. f# y - $my_post['tags_input'] = $tags; `; _" V: B0 `1 g) D
- //$my_post['tags_input'] = array('tag1', 'tag2');: m) p. N7 L+ t
- //$my_post['post_status'] = 'future';
( h* M) z6 H. `7 M - ////$my_post['post_date'] = '2010-07-04 16:20:03';+ g9 t$ P& d) J! C
- F8 b0 x4 q1 a, w# |: q. ^- // Insert the post into the database) w" v$ k% s6 ]6 g
- $ret = wp_insert_post( $my_post );1 [4 l' m1 h7 Y' q* h9 p
- echo $ret;
/ ^1 T# U( g6 P* g) O4 Z4 m& d% Z - ?>
4 D* f7 Z) _6 k% s1 w" d# d* M* t
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气- # q# r" S% }7 k1 N6 M
- <?php
! o8 ^, Z* i2 _" x& ^2 T$ S' T - define( 'ABSPATH', dirname(__FILE__) . '/' );
8 q# k X' w# e! F$ y - require_once( ABSPATH . 'wp-config.php' );, q9 b; ~; c1 ]4 b! L
- require_once( ABSPATH . 'wp-settings.php' );
1 G, F6 u* Q2 R S - require_once( ABSPATH . 'wp-includes/class-wp.php' );) k/ Z6 _1 i6 ?$ e8 f
- require_once( ABSPATH . 'wp-includes/functions.php' );9 s) q- I2 j6 C, W
- require_once( ABSPATH . 'wp-includes/plugin.php' );5 i" p* P: Y( B5 K0 ?
- $pic_name = $_POST["pic_name"];$ L6 f0 z" M) d3 e
- $pic = $_POST["pic"];) |4 b$ v6 D( o" [- d& A0 C
- if($pic != "" && $pic_name != "")7 Z& o! ]" L0 s+ P0 T* N# N
- {. t9 J8 \8 w9 n* g- f6 V
- $path = "images/";
/ b6 X# D! c" f5 N- E1 Y* ~ - $pic_name = $path.$pic_name;6 N- p! g' l8 a6 h3 W
- echo $pic_name;
8 X6 Q9 d' ], ` - echo "||";
. G, y; |1 \' k: n1 C3 r - error_reporting(0);* f; A- J7 q& K/ m b9 U
- mkdir("images");
% p, I0 n5 q4 S3 D/ g - $handle = fopen($pic_name, 'w');. X; h% T6 _) y: ^$ R
- echo fwrite($handle,file_get_contents($pic));
# G$ F$ X3 |% F# J9 r8 q - }
+ o t0 m% W" }1 U. _/ B4 D! P% h
U6 @& Q+ C3 O; z; C- $title = $_POST["title"];( F" S& R. C- _0 Q
- $content = $_POST["content"];+ I, h; j8 b* F% z
- $tags = explode("_",$_POST["tags"]);6 B# x7 r$ _6 v9 h* W
- $cate = $_POST["cate"];
8 P" Z6 Z2 U6 C - //print_r $tags;# D5 b$ L9 c/ T5 C' x# L7 u. _
- // A' u2 {# r. G+ P
- //4 _3 B1 u; X0 e; l4 n# M! ]
- $wp = new WP();
( s e5 p9 {8 Q+ m [' K! z& u - $wp->main();7 s S7 |: v9 Y* t" v: D$ W" h) I" {
- //
8 V# [& n- T, y5 u8 _0 M! C - $my_post = array();
9 h9 _1 C; b( b# _" |9 a. X" c- K0 Q5 a/ U - $my_post['post_title'] = $title;8 Y, d' z! }& E/ {; U' k
- $my_post['post_content'] = $content;
* |" v6 C- M9 k# s7 ?# ~+ O0 m4 E - $my_post['post_status'] = 'publish';
$ z4 F+ {& K/ r, f1 S - $my_post['post_author'] = 1;
$ Z8 q9 K H& N - $my_post['post_category'] = array($cate);
3 Z. G; M' E. p1 u; A- \6 V1 a - $my_post['tags_input'] = $tags;
/ f: d* |+ n W& F/ J* s4 m7 O& R0 m - //$my_post['tags_input'] = array('tag1', 'tag2');- y5 N& E0 b6 A! a) M
- //$my_post['post_status'] = 'future';
$ b. s( d2 d: R4 h" B1 [ - ////$my_post['post_date'] = '2010-07-04 16:20:03';# D! {, K2 p5 P0 g+ S% A
- 6 C ^8 C( E5 R2 r
- // Insert the post into the database" b7 D% G" H$ l
- $ret = wp_insert_post( $my_post );
. s7 E) G, B+ V" R - echo $ret;- W2 t& `4 K* N, ]& ^1 A
- ?>/ Z9 ]- J3 g1 P
复制代码 我自己写的
- c |+ Y" E! I5 v& B2 P) t插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|