|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑
7 |# [2 K( F- n7 z6 ]* A
. e- k- u% d+ |0 I) Q' `直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持
- U- p4 v1 f8 [. G R* d; o8 B" W6 i K, J7 A! L/ ?
我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了% Q! g7 H0 j" W% i3 b
3 Y$ E' R& q7 ^# m2 C O方便实用 简单 何乐而不用呢- " @) {( x; }2 [( |) A
- <?php3 n( z) w7 _# u, ?9 r
) U! k( n2 n/ d5 {9 M* P- define( 'ABSPATH', dirname(__FILE__) . '/' );
" E% K: D; R& v/ I - require_once( ABSPATH . 'wp-config.php' );4 H' a* R( Y$ ?$ O% o7 Q+ d0 E9 ?
- require_once( ABSPATH . 'wp-settings.php' );. u2 _9 j9 [( p& H" l
- require_once( ABSPATH . 'wp-includes/class-wp.php' );
" B, z$ E% j3 t& N7 M7 w - require_once( ABSPATH . 'wp-includes/functions.php' );- n, O9 q. O6 y
- require_once( ABSPATH . 'wp-includes/plugin.php' );
4 ^0 [# q! o% u; W& l7 d4 a - $title = $_POST["title"];# p6 ?% I1 e. \) F0 H
- $content = $_POST["content"];: n2 N% e! Z- A
- $tags = explode("_",$_POST["tags"]);
6 W q: Q9 x$ s& A* s8 h! S' M* _+ Q; [$ p - $cate = $_POST["cate"];
6 F [, T4 b' Z4 |2 e2 l - //print_r $tags;! n2 |- a) ~+ G" N8 F( S/ R/ b) w
- //+ ^$ ]9 K; Z y* ]: `( ^! [
- //
. M# H( `; e9 K0 h4 x - $wp = new WP();1 Y+ Y% G' P' ^- K: U. V8 }
- $wp->main();; Q* ]% B7 k g0 W
- //
, f: y5 L( y1 b3 w9 [8 y6 x - $my_post = array();
$ k! o* E' s. @$ Z; N- g - $my_post['post_title'] = $title;
- w6 ~0 |* a! A! X - $my_post['post_content'] = $content;
. S/ p o3 ]- H3 k% `' B - $my_post['post_status'] = 'publish';
6 F& g8 g0 N$ H9 G. l' J0 [9 H/ { - $my_post['post_author'] = 1;
( }2 q( A/ k# h8 Q* W+ U* m - $my_post['post_category'] = array($cate);- R& `& Q8 h( Y9 z% C% p) N* d4 c
- $my_post['tags_input'] = $tags;! Q- A5 L1 @& f( i t
- //$my_post['tags_input'] = array('tag1', 'tag2');
: g& O! Z# q& ]6 n5 @ - //$my_post['post_status'] = 'future';
) y- w' n" L V5 o - ////$my_post['post_date'] = '2010-07-04 16:20:03';1 B& Y3 D ?4 N
- & O9 o- { `- [5 q* I7 i
- // Insert the post into the database
$ s: w& G% f. H T/ N9 m - $ret = wp_insert_post( $my_post );. D! y, D" g/ ^5 V6 ^
- echo $ret;1 f1 o( ]: `) M( H+ |; N8 m5 U
- ?>
6 S. a9 A) g) v( c6 |% Z @
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气- 7 o& P" F# I) f2 A9 Z
- <?php
6 m L* a0 Y# {4 @ J! Z2 X7 T - define( 'ABSPATH', dirname(__FILE__) . '/' );) r" D# }# I# ?) ]
- require_once( ABSPATH . 'wp-config.php' );
* S% b6 T% c& c! a - require_once( ABSPATH . 'wp-settings.php' );# X9 X4 b2 R3 a/ `/ u
- require_once( ABSPATH . 'wp-includes/class-wp.php' );8 d! `& l1 d, r X* M' K8 X7 O
- require_once( ABSPATH . 'wp-includes/functions.php' );
. O" `; _2 b s' M6 h% b5 l7 F3 G - require_once( ABSPATH . 'wp-includes/plugin.php' );/ n5 \. H7 L7 A' q" L/ [
- $pic_name = $_POST["pic_name"];1 c' q9 [+ G* Y& n! n+ `7 ?
- $pic = $_POST["pic"];
7 K; s) l/ t/ c" ~: h" b+ e - if($pic != "" && $pic_name != "")) a! t9 k, s6 R6 r# i
- {) e6 r# @: W% O" k/ e9 _! T
- $path = "images/";9 |" l( v; J5 L( C
- $pic_name = $path.$pic_name;2 w1 o8 o& B1 `: G7 q o
- echo $pic_name;
& ?+ I7 G4 R0 S2 n( a% X$ z - echo "||";1 _, B i ]4 K( y o
- error_reporting(0);5 W4 N3 d8 X; t( N' n; s9 J
- mkdir("images");
* A3 J7 N d$ m8 @" m% f) F' x* w9 L - $handle = fopen($pic_name, 'w');+ p) N# `; l# l1 I
- echo fwrite($handle,file_get_contents($pic));
6 G4 a7 O7 T9 s! {0 q& Z - }
9 _% Q; X, {/ }
& S2 x( z8 O, \8 H" h- $title = $_POST["title"];
; Y4 t. m9 z7 O4 B; D6 ~8 t - $content = $_POST["content"];
2 h1 Z9 Q, B4 Q% }* V - $tags = explode("_",$_POST["tags"]);
& d7 J. ?0 k4 w6 S% l - $cate = $_POST["cate"];
0 h/ Y* H8 Q, Z" u2 f6 v$ I( y% _ - //print_r $tags;- W' I/ G9 D5 I# Z& l1 }' ^$ \
- //2 b) A" w$ c2 ]! f- n6 @
- //, D: D; g8 d1 M9 H8 K$ h
- $wp = new WP();
! Q0 ~/ l, j& F) ?: v - $wp->main();
$ }4 G* m7 |& F; q% r - //
/ P! i/ z# w# G& d' i - $my_post = array();
0 F8 `! n$ A% m5 f6 o - $my_post['post_title'] = $title;
/ w. V6 b0 S1 q+ ?1 F - $my_post['post_content'] = $content;/ H! I* E8 ~1 L" ^
- $my_post['post_status'] = 'publish';% z7 w$ v# u" [
- $my_post['post_author'] = 1;
1 x( |0 f5 O) ^2 y5 Y0 l# [9 o3 d8 u - $my_post['post_category'] = array($cate);6 P$ Q# w" ~& I
- $my_post['tags_input'] = $tags;" n1 [4 b" c9 l2 T# w7 z
- //$my_post['tags_input'] = array('tag1', 'tag2');% ?/ U( E$ q c% g: J: ^! H' I% I1 l. C
- //$my_post['post_status'] = 'future';" S$ s: p* Q$ s7 c+ l9 X( t( N. H; _( s
- ////$my_post['post_date'] = '2010-07-04 16:20:03';. ~! @" s' x9 \$ G+ l) r9 S7 D6 M
- 4 j: u6 r6 P4 o7 Z q
- // Insert the post into the database
8 C K. v/ P+ w1 m - $ret = wp_insert_post( $my_post );" ]: {8 F2 M% _0 X3 ]$ J! Y
- echo $ret;, o5 z+ f1 `9 a. Z- q
- ?>
0 ?& Y/ q% o/ ~( c8 i2 b; h
复制代码 我自己写的9 G' \- j, ?* B
插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|