|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑 1 t! S: w( Y2 z9 u# B+ m
* k! |' {, ?2 S6 \3 v直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持0 O+ ~) ? y4 l; E. h2 O
. ]1 c1 w, G# u我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了
9 p8 k* s% {$ _/ a& K5 u
1 B% q+ R4 Q2 }$ `$ W7 l! V: |方便实用 简单 何乐而不用呢- ! y/ R" i) x; n$ Y# y
- <?php
/ f' ~7 y$ E8 S$ A7 U" {: P - , R' h5 Q. e' F7 M
- define( 'ABSPATH', dirname(__FILE__) . '/' );% c+ q8 f- A# h* Q* O
- require_once( ABSPATH . 'wp-config.php' );4 K9 S/ l' d- V- V0 g: W% n
- require_once( ABSPATH . 'wp-settings.php' );1 D2 V0 k# h9 k }3 `
- require_once( ABSPATH . 'wp-includes/class-wp.php' );
# @! y; y* ~+ R6 P. _# G8 V9 l. E - require_once( ABSPATH . 'wp-includes/functions.php' );
- G/ d* Z7 ]# G% p. T4 M - require_once( ABSPATH . 'wp-includes/plugin.php' );
/ i+ S6 `4 V' }9 o - $title = $_POST["title"];
# P5 B5 z; b9 ?' m - $content = $_POST["content"];8 J2 m& T0 K/ _7 V- w3 Y# B
- $tags = explode("_",$_POST["tags"]);) z* R- p) r, a9 }1 c) E
- $cate = $_POST["cate"];
9 F+ x+ h5 U7 a- w) \ - //print_r $tags;0 |" L$ h8 _1 m
- //
$ O0 U2 L. x/ H* D. y. Z - //
6 ^5 R* {6 W8 \/ H - $wp = new WP();
9 i- r' E7 s6 Y# X1 l - $wp->main();" L4 ]4 [) f# d
- //
. Y! r a* m. ^- s - $my_post = array();9 C$ m* l3 L* G/ }% T8 S
- $my_post['post_title'] = $title;6 C# P' ^* d- W6 h4 Y4 p9 g
- $my_post['post_content'] = $content;
- d! l! L* h+ W" C - $my_post['post_status'] = 'publish';) }9 R2 }4 H/ x" h5 P4 P7 i
- $my_post['post_author'] = 1;
^! }! U H! c+ t% i - $my_post['post_category'] = array($cate);
% A+ {' ?4 P7 S) R% f1 r - $my_post['tags_input'] = $tags;" Q2 h3 v8 I) p1 L/ C
- //$my_post['tags_input'] = array('tag1', 'tag2');
' G p* Z& y( o' u& v - //$my_post['post_status'] = 'future';9 X% r4 C" w6 @) P3 G' t
- ////$my_post['post_date'] = '2010-07-04 16:20:03';6 R3 i/ ?& ~1 v6 A( ]( }
/ a# q6 x" l, k1 y3 t. e- // Insert the post into the database
\ n% q( X1 S M - $ret = wp_insert_post( $my_post );
9 x: }5 h% t" O3 m - echo $ret;
8 }# v$ d2 k- X - ?>/ [/ I! l( o+ T0 w: e
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气
/ R# t7 ~ B; F6 Q7 e- v! U- <?php6 o) W6 r/ v0 S1 B6 v, q) [
- define( 'ABSPATH', dirname(__FILE__) . '/' );
3 Z. ?( E" H& j f# i+ U - require_once( ABSPATH . 'wp-config.php' );
7 c% j2 L% ^# L) U; Y" _ - require_once( ABSPATH . 'wp-settings.php' );$ f! m2 g; |' @9 @8 Y
- require_once( ABSPATH . 'wp-includes/class-wp.php' );! {( L$ c' ]5 P6 J3 |2 C
- require_once( ABSPATH . 'wp-includes/functions.php' );% I! f6 u, b- h6 q* {2 {4 O/ r
- require_once( ABSPATH . 'wp-includes/plugin.php' );
5 i5 V1 z/ ~4 y( w7 N( \8 s - $pic_name = $_POST["pic_name"];
7 h% X8 j. m3 `4 Q, p; Y+ f& |+ c$ t - $pic = $_POST["pic"];
5 X5 A; | m5 S( H7 ]1 ~$ u - if($pic != "" && $pic_name != "")* a- t F: R2 ?+ E: k D D# ~
- {
* |/ N+ V7 h3 J2 D5 W+ P! q - $path = "images/";' q9 ~3 N+ }' ?
- $pic_name = $path.$pic_name;
: U; h$ W) A2 L6 m* {, m - echo $pic_name;, d- c2 ^6 k! f4 s, V4 J
- echo "||";" ~ P. d- Q2 o& B9 x1 z& U' `
- error_reporting(0);$ K* k1 D$ T0 i- f0 b# O
- mkdir("images");
+ t& | G+ y' C* l* N& ?6 c2 C - $handle = fopen($pic_name, 'w');3 Z8 O% f7 z. c" X
- echo fwrite($handle,file_get_contents($pic));
4 B$ ~5 Z3 {! A8 B8 m {- E7 t& D1 n - }/ l5 U% @) E' g/ q
1 d, @2 i7 Q5 v8 D8 p- $title = $_POST["title"];& Q; p0 J' B- f9 ?
- $content = $_POST["content"];( S, V# d! A4 P" d. P
- $tags = explode("_",$_POST["tags"]);
O$ G; N, a6 A+ j - $cate = $_POST["cate"];
/ w' {) H; P! l9 c( o! i2 p% q% K - //print_r $tags;# U2 C8 e# x1 {
- //
/ f9 a! E' i7 r$ j, F u - //
. P" h) I7 C9 Q9 h3 k) u - $wp = new WP();
9 T4 Q w( \" [- f% e - $wp->main();0 u% u# _; U: u9 m" v& U
- //3 o. M" j% f7 G, M$ c* R; Q
- $my_post = array();8 O( i( ]$ I7 x# `2 X
- $my_post['post_title'] = $title;; ~6 U' |4 K. W
- $my_post['post_content'] = $content;& U+ q. N, n% s# h" p
- $my_post['post_status'] = 'publish';
! \9 d* a( @3 ?, P - $my_post['post_author'] = 1;) M4 b3 N: W6 g" A+ U# Q2 l/ Z7 w; A
- $my_post['post_category'] = array($cate);' t/ d1 I$ ~2 U/ C
- $my_post['tags_input'] = $tags;' R, j! p- t- k1 s7 B5 _& M
- //$my_post['tags_input'] = array('tag1', 'tag2');
3 k* A7 F- X! v, v% ] - //$my_post['post_status'] = 'future';- o9 }8 J1 t' @4 m( J8 n1 s7 [
- ////$my_post['post_date'] = '2010-07-04 16:20:03';6 b) z% [1 V& }. F- c" ]0 E: j- c3 ]
5 g0 ^/ ~) y8 _: }" |2 ~; O# o- // Insert the post into the database
5 v1 x: ^; F$ B; U4 U5 [ - $ret = wp_insert_post( $my_post );2 u8 n- ]$ q$ ?$ W" W5 m
- echo $ret;& z. ~9 G6 d' ?- [, M% k# b
- ?>! d' q# ]% H2 P- K+ f$ q
复制代码 我自己写的
. ]% @& f% ]& x K y插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|