|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑 : W) \* h- ^; i! B! C
" o9 I7 b; g& h5 o# [直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持6 b8 Y, v |7 X
2 f; Y- R4 {) U6 B4 s$ j2 x
我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了
8 q# F8 m9 _% \* Y: W) f
s3 I7 Y( e) M+ r% a方便实用 简单 何乐而不用呢
; N( `" g+ K c: j- <?php% D/ X% n+ r1 @$ ~! t
- 2 N2 [" z: y& R! Z8 T. Y
- define( 'ABSPATH', dirname(__FILE__) . '/' );
2 u! }8 A0 }: X, A1 |! T( g - require_once( ABSPATH . 'wp-config.php' );
( F8 G7 V9 N% Y - require_once( ABSPATH . 'wp-settings.php' );& E2 a' z( o7 X. v* h
- require_once( ABSPATH . 'wp-includes/class-wp.php' );2 y7 b; I0 Q0 r' d, I2 ?" Z/ C; k
- require_once( ABSPATH . 'wp-includes/functions.php' );
" W9 a) G, V5 M+ A, C - require_once( ABSPATH . 'wp-includes/plugin.php' );
& S1 d8 d2 O* f4 z1 |3 J# E7 y( M - $title = $_POST["title"];
- \' `" G3 r/ X7 ?! h - $content = $_POST["content"];+ X. E. E7 `. u
- $tags = explode("_",$_POST["tags"]);
/ V& ]! i% O" J$ M) S' O - $cate = $_POST["cate"];
0 |7 w6 P4 y3 f2 Q; Z& ? [. K - //print_r $tags;2 I, ~, @: Q* V) }
- //' s: `: \0 I* o9 @' I' w
- //
V: t1 Y) I. p- a( Z$ m/ H8 t - $wp = new WP();
' k" |1 t& R8 @) W& W& F8 s5 B; w - $wp->main();: [' u" y1 W B+ }0 V: x' ]
- //9 C8 j: U e+ w+ ^, N+ Q7 r8 S
- $my_post = array();
# e# r2 o& |' N. Z5 t - $my_post['post_title'] = $title;
8 r- d6 T/ W- J- I; y* r9 M - $my_post['post_content'] = $content;
- N$ ?' r* d- _6 `' _ - $my_post['post_status'] = 'publish';
, {7 m( B: v* u - $my_post['post_author'] = 1;
, \$ q0 N7 Z, }' ~# u. F: X% h - $my_post['post_category'] = array($cate);
, z j8 A$ v- a' @/ x7 u - $my_post['tags_input'] = $tags;
1 {+ x/ D+ e9 q% K - //$my_post['tags_input'] = array('tag1', 'tag2');# T! }% G% f! s, M) g5 o' J
- //$my_post['post_status'] = 'future';' c5 [, Y) C4 Y' l; Q- N
- ////$my_post['post_date'] = '2010-07-04 16:20:03';+ o; s& T7 Y& n
6 c4 |! i4 f/ x- // Insert the post into the database
4 }; r w2 Q& s' x' u8 i I* A - $ret = wp_insert_post( $my_post );
% M- P' h$ M2 Y - echo $ret;4 i f1 i3 y" x8 s6 H) [2 I! V
- ?>
9 h/ [/ q' c# l; P: U" u
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气
7 p( _% e1 c( O9 }; k- <?php
$ B' q& g! }1 N3 Y" t9 k8 l" }( T - define( 'ABSPATH', dirname(__FILE__) . '/' );# a) e* z* ^' i- r
- require_once( ABSPATH . 'wp-config.php' );
: F6 a0 [; _2 c, l! D8 _( _& u# C - require_once( ABSPATH . 'wp-settings.php' );
: P! t! O: `8 K% v9 r - require_once( ABSPATH . 'wp-includes/class-wp.php' );7 X) G x: X! D# k
- require_once( ABSPATH . 'wp-includes/functions.php' );# l6 n) t" R+ }; c3 V( @# `# B
- require_once( ABSPATH . 'wp-includes/plugin.php' );
& i, z& p% @: ~& I$ D - $pic_name = $_POST["pic_name"];! ~" [, ]& G2 E) Q& k
- $pic = $_POST["pic"];) E8 E! N/ C: d! K5 E
- if($pic != "" && $pic_name != "")" x5 S9 @8 H8 E% H- t# {
- {
! D- \/ }! q& i - $path = "images/";
0 M5 O" U8 s# v; D7 c - $pic_name = $path.$pic_name;
5 i O2 g3 t/ N {( B# { - echo $pic_name;& \0 s$ |/ g: V% d! x
- echo "||";
+ C% m( n6 k/ v# b8 q6 t - error_reporting(0);
$ V. \" x V* K% x% G - mkdir("images");' M2 B, b9 L5 G% T/ h; z
- $handle = fopen($pic_name, 'w');
- v7 Q) s3 a+ l( B# k N& x6 n - echo fwrite($handle,file_get_contents($pic));
3 H0 h' z5 r" I) w( J, M' l - }" I$ b# P+ `/ p$ M) F' k0 W
' q) n- U& X2 J; n$ e, F- $title = $_POST["title"];6 P: d {$ {3 y3 R2 J4 d; o" @
- $content = $_POST["content"];
9 e- T7 h% K$ s1 ^; Q$ u9 P0 b - $tags = explode("_",$_POST["tags"]);& v, e# m* W% n
- $cate = $_POST["cate"];
; e* d: P5 a- p5 x! U - //print_r $tags;
% N* e- y* o0 J6 s+ V - //
+ B k5 p! ?* M. e y: E( f3 l/ B - //: M$ P: }5 v g
- $wp = new WP();! G+ U* h; Z0 o/ W) A4 c
- $wp->main();* L2 I, x1 k7 P" L* g9 G2 i1 L. h
- //& b& g5 Y, }: z6 s4 D8 o- J7 T
- $my_post = array();' n! E: L+ g( r# H, G
- $my_post['post_title'] = $title;
2 N5 u/ d! g! s' L+ ?- g - $my_post['post_content'] = $content;
& F$ w( x; o, e# [, ^" X' W& n - $my_post['post_status'] = 'publish';# L: \% j f* R8 [
- $my_post['post_author'] = 1;/ |" ?5 p1 i0 L! @3 @
- $my_post['post_category'] = array($cate);
$ b; z, n$ U+ C- i) E; x9 \2 q$ U! r - $my_post['tags_input'] = $tags;2 o0 W( c+ _5 w
- //$my_post['tags_input'] = array('tag1', 'tag2');
$ J" T! Y, K, O- L. B$ K2 b: U - //$my_post['post_status'] = 'future';
4 C; f' z$ U3 V! i; \4 s' A1 ]! R - ////$my_post['post_date'] = '2010-07-04 16:20:03';
) F# y+ n/ {6 Y# `! R2 b3 C - 4 w5 j- X/ X& j# F0 J$ i! @5 i% n8 u
- // Insert the post into the database
* P, P/ X0 c6 E: F/ f - $ret = wp_insert_post( $my_post );, `6 n" j1 c }2 D9 d) t7 }5 D
- echo $ret;$ ]1 ?* m( i A2 w( c5 L7 L3 `
- ?>
# `# p0 |8 f3 K ~
复制代码 我自己写的
3 }, P* L5 J- |6 o插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|