|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑
9 V4 Z8 }: h; S+ t
) J4 h7 J" T% O) E0 J! @% V! I直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持) [3 \# E% \& b/ D$ ^: i+ g
( Z" D/ W. @- q" W
我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了0 C! y' g1 ~# @4 s* S( B* U( j
5 U- Y" E: |2 V1 W1 a0 E- m3 G
方便实用 简单 何乐而不用呢- # A( e1 G& U+ @) V, n$ p2 y7 Q
- <?php# I( q/ `) C" D! }# V
$ b0 a6 J- A, h ^1 U- define( 'ABSPATH', dirname(__FILE__) . '/' );% U6 d: U1 G8 m3 ^5 U
- require_once( ABSPATH . 'wp-config.php' );" I) W" h. N4 p3 ^8 v
- require_once( ABSPATH . 'wp-settings.php' );
* o" ?: w( r# g4 |- A5 E - require_once( ABSPATH . 'wp-includes/class-wp.php' );
. V* x$ J% F( G - require_once( ABSPATH . 'wp-includes/functions.php' );
# A' Z0 P/ [$ y8 L9 I - require_once( ABSPATH . 'wp-includes/plugin.php' );9 T* I+ P# j5 V( X. V8 N- y6 m, W
- $title = $_POST["title"];
7 V& ^0 }% _& O* R$ _ - $content = $_POST["content"];
$ C: g+ K6 Y+ w$ d - $tags = explode("_",$_POST["tags"]);% h9 C ^" W1 u/ k+ O0 _
- $cate = $_POST["cate"];
' X- a9 v* K7 L+ V: c- h# Q7 G - //print_r $tags;
/ d: y, o1 o2 d( c8 _8 A1 } - //
* Q! Y# s7 \* h2 J# [ - //3 N. u1 O0 r9 j; {9 R: T
- $wp = new WP();) I" m" \+ F9 U5 [6 T1 S
- $wp->main();
( X, l0 }% b% i. k$ c8 G - //
( h3 a1 m! T& w% ? - $my_post = array();
7 W9 l+ \" h0 D q8 x0 m C - $my_post['post_title'] = $title;" S* _& f% V: ?* `
- $my_post['post_content'] = $content;
% F0 g0 C# t* Y/ o- v# c3 Y - $my_post['post_status'] = 'publish';/ F% R- O9 d' N9 @- A
- $my_post['post_author'] = 1;3 b5 Q% z& {" p, i, W+ J9 z6 r
- $my_post['post_category'] = array($cate);
/ A4 u$ D' p9 D9 ~1 g8 b# G4 F - $my_post['tags_input'] = $tags;
( |4 g" }% F$ D1 \4 d - //$my_post['tags_input'] = array('tag1', 'tag2');
; Y6 J3 o" z/ e- V, Q1 d1 }. ? - //$my_post['post_status'] = 'future';% l* n2 ~: r: \
- ////$my_post['post_date'] = '2010-07-04 16:20:03';
; m8 H1 Z, w: `' u3 B1 U0 E2 }
) d. U/ A9 c1 Z; _2 m+ S8 u7 w- // Insert the post into the database
4 q! J2 w4 Y6 `' L7 a0 R2 G' W b - $ret = wp_insert_post( $my_post );
u. p' B% S3 |1 O5 [/ g) u1 d" W - echo $ret;
- K& \4 [' ] G' ?7 l; W V - ?>
) h/ o9 Q6 \# C, m" R% } W. R6 `
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气- 1 q% J" b& C7 w( ~' X$ P0 Y
- <?php3 \2 j$ K5 x' `# X( _" ~9 _
- define( 'ABSPATH', dirname(__FILE__) . '/' );
0 z5 p) y2 I0 C7 _! i3 f1 _ - require_once( ABSPATH . 'wp-config.php' );* Q4 w1 A6 ?6 D
- require_once( ABSPATH . 'wp-settings.php' );2 `3 K- C5 [$ t2 k( [$ F
- require_once( ABSPATH . 'wp-includes/class-wp.php' );* {/ k7 C9 h8 Y- s; }& g
- require_once( ABSPATH . 'wp-includes/functions.php' );
; W' \% e, n3 ^2 J L* R' Q - require_once( ABSPATH . 'wp-includes/plugin.php' );, a( |0 {# t4 L' j0 Y m; p
- $pic_name = $_POST["pic_name"];
4 M1 `. L! d, T0 n - $pic = $_POST["pic"];
8 ~) V* c( ?' u7 G) r' `& N - if($pic != "" && $pic_name != "")
/ T) ?/ R# {9 J7 ^6 } - {; I# }* _, s2 `) ]: Z5 \
- $path = "images/";3 D$ `5 O3 y+ g0 M+ a
- $pic_name = $path.$pic_name;; s$ ]; j% |% B0 f& ?
- echo $pic_name;! O% n7 `7 H, j6 r/ t( f; Z& [
- echo "||";
/ e( D' V, I2 ]/ J# D& J0 T - error_reporting(0);* ~' [2 Z# u/ ?! b3 @; Y
- mkdir("images");9 }% R3 @; F, E% `4 t; M
- $handle = fopen($pic_name, 'w');) T7 @0 w5 x; y4 U% z- N0 I
- echo fwrite($handle,file_get_contents($pic));
4 b6 B7 ^( A, l- Y0 f5 u: h0 ? - }
- e& ], R& F5 s% g1 { - ) g0 E# F. ?5 o1 \0 d
- $title = $_POST["title"];
! R$ j5 l( E2 u& V* ?0 Z- L$ L - $content = $_POST["content"];
" H3 a& M% F% n" G0 Q - $tags = explode("_",$_POST["tags"]);/ u7 M% i, j6 z
- $cate = $_POST["cate"];
& f1 y, m7 Z( b3 g& b3 P - //print_r $tags;
- A& q2 r6 ~7 K3 Z/ U3 ~$ O0 P1 Z - //
! K9 Y$ U" d6 M' n5 C$ F8 W - //
9 |- h6 x* i) H* h - $wp = new WP();
6 {6 M6 s0 k4 s6 H. | - $wp->main();% ?$ z7 Z4 M H- i9 e3 b) N( _& \
- //
" J' F6 }1 e, ?% P; \+ y - $my_post = array();5 s6 o" I$ O) B9 H
- $my_post['post_title'] = $title;3 z4 I* P, c; `6 b
- $my_post['post_content'] = $content;% f! d2 W8 f% C) [9 h
- $my_post['post_status'] = 'publish';
: ~ f& Q( B p - $my_post['post_author'] = 1;
! v0 w, N' {8 {+ U8 m - $my_post['post_category'] = array($cate);5 c1 e# W8 D$ k
- $my_post['tags_input'] = $tags;
% p/ L* R" E3 P* r. P0 h! [ - //$my_post['tags_input'] = array('tag1', 'tag2');
% `' }( ]! z, f5 ^5 V$ _, j( Y. ~ - //$my_post['post_status'] = 'future';8 O1 f2 v7 s# c/ K, E0 t% D
- ////$my_post['post_date'] = '2010-07-04 16:20:03';
1 t; k0 S* T% N _, W6 W& [* W# H - 2 V, o+ r" E' i4 j- e2 V
- // Insert the post into the database
: A7 Q: {/ a, v. t3 x( I - $ret = wp_insert_post( $my_post );* f" p7 J# B% V: S) T
- echo $ret;5 A( M0 Q, Y0 L+ d+ S
- ?>* o- S% D% B. Y7 O( M
复制代码 我自己写的
1 O3 H! i+ f- L9 b5 ^+ p3 P插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|