|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑
; F$ I* w, {0 ?5 g
# m6 p5 T5 d3 ?6 m直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持# f8 U+ e1 F/ v7 q) q+ K" B) }& F
% d3 {# q4 M% y: }7 _/ S
我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了
% R: H( o K9 m# R& L
+ p0 u+ v1 {( | i. A# m. h方便实用 简单 何乐而不用呢
- x: k5 b6 Z- R- <?php
" k3 o% M7 s# }2 d) H8 t0 G6 N- \- d - . U) p! G7 u( z2 S' w
- define( 'ABSPATH', dirname(__FILE__) . '/' );
: Z: c3 W2 K3 B6 E( V - require_once( ABSPATH . 'wp-config.php' );; B& U. `( b& m0 G! {
- require_once( ABSPATH . 'wp-settings.php' );
& I( U2 M0 S; M# Q$ k# |1 _7 ` - require_once( ABSPATH . 'wp-includes/class-wp.php' );
S w! r5 E0 R% l$ }0 T4 u - require_once( ABSPATH . 'wp-includes/functions.php' );: p( }4 a/ A9 o- {
- require_once( ABSPATH . 'wp-includes/plugin.php' );
3 m5 D6 J/ W' `7 Z# R9 L7 G - $title = $_POST["title"];
6 O" d- a. |9 R, A/ i - $content = $_POST["content"];
$ l, k/ N+ X+ J* q4 F - $tags = explode("_",$_POST["tags"]);* p* n& A: B) N( {2 ?
- $cate = $_POST["cate"];- Y7 D+ M! Z- P* V `0 M* k
- //print_r $tags;
/ B. a% ~# G3 b - //; Y' Y' }0 |: `; A
- //1 h: M. @/ d) ]
- $wp = new WP();6 K4 l' j7 ~. n- P
- $wp->main();. i p) i, \* f7 o+ @5 F
- //
2 p1 O4 I5 f& i - $my_post = array(); R, P# l3 C5 v1 L' A4 z
- $my_post['post_title'] = $title;0 m2 I* v5 Z5 S' }; ^4 V# s
- $my_post['post_content'] = $content;2 l( Q, ?, s* E: I, Q+ u9 b( ]/ b: ^
- $my_post['post_status'] = 'publish';
4 C! e7 Q$ T5 ^1 {4 L, c - $my_post['post_author'] = 1;/ y( D2 r, K4 e: W" a& j% F- \
- $my_post['post_category'] = array($cate);9 s J% d( s* c
- $my_post['tags_input'] = $tags;
" f; w" q( F% W" o9 M - //$my_post['tags_input'] = array('tag1', 'tag2');
' i1 a" Z5 ^/ f3 c5 b - //$my_post['post_status'] = 'future';; T- X$ Y; X0 u7 a
- ////$my_post['post_date'] = '2010-07-04 16:20:03';& |" t) w# q4 \8 j7 z
- " g3 z* n/ N) R' B$ d
- // Insert the post into the database
j+ @' e, n* b# G% ~ - $ret = wp_insert_post( $my_post );6 } ], |+ i" T7 j, ^" ~- Y
- echo $ret;
8 Q1 Z+ X' _; I1 O& I( r - ?>
5 u( D. r7 L9 o" D' `: X& j# n
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气- 1 L4 Q% k# e7 {; g$ Q1 M) P5 W1 D
- <?php
4 X5 p+ a& h- G& f) A; D - define( 'ABSPATH', dirname(__FILE__) . '/' );
, j; i+ B* [/ L, E2 _7 j( G - require_once( ABSPATH . 'wp-config.php' );
5 ~7 x8 ]( O% W$ b - require_once( ABSPATH . 'wp-settings.php' );
; k- d ?" Q) x5 L/ U" H' z - require_once( ABSPATH . 'wp-includes/class-wp.php' );
' q1 s" d! [! b' w$ ~2 \* c A - require_once( ABSPATH . 'wp-includes/functions.php' );
: _% h/ r1 t0 J - require_once( ABSPATH . 'wp-includes/plugin.php' );
# v3 T$ H ` K. P - $pic_name = $_POST["pic_name"];
, Z W( e0 f |% I. a( H - $pic = $_POST["pic"];/ E# n8 w6 n! q( t: u" E5 G. F
- if($pic != "" && $pic_name != "")7 x, L$ j1 x# l I
- {' P+ e, N1 ^7 a2 s1 w
- $path = "images/";
" a* R5 B$ B: }4 G9 o3 I- u3 a - $pic_name = $path.$pic_name;
6 h/ [/ Y* O; l, T8 m& Q" c - echo $pic_name;% P& d! X: Z1 O* l- w! z" K
- echo "||";1 l9 U2 L- h& ^1 J. }( {4 U% O d
- error_reporting(0);
2 X* d) B$ D8 E6 i - mkdir("images");0 M! B' b* I# ^0 X5 t1 M4 N
- $handle = fopen($pic_name, 'w');, h& A' c4 [$ L& t
- echo fwrite($handle,file_get_contents($pic));. ]$ F& E! V0 u9 X5 N0 E; n
- }
3 L: d" K- G$ P
& V5 Z4 u/ I0 R. i( i0 `3 [- $title = $_POST["title"];
7 c+ @7 X) ` l0 E' Z - $content = $_POST["content"];1 W) u3 D) y8 G9 ?# f/ l* c0 F
- $tags = explode("_",$_POST["tags"]);
6 N9 \& |! m* e [ y% d/ I, g/ b - $cate = $_POST["cate"];
$ a: Q- }2 k N% T' O: ]9 h! l* g - //print_r $tags;' o: k& D) r, M" C) _
- //" ~9 ?$ C- c4 \/ n8 w
- //
+ ]2 i$ g' u: }5 U i! n) R - $wp = new WP();
+ ^% d& y6 `6 {1 D - $wp->main();5 {( r+ J9 W1 v& s2 ~3 W
- //7 r" H2 {7 G1 S. L
- $my_post = array();/ J9 d. S4 C$ _ Y' J# Q1 ~
- $my_post['post_title'] = $title;
" E3 K a; r7 Q; o5 ?9 T, n3 b* b9 A3 X1 z - $my_post['post_content'] = $content;
- I3 q; ^. i' N. V4 o5 c0 T - $my_post['post_status'] = 'publish';
/ P: L/ d+ k' M$ l) h - $my_post['post_author'] = 1;
9 s S, g0 v2 _ x. r - $my_post['post_category'] = array($cate);
, ~7 o% g, |5 H - $my_post['tags_input'] = $tags;
& Q9 P* q% W- @ - //$my_post['tags_input'] = array('tag1', 'tag2');
4 L( O5 w; w$ X/ U- }( R - //$my_post['post_status'] = 'future';
" b* B& F' e) l9 p - ////$my_post['post_date'] = '2010-07-04 16:20:03';
9 ~9 v$ d$ J7 m3 v. N - & O) k! R. g* L' G5 b
- // Insert the post into the database0 Y6 c; ~8 t7 ^2 ?) W5 Z
- $ret = wp_insert_post( $my_post );6 w0 G ]6 [9 M1 P
- echo $ret;
' v7 \+ ~) h8 V4 b' b$ y- H; _ - ?>
: ^- q% K! a! ]( e8 O
复制代码 我自己写的1 C9 n9 t9 ?" i8 Y
插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|