|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑
; A$ _# `# v6 c: l6 l, P! ^/ w; [( P3 l3 c( Z! M; N* C5 M& v
直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持! A. ^7 S2 P( j, m3 T) L
$ r( k! x- j- D/ p
我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了
2 e7 o4 c) c( r- e) ? s& u
" G, H( O0 f9 o& y3 {4 t方便实用 简单 何乐而不用呢- $ k5 w4 }6 v. d4 ]" R
- <?php& Z$ q) `& Y* n; c: ~* f% z
* C+ V# j9 |/ d7 `- define( 'ABSPATH', dirname(__FILE__) . '/' );" W Q2 k4 P! I1 ]8 q9 V
- require_once( ABSPATH . 'wp-config.php' );, c! W( l% {$ w$ i
- require_once( ABSPATH . 'wp-settings.php' );9 ?! h! r& h' w
- require_once( ABSPATH . 'wp-includes/class-wp.php' );# K' D3 ~# E& I, M$ v( `$ K
- require_once( ABSPATH . 'wp-includes/functions.php' );
9 o' Y: l# }4 q, g: U4 e - require_once( ABSPATH . 'wp-includes/plugin.php' );
8 o n. ^; I, V- D( ~7 c - $title = $_POST["title"];
4 }5 ]5 D! i: n% E' v% ^ - $content = $_POST["content"];
9 d5 x6 A: ~" a' E9 j/ S - $tags = explode("_",$_POST["tags"]);
+ N& t4 K3 |' L: D: L' X1 c - $cate = $_POST["cate"];6 D5 P! ]& a4 _3 O" t: Y
- //print_r $tags;
& E B; r! r, d0 F9 S - //
% p. N/ a# h3 W; J* V2 V) e0 y( z - //
7 d: S& ?( j$ b - $wp = new WP();
( M+ v& d; e1 y! \- ^, q Y2 K, I - $wp->main();
. H0 B/ r9 S. O0 y2 E6 A$ V, g - //
' X# l* |6 e% P - $my_post = array();$ c2 d8 \( o2 g2 {9 i
- $my_post['post_title'] = $title;' j' ~1 j( I+ [+ n7 k& v2 }
- $my_post['post_content'] = $content;5 E/ j! m, J- m# G! X
- $my_post['post_status'] = 'publish';: l" Q5 R I& o7 l* a/ K" O
- $my_post['post_author'] = 1;
4 r) W |0 \5 A( \ - $my_post['post_category'] = array($cate);$ E* J, r& d! U0 j9 x* s" m
- $my_post['tags_input'] = $tags;
5 B0 h% g6 S8 C3 f0 F - //$my_post['tags_input'] = array('tag1', 'tag2');0 o% b" d. n3 \
- //$my_post['post_status'] = 'future';, {0 l% Y8 h; ]* L @1 A
- ////$my_post['post_date'] = '2010-07-04 16:20:03';6 d) `) z, ?, E4 ~; s( Y( n) j
- ; C6 U2 ]( T; B- b$ N
- // Insert the post into the database3 @7 o3 c( f9 i. r. g- D
- $ret = wp_insert_post( $my_post );1 @/ }" ]9 v+ _! y; ?2 ^
- echo $ret;
6 I& d' F/ s) j# i - ?>
7 [' {$ M. @+ J
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气
3 b2 e* t! P7 U- <?php9 `; `2 V& }) j5 ^7 U
- define( 'ABSPATH', dirname(__FILE__) . '/' );9 r8 U; L( a+ ]4 \
- require_once( ABSPATH . 'wp-config.php' );
0 w7 \6 @: C; R2 d& s - require_once( ABSPATH . 'wp-settings.php' );
) Q; ^4 D: g& K4 a - require_once( ABSPATH . 'wp-includes/class-wp.php' );, i# j7 L5 w7 t7 C
- require_once( ABSPATH . 'wp-includes/functions.php' );9 ^2 _* S# i' j' | O5 l2 H% | P
- require_once( ABSPATH . 'wp-includes/plugin.php' );
5 w( [. m6 C4 O2 T - $pic_name = $_POST["pic_name"];; d2 D% H. y. T( m& X
- $pic = $_POST["pic"];* x$ T- y7 E: F& @4 _1 b6 s
- if($pic != "" && $pic_name != "")- \8 q" `) [, b7 h- ^. `
- {
2 T0 |, F: m. }. |! x: R' e - $path = "images/";
- x: ^3 c& h) N - $pic_name = $path.$pic_name;
T) S8 g+ E) X0 [ - echo $pic_name;
( T* H: K6 }7 g - echo "||";* E# S# S z, l4 T' \5 A
- error_reporting(0);
2 d9 o5 }% s; a6 v - mkdir("images");
0 K9 @& U; ]8 Q! ]: y0 n - $handle = fopen($pic_name, 'w');
t- p2 K6 j5 L0 e$ B i - echo fwrite($handle,file_get_contents($pic));) U$ V+ G# w& Q+ @8 ?1 W
- }
! V) r4 T" I' p/ J$ ]! e! m3 d( F6 f - 7 I& N, s4 ^, `) L% Y; a' y
- $title = $_POST["title"];
+ z; E! U9 A" u+ p - $content = $_POST["content"];( c r! f- M/ Z- S; F
- $tags = explode("_",$_POST["tags"]);7 l, Y' d2 P- l
- $cate = $_POST["cate"];* c7 ~6 W5 h3 k$ V
- //print_r $tags;
' P+ T/ g; H. r7 r7 ` - //
4 J& S+ s. h. e. M+ `/ } - //+ `* L3 B3 ]' x) J2 r
- $wp = new WP();( |' a+ G3 B# a0 u
- $wp->main();% Z% U j# q) I& u1 S
- //$ r1 K" O! ^# h8 E/ ?* F
- $my_post = array();
" Y) H- Q; L9 [9 t9 X; f3 e - $my_post['post_title'] = $title;
+ P( ]4 J( O$ ^; K, q! J1 l - $my_post['post_content'] = $content;
Y) {$ m; o3 h; _' s - $my_post['post_status'] = 'publish';
& ^5 `) O. z o: l$ @ ? - $my_post['post_author'] = 1;8 _6 P9 i1 n3 Q$ x( G' m8 N) J4 d
- $my_post['post_category'] = array($cate);
2 g1 _: G4 ]7 x8 Q& w. z3 f - $my_post['tags_input'] = $tags;5 G( D; x( c. \% g, c
- //$my_post['tags_input'] = array('tag1', 'tag2');3 {: W, |5 C( k# k
- //$my_post['post_status'] = 'future';( v8 E) n& O' x% V$ ]
- ////$my_post['post_date'] = '2010-07-04 16:20:03';4 v8 K; d l# m8 H: q# V, N
- ( y- a9 I1 Z' A4 t4 E
- // Insert the post into the database, L: C3 r% N+ y! y" e
- $ret = wp_insert_post( $my_post );1 ] I% q/ F1 F- h
- echo $ret;1 h0 f" `& n, J3 @8 s/ q8 S
- ?>- q' P' R2 K/ Q3 N9 h- R$ M
复制代码 我自己写的! O6 F) U7 ?) x* \
插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|