|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑 9 h- q- q8 o7 \7 p9 y
. s/ r+ h% i4 w% K; o6 I1 B直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持
) I4 i; S {. V2 S2 J9 T0 t
+ r7 G M# }. w我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了) q+ G3 K5 e( }$ U, N- a, Y
, D) F9 ?' N2 j- O4 D3 ^9 k
方便实用 简单 何乐而不用呢
! B5 j0 t- y% f l( _4 d& G- <?php
- z2 |, m+ I: o0 O# a - 2 q' o, Z/ p5 s0 P5 g
- define( 'ABSPATH', dirname(__FILE__) . '/' );. y7 y$ S# y: k: x9 p% H% T, k
- require_once( ABSPATH . 'wp-config.php' );9 Q: @/ z6 Q4 K* n! L
- require_once( ABSPATH . 'wp-settings.php' );7 S' x" }* g# i; u3 \4 p( Y9 ^
- require_once( ABSPATH . 'wp-includes/class-wp.php' );, o& }9 b7 Z7 d. u/ r
- require_once( ABSPATH . 'wp-includes/functions.php' );
: Y( I1 A& i w4 f/ `- Y - require_once( ABSPATH . 'wp-includes/plugin.php' );- K. A% R7 Q) x, R J6 S
- $title = $_POST["title"];
/ p4 F$ ~( u' n! x0 V4 G - $content = $_POST["content"];
' T/ K: A8 X, K& i/ `+ Z - $tags = explode("_",$_POST["tags"]);, k* v/ j. A/ [1 u# @0 f4 A
- $cate = $_POST["cate"];
# [0 p$ r! ` j, j- O2 x9 Q- g4 X - //print_r $tags;1 U" J9 m+ e* t9 c, T# G
- //
1 j( R% ^) |9 h1 z% X% L - //- n- d, N( B( T; p
- $wp = new WP();
( ]; ^1 y2 j3 Q, B! ? - $wp->main();
0 D. h) R, p2 p9 S* {8 b - //
. H( J! m6 i7 A; I; T - $my_post = array();% n9 O+ N( y+ i3 b4 |! v) N. R
- $my_post['post_title'] = $title;6 g, P) g1 R4 W3 t
- $my_post['post_content'] = $content;
& ^9 m3 a% ~3 I9 v! s( S - $my_post['post_status'] = 'publish';5 G" i9 `" G5 P5 _
- $my_post['post_author'] = 1;
# p! x& Q: E. f, G- H; e - $my_post['post_category'] = array($cate);
4 f, ~; W( Z) w& t4 q' [: l - $my_post['tags_input'] = $tags;: W' C a. D) y+ ~2 ~
- //$my_post['tags_input'] = array('tag1', 'tag2');5 d/ ?% K+ b/ e! B% L' B1 e
- //$my_post['post_status'] = 'future';5 `4 G! y- i* I) }4 a
- ////$my_post['post_date'] = '2010-07-04 16:20:03';
9 o% t( V; {1 t @ - 4 Q* l. s: P" P( ]
- // Insert the post into the database4 G; ?6 R* ?: v4 W
- $ret = wp_insert_post( $my_post );
* A) z$ ]6 A/ K7 W0 e% w# D - echo $ret;4 u; q* x7 e5 a. r% w1 E; s
- ?>( a5 s2 s5 c! @! Q
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气
# Q" A5 q' b g0 o8 ~- <?php" b6 q8 H% J2 R/ P d) Z3 N* _
- define( 'ABSPATH', dirname(__FILE__) . '/' );
: S* i( o2 K! Y5 m- d" v/ Q. A$ K - require_once( ABSPATH . 'wp-config.php' );
t: T( {( O- U& P5 m - require_once( ABSPATH . 'wp-settings.php' );
8 C, q" N1 ~$ J - require_once( ABSPATH . 'wp-includes/class-wp.php' );
7 s9 A; w* G; {# S - require_once( ABSPATH . 'wp-includes/functions.php' );
9 [% L/ q7 K( [ - require_once( ABSPATH . 'wp-includes/plugin.php' );$ J! K. `7 ^- U, h% s6 ~$ l+ @5 t, Y
- $pic_name = $_POST["pic_name"];
6 a" W+ Q; }- N- S u - $pic = $_POST["pic"];
, u9 T1 L2 K0 f - if($pic != "" && $pic_name != ""), g9 L( d' d- O( i
- {! W9 K* e2 O; O5 G/ a" L- \
- $path = "images/";0 p9 m( S3 V( {; w
- $pic_name = $path.$pic_name;
2 `. G8 C- I4 h& ` - echo $pic_name;
$ U0 F' ]2 g0 ^0 k- _6 S - echo "||";
& } ?# G, [5 U2 w2 f, E4 O- Y - error_reporting(0);
1 b1 ?# K$ a$ z* p* F4 s) |, X - mkdir("images");$ l5 `, i6 @9 B" q
- $handle = fopen($pic_name, 'w');4 w0 L$ P( n4 ]+ w
- echo fwrite($handle,file_get_contents($pic));
- n3 m+ O: C+ p2 Z0 E3 o - }8 ?+ r: d2 v& D& f
- ) g1 B! @& E3 u( E8 P7 l
- $title = $_POST["title"];
! @! g% N* g+ Q, U6 p - $content = $_POST["content"];
6 q2 d8 z0 ]. P# r$ \ - $tags = explode("_",$_POST["tags"]);. j1 D% g" L8 X# p
- $cate = $_POST["cate"];$ F! m2 x6 h5 q; k) K1 V
- //print_r $tags;% ]6 L* ]0 Q8 d j
- //
7 r% l8 Y2 B$ H, ], ] - // e6 R" j4 E; m M7 Z+ e6 E
- $wp = new WP();
3 M! g* l8 T% \$ n2 [# I - $wp->main();, q% x0 n2 M6 J" F# H! v" B F
- //
' d. ?. a9 R: w& R2 g3 e - $my_post = array();
4 n/ K! v- E+ ^ - $my_post['post_title'] = $title;/ ^) x) ]! w8 ~5 @, U* ]) N
- $my_post['post_content'] = $content;
* w! Y* q( Z7 a2 z - $my_post['post_status'] = 'publish';* n, ?7 P; o: e" W) m
- $my_post['post_author'] = 1;# h0 x F! s8 w
- $my_post['post_category'] = array($cate);
5 x. o6 K$ R7 P% D; s - $my_post['tags_input'] = $tags;
4 F/ C1 \0 e8 ^7 D6 C1 Q - //$my_post['tags_input'] = array('tag1', 'tag2');0 {9 L6 w% J" W/ q. W, x$ _
- //$my_post['post_status'] = 'future';% s6 d, A2 P3 h) H' r6 F' n: \
- ////$my_post['post_date'] = '2010-07-04 16:20:03';" N7 R" k" ^2 j( K3 m
- 7 R0 y- z+ l# I1 D9 }$ z( q
- // Insert the post into the database
. g4 f* w1 t8 i$ U - $ret = wp_insert_post( $my_post );
; l. {- X) F$ ^0 E1 K9 p1 g1 t4 ~ - echo $ret;/ e8 g; c3 E% Z- L/ q
- ?>" Q9 \+ R+ Y6 ?- |/ b s6 [
复制代码 我自己写的
% R6 n& M5 t% t插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|