|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑 3 l. x# k/ ^- q" c
* T5 M- i" R2 `+ J直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持
; j T* V: v3 [, K3 j8 L
% L: _ i+ J7 {我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了
1 q$ K+ U" l0 h, R2 v0 L# N5 U' D# K. o- n
方便实用 简单 何乐而不用呢- 1 q4 y1 @$ B# E& b
- <?php
5 P) ^6 ^ e+ D$ L
. p$ X* f( Q! V0 ]" a6 {1 z/ B2 b1 K- define( 'ABSPATH', dirname(__FILE__) . '/' );
2 `; P5 |( a+ H. N - require_once( ABSPATH . 'wp-config.php' );
" L2 B3 N! V; r4 r6 `( C6 R - require_once( ABSPATH . 'wp-settings.php' );
, L' N* n4 }2 Y - require_once( ABSPATH . 'wp-includes/class-wp.php' );
5 h4 B0 X0 m: U/ w, d - require_once( ABSPATH . 'wp-includes/functions.php' );6 g) N, x; a# |0 i! F8 J( W
- require_once( ABSPATH . 'wp-includes/plugin.php' );; a/ F, u9 d1 v" P$ o
- $title = $_POST["title"];
, {. x9 Y. v; a - $content = $_POST["content"];( v' V5 N* R4 z2 [5 V3 [; @! z
- $tags = explode("_",$_POST["tags"]);
% t% M) P% G8 T& [ - $cate = $_POST["cate"];
7 \' ]0 t+ f+ _ - //print_r $tags;
" W" d/ o% _, W( z$ u - //
6 f% E+ w) R1 s; s3 J( ] - //
* D1 {: z/ [9 i - $wp = new WP();
) w& N9 B0 s* V: h' M - $wp->main();$ _ y/ l: P Y' |" k9 i
- //6 M) Z3 x' f; a
- $my_post = array();+ i% T) I6 C& q) l) B2 n& K
- $my_post['post_title'] = $title;; t* w) Q7 C$ N7 h- ]
- $my_post['post_content'] = $content;
6 [% F' _ p: Q% _/ l - $my_post['post_status'] = 'publish';
( c& E) e& _) v) T+ ~- B' _ - $my_post['post_author'] = 1;
$ r9 H( d j* R; A3 u - $my_post['post_category'] = array($cate);
Y& b5 x- ^6 t% V4 m- _8 ? - $my_post['tags_input'] = $tags;
7 W# o G7 u5 \ _' e/ } - //$my_post['tags_input'] = array('tag1', 'tag2');
y" f6 g: N3 T. k* N- u - //$my_post['post_status'] = 'future';
7 D1 w$ U5 I* r B+ { - ////$my_post['post_date'] = '2010-07-04 16:20:03';
. a1 d1 }1 D0 Q6 }7 d - 7 z3 }) m- w- p; e, |1 @ v- K
- // Insert the post into the database
$ `6 g' A% M! g# y* m2 f' H7 t - $ret = wp_insert_post( $my_post );
6 L0 ^3 V" m* Y$ B5 T/ u - echo $ret;" L* M F S* q- y* j
- ?>
4 c% K8 u \ q4 B7 T) l) G8 r- ~4 `
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气- " g! U4 s5 A' h$ r3 `* y" V# {
- <?php
# C% J( ~7 O; l8 G9 t5 E3 U3 q - define( 'ABSPATH', dirname(__FILE__) . '/' );" ^2 L! P5 |: n. E; p: X
- require_once( ABSPATH . 'wp-config.php' );- p/ y* u) p* i( ]' F+ ]
- require_once( ABSPATH . 'wp-settings.php' );
4 E% D9 B2 i! b9 f/ X - require_once( ABSPATH . 'wp-includes/class-wp.php' );; G2 K2 E# v* _$ e7 z+ W: H
- require_once( ABSPATH . 'wp-includes/functions.php' );
2 g! k& b; K* ~( j- t! e) B F0 { - require_once( ABSPATH . 'wp-includes/plugin.php' );
0 i4 B- ~3 }: U; E) u$ j' m( G9 @ - $pic_name = $_POST["pic_name"];
/ `+ y3 o8 }$ J2 S0 K6 z) @) g - $pic = $_POST["pic"];
) U6 f" @& A. G5 V - if($pic != "" && $pic_name != "")
+ p( o7 S) K5 @6 Q. a+ I) _$ v - {
9 X/ ^1 m8 `5 [8 h) G - $path = "images/";
% n/ W2 }/ y# t3 T! ~& e; t - $pic_name = $path.$pic_name;
1 A: D+ C: p1 ]9 ?' n ^6 K - echo $pic_name;
- G! l$ I/ s7 I/ Q) N" B - echo "||";- W/ V2 D1 L' t1 x
- error_reporting(0);
& n- Y; |, k4 F0 n5 C - mkdir("images");
w+ P( D( F* a, e& ?8 C% I - $handle = fopen($pic_name, 'w');
/ T4 v5 d. O' s! P; K( e2 ] - echo fwrite($handle,file_get_contents($pic));
# U3 k" s9 e- C4 K5 u$ w. [$ r+ y - }
# l( i1 }! ]! P" \4 g2 t, u - $ H& t; B, j9 h+ l
- $title = $_POST["title"];% }/ s: e+ w4 V7 g' s
- $content = $_POST["content"];9 A# j7 [, i' ~4 P7 v: x, O
- $tags = explode("_",$_POST["tags"]);
6 u; D6 v/ p1 D" E! J. z0 B - $cate = $_POST["cate"];* D% g3 G, Q% z3 C
- //print_r $tags;( {! ~* b0 B: O" a. {5 b
- //+ s9 L% @# n7 A# L c9 O
- //" h# p- Y- W: a6 x* ?
- $wp = new WP(); d% K; |" N0 X3 Q& H, {
- $wp->main();5 R }0 E2 U0 v+ f& \ ]
- //
* A6 I+ Z2 F2 K4 t" e( g/ p {: S - $my_post = array();8 Z) W ]8 A. C7 a
- $my_post['post_title'] = $title;. X5 W4 @ J- ^7 ~8 m! F$ }% z& ~
- $my_post['post_content'] = $content;
- V& B/ ^2 P4 I - $my_post['post_status'] = 'publish';' G; L! y8 a" X+ w5 y/ @6 i
- $my_post['post_author'] = 1;
3 `5 O7 Z1 A! [ - $my_post['post_category'] = array($cate);+ d9 x' H) G, ~, }+ G
- $my_post['tags_input'] = $tags;# Z$ M) ]) ]- W/ Y' U* ~/ q D! F
- //$my_post['tags_input'] = array('tag1', 'tag2');
& }" {" C. J y: u - //$my_post['post_status'] = 'future';
3 R; e* w: t2 |) {; K2 J - ////$my_post['post_date'] = '2010-07-04 16:20:03';
/ X" w: i/ B/ K1 g- p7 V4 V' R - - }3 a; v6 s7 q1 b4 I/ ?+ V+ L# q
- // Insert the post into the database5 U6 h$ l; I$ L8 X$ H$ R0 c2 E
- $ret = wp_insert_post( $my_post );
8 B$ H9 `$ P+ d/ `6 t& B - echo $ret;( W0 F: a9 v! [; ^3 w- }' ]) J( r& m! k
- ?>
2 l/ l! s" J5 h/ }) Q
复制代码 我自己写的- L* T1 M* W' `
插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|