|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑
! L" o6 h9 x+ M6 \0 @+ D% t
0 u u. a! G' d1 l直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持
% O4 A9 i% J. e R4 ]/ Q0 t$ }6 a1 z0 R2 ^& z' O: q* G
我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了1 o$ y5 O4 Z i! y2 V
- m* e; u4 ~( a
方便实用 简单 何乐而不用呢- 1 X: e$ n( b7 L& o
- <?php
/ T8 \7 E& `5 U/ R2 h" a6 t' e' D. R
- N8 v: w% I# P& U: q8 ~( ~3 [- define( 'ABSPATH', dirname(__FILE__) . '/' );9 [, d+ x* E, P" z6 D
- require_once( ABSPATH . 'wp-config.php' );
; M9 o# ?+ I- B. L+ S - require_once( ABSPATH . 'wp-settings.php' );: I4 ^: C7 i* g( s' Y7 A, U
- require_once( ABSPATH . 'wp-includes/class-wp.php' );/ \! j4 c( J& }3 F. e# e; d
- require_once( ABSPATH . 'wp-includes/functions.php' );
2 W& z. r; J; I3 D. ^ - require_once( ABSPATH . 'wp-includes/plugin.php' ); j1 J; H! }5 n( q' \9 c% `8 u- Y
- $title = $_POST["title"];4 _+ k4 H% Q( ?' k- |
- $content = $_POST["content"];
/ ? D; E5 `/ a H4 F - $tags = explode("_",$_POST["tags"]);- c& R$ T2 r! h9 L% S: p
- $cate = $_POST["cate"]; V, V3 a9 ?2 _( p' R
- //print_r $tags;
9 ]0 f# f% }4 n: S$ m* _ - //: p- @2 J9 l0 F8 C6 j2 M' i
- //! B8 _) C" r& p7 V
- $wp = new WP();
* P; _& u1 z0 ~2 { - $wp->main();
+ Q. i- j; L% f1 ~: a m( F# h - //
; N {7 [* @% l4 _ - $my_post = array();( @* a2 G* M0 V) B2 k+ m
- $my_post['post_title'] = $title;
l% s( Y4 }5 [/ W - $my_post['post_content'] = $content;
% v- ?. x G' M9 V - $my_post['post_status'] = 'publish';" y) d3 \) A4 n
- $my_post['post_author'] = 1;' d' F6 {- R5 J! | ^+ {
- $my_post['post_category'] = array($cate);
, r2 y; J! A1 d9 U4 T) M% d0 z - $my_post['tags_input'] = $tags;
* W1 C7 P( f/ ]. q- C/ B# r( S1 u - //$my_post['tags_input'] = array('tag1', 'tag2');
% c1 v- r/ ]' @! t. ~: x- I - //$my_post['post_status'] = 'future';
# S7 g% G; _+ f m - ////$my_post['post_date'] = '2010-07-04 16:20:03';
$ e: Y! [& i" N+ [/ W/ V% G
' T; u% C2 f9 F. d5 r% k- // Insert the post into the database
3 f. U% h4 o! F1 g - $ret = wp_insert_post( $my_post );( e& i e1 L) I
- echo $ret;, p* g% p4 ?, z( n8 H ^
- ?>
K5 X& I; I L; S, v* K( ~
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气
5 a. [- ~2 \% C" a$ U- <?php
: e- j# j( s& {- y - define( 'ABSPATH', dirname(__FILE__) . '/' ); K/ s. R: s9 w0 Q
- require_once( ABSPATH . 'wp-config.php' );
- }2 L# S! {; N$ U/ S% t* k& n; O - require_once( ABSPATH . 'wp-settings.php' );0 G& \+ r6 Y5 q0 l
- require_once( ABSPATH . 'wp-includes/class-wp.php' );6 H/ P7 ^$ B9 l: E- S1 a6 I a
- require_once( ABSPATH . 'wp-includes/functions.php' );9 E0 v% S) R/ x
- require_once( ABSPATH . 'wp-includes/plugin.php' );5 M* Q. z- O1 ?2 p+ B+ Z
- $pic_name = $_POST["pic_name"];
9 w/ j' d7 b( Z v7 B: R5 w - $pic = $_POST["pic"];
+ z! l9 k: ?& u [% S - if($pic != "" && $pic_name != "")
% K& y" q/ ?2 Z7 m - {
7 v8 m- K* Y& o4 H - $path = "images/";3 L- A5 Z* u" a1 ]0 o3 L
- $pic_name = $path.$pic_name;
# h+ T0 }/ L' L- _- j - echo $pic_name;/ h" \$ q. _: N% ^' N, C ^
- echo "||";7 q8 D3 v0 q; j+ u0 K j
- error_reporting(0);! ~! N2 u* ~' k( I" z
- mkdir("images");
! B7 J6 |1 t- B9 g* V - $handle = fopen($pic_name, 'w');* |5 j3 H7 B; b5 }
- echo fwrite($handle,file_get_contents($pic));
6 }) I4 V; \3 J - }- {4 R! }* D, T+ W n1 n3 N: u
8 A4 y) u3 C: p% x- $title = $_POST["title"];! c9 C# t" E5 }# a- J; J, `
- $content = $_POST["content"];
' `$ M& W' _* b! w( M+ P2 B - $tags = explode("_",$_POST["tags"]);, g, X0 A, S& S. J; Z% P8 A
- $cate = $_POST["cate"];# t6 w1 r6 n2 @
- //print_r $tags;8 |+ o3 H+ q" B! T1 D* N# s
- //
9 N% S+ m+ H* C# P# t( d3 B - //
. X1 O' h$ p% p$ R% g - $wp = new WP();2 ~1 }. n3 f2 R
- $wp->main();" s Y, C2 F6 u
- //' F0 K2 v) b1 V5 D
- $my_post = array();
4 } B. J7 ^! v/ ?% ~3 _2 n - $my_post['post_title'] = $title;
& ]7 X- _7 ]2 N - $my_post['post_content'] = $content;; G" r& G1 X- T6 V4 ^( F
- $my_post['post_status'] = 'publish';: S5 ~. f Z- g8 P' ]8 g
- $my_post['post_author'] = 1;
/ c7 h$ n0 J/ ^+ S/ C - $my_post['post_category'] = array($cate);
. O1 P: _* y$ H - $my_post['tags_input'] = $tags;( e3 x0 y9 l' V+ X% T% l3 n
- //$my_post['tags_input'] = array('tag1', 'tag2');
( C$ _, w. k2 o$ _% S - //$my_post['post_status'] = 'future';2 {+ i( j6 G; l! X( t) C: @7 i
- ////$my_post['post_date'] = '2010-07-04 16:20:03';7 b$ E0 D7 w7 I( J
: Z+ w* l: H8 [. d k$ a* g8 W- // Insert the post into the database
* @0 Y* _* p/ q - $ret = wp_insert_post( $my_post );
7 k. ~* j2 S+ T( O2 i - echo $ret;
{7 |" w& b# r2 `! a; b# B - ?>, W7 c$ m. k& b, o
复制代码 我自己写的7 Z2 t' F* i) E$ f, `5 \
插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|