|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑
, z/ K+ b N0 v$ m* S
9 g) Q- k0 z8 n( Z2 l直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持0 ^7 z' s4 w. I" @ s u* _
& @/ ]( m+ i' M* n, G, a
我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了: f* G) l; N! g* b6 r
" \; c0 Z7 { K# k* E; t
方便实用 简单 何乐而不用呢- ; @) \7 `# @* h
- <?php
* L& i% i+ ^* { - 9 l7 j+ x$ v) u$ N9 k3 G
- define( 'ABSPATH', dirname(__FILE__) . '/' );
8 U( s7 I; u, l2 h" E; Z - require_once( ABSPATH . 'wp-config.php' );9 e& I! B( L* O/ a7 z& U
- require_once( ABSPATH . 'wp-settings.php' );6 ^5 ^& o$ x7 I m* L, A3 X# r+ Q% ^
- require_once( ABSPATH . 'wp-includes/class-wp.php' );" S; h8 r& q& i7 ~6 [. z" t( _
- require_once( ABSPATH . 'wp-includes/functions.php' );( {) t$ S) `" x) h6 V4 {
- require_once( ABSPATH . 'wp-includes/plugin.php' );
: y0 ^% J0 E' R) w3 V# L - $title = $_POST["title"];
) s/ m9 p5 B" M& z- \ - $content = $_POST["content"];
7 V a* Y1 f; Y3 v# n/ o, u. s - $tags = explode("_",$_POST["tags"]);8 o* t( y" ]1 @& I( X: f
- $cate = $_POST["cate"];
$ ?8 k; x1 z. A: U4 k - //print_r $tags;
+ h+ z4 I4 H" N1 W1 G - //& p" J ~: V, Y7 H) c$ i
- //
- P# J2 t$ i% C6 S& q4 o2 _" g - $wp = new WP();
: B Y- ~8 j, b" X: N - $wp->main();* h6 K5 a1 _4 X, H: ]. m2 j
- //: Q V3 |) P% B; v; I+ Z
- $my_post = array();6 F: P) Y3 R0 @
- $my_post['post_title'] = $title;
# b. d5 ^2 w4 ` M: Q - $my_post['post_content'] = $content;
: k) |* m; n6 ~6 U/ X - $my_post['post_status'] = 'publish';
7 x- H5 t" v& }% V$ S8 h. @ - $my_post['post_author'] = 1;
$ `7 D0 ]0 W3 M - $my_post['post_category'] = array($cate);. O( u* r& E2 P' f
- $my_post['tags_input'] = $tags;" I& J7 ^( O9 i! Y# s
- //$my_post['tags_input'] = array('tag1', 'tag2');
9 B2 }0 j4 ]- @' X: r d9 \/ f - //$my_post['post_status'] = 'future';
. @" Z( h. n- O! g - ////$my_post['post_date'] = '2010-07-04 16:20:03';
5 W0 a, t; L! O7 r X
0 C; K$ X) u5 m! s& k. B% U- // Insert the post into the database* K O8 ?5 m7 }2 w0 o2 V
- $ret = wp_insert_post( $my_post );4 G) b# N6 J" V; v
- echo $ret;
! C) M' X1 y) J% d3 M* T - ?>
( w( w. A; z" |- l6 r7 w6 c
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气
- y; P, p: O; d- <?php
& L9 B* P" I% O0 d$ A - define( 'ABSPATH', dirname(__FILE__) . '/' );
: c9 m: j+ n6 i9 v; \ - require_once( ABSPATH . 'wp-config.php' );5 ?' P+ Q" m* u! k3 B# ]+ R9 c
- require_once( ABSPATH . 'wp-settings.php' );% V! t1 {* a" T' Z: }& b
- require_once( ABSPATH . 'wp-includes/class-wp.php' );
; [& j r* y8 j7 f( p - require_once( ABSPATH . 'wp-includes/functions.php' );1 v: U ^ d( S' \- \* h
- require_once( ABSPATH . 'wp-includes/plugin.php' );( H1 k5 f1 h* N
- $pic_name = $_POST["pic_name"];
4 ^6 w& S+ B5 S, M' B - $pic = $_POST["pic"];
8 b7 f) \+ T# c. D- D - if($pic != "" && $pic_name != "")
V. Q. _" c4 I7 C - {" I4 H' V. x+ F& a! Z
- $path = "images/";" g" L9 x" I( l7 {- T
- $pic_name = $path.$pic_name;3 [7 |* h* j( U; `" X
- echo $pic_name;4 Z6 Q- \. _/ E' I5 Z0 T& E
- echo "||";
t) t( G! z% q; i9 z* c" q - error_reporting(0);
2 B: }; H& G3 c* U# S! x- v - mkdir("images");; S& f* D: A X" a- s0 l6 V- v) U
- $handle = fopen($pic_name, 'w');
' W! s' ]+ S) [7 ^8 e9 h) T8 | - echo fwrite($handle,file_get_contents($pic));
: a( T* @, v4 o1 A2 m% B - }4 ?3 v: z( k# s5 k
2 G: E( Y% F7 Q- $title = $_POST["title"];
$ t, C x9 t( O' v8 G) ? - $content = $_POST["content"];
' T1 m' q1 \. I; |5 }! t - $tags = explode("_",$_POST["tags"]);
/ u& L" X% o+ d8 e - $cate = $_POST["cate"];8 \" m2 v4 x; `, ^
- //print_r $tags;
: r8 } R5 ^& G/ X f( ]8 [ - //
; R. \- ^" h/ g% V - //
% p7 r4 i2 U3 J" A- ~5 B6 d4 {6 J1 { - $wp = new WP();
6 M/ B% O: Y2 q6 _* M - $wp->main();& J2 E8 Z- Z h( x
- //. F) z( }5 ?# b# d/ b4 h, o
- $my_post = array();
* @. f$ d& N& A( ?, ] - $my_post['post_title'] = $title;
2 k( g7 W H# m) N4 w8 a - $my_post['post_content'] = $content;
8 W B5 y, n3 S3 t' E - $my_post['post_status'] = 'publish';
2 Z$ y! G1 m" k& x; T6 i2 k - $my_post['post_author'] = 1;
0 _, {0 r4 z$ Y/ Z* H) h# a! B - $my_post['post_category'] = array($cate);
5 \/ u1 s Y v m1 Y/ v; f" e - $my_post['tags_input'] = $tags;4 i3 b# Q" g( c- ^: C8 H
- //$my_post['tags_input'] = array('tag1', 'tag2');" i6 L' g! x; A+ V4 M8 m
- //$my_post['post_status'] = 'future';
" e$ S9 a4 V) m6 d - ////$my_post['post_date'] = '2010-07-04 16:20:03';1 L5 D+ L1 ~& a }2 j% _
- 1 @0 f- W& t9 c! l2 P; Z; l2 {
- // Insert the post into the database
# n! ~2 ]3 z* X3 s6 X6 k - $ret = wp_insert_post( $my_post );6 e, x7 b6 B& m$ k
- echo $ret;7 ]! |8 F9 j3 Y7 ~% k
- ?>3 p3 _$ }) `9 b
复制代码 我自己写的
+ ]. X1 N- m; }; t) a2 Y$ D插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|