|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑 $ y' x- n, I. s( S% y" o, j
3 M( I7 ~8 a" j) P a( G, k
直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持& A$ G- W9 O3 w* c x7 n
: _, k$ e4 S+ |" q
我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了
9 o3 e) o& j! Q. j; P) o$ W9 R3 a. m
方便实用 简单 何乐而不用呢- & U4 D6 s, t3 x! m0 B7 ~
- <?php
; n9 v& S- H4 m1 n5 Z2 v - $ t" n2 A8 x( J. m( a- @) g# I- O6 T
- define( 'ABSPATH', dirname(__FILE__) . '/' );
# V" x' F' p9 Y8 ^% N - require_once( ABSPATH . 'wp-config.php' );! B9 {" h! ]; T$ t$ k
- require_once( ABSPATH . 'wp-settings.php' );7 @1 y: b5 s3 E* K' U* I
- require_once( ABSPATH . 'wp-includes/class-wp.php' );4 u. k! }0 `5 @% r! ?9 m5 _: V
- require_once( ABSPATH . 'wp-includes/functions.php' );
; |! s& i: C9 G( g) L0 P - require_once( ABSPATH . 'wp-includes/plugin.php' );
; w* Q' A, e+ S9 }" @ - $title = $_POST["title"];
5 v5 y5 b" ?/ `/ i5 z- Q- i' x - $content = $_POST["content"];0 Q9 ^% X4 C, t; o% X
- $tags = explode("_",$_POST["tags"]);
, _, h5 ?7 f$ i! A* W) t6 u. O - $cate = $_POST["cate"];
* t- C& E/ L9 e9 {; P& C$ a - //print_r $tags;
1 x; h. e3 x: h% w7 U2 l - //. s1 H& Y* z, k2 j4 X {
- //
9 J$ F. c+ r+ |9 e8 T2 h - $wp = new WP();5 w j5 d/ D; P7 g g
- $wp->main();- r4 J( B, A1 M$ j3 Q
- //% F6 [- V# j6 k2 d5 o# r
- $my_post = array();. w! v; T$ l1 s! p( u' P6 K8 _1 n
- $my_post['post_title'] = $title;
( w( j A+ a- P/ t - $my_post['post_content'] = $content;, R1 g7 J% f: N) q
- $my_post['post_status'] = 'publish';
, S9 x2 A' T1 [ y, C% l - $my_post['post_author'] = 1;
0 a* Q/ R/ F' {, u6 z - $my_post['post_category'] = array($cate);4 c: {" ?" ]8 G% Y
- $my_post['tags_input'] = $tags;
- |8 U! ]% n5 l" W; l e, J - //$my_post['tags_input'] = array('tag1', 'tag2'); t6 u0 Y; M6 D4 h( ?3 ]& e+ n8 Q
- //$my_post['post_status'] = 'future';2 h' x+ t1 k$ z; `
- ////$my_post['post_date'] = '2010-07-04 16:20:03';
' _% n. Q9 i0 w2 k$ I - : q4 }+ G" I9 O! h' \# }6 ~
- // Insert the post into the database
0 R) d9 C- B+ u0 S2 G - $ret = wp_insert_post( $my_post );
' q r! V* S0 @ - echo $ret; h9 [: m- i4 }; |4 D' T) @
- ?>% H! Q8 L; u, w, W9 G
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气- # u2 U9 m' E3 G" G7 `
- <?php6 Q( m: r- R# c0 [5 A+ l
- define( 'ABSPATH', dirname(__FILE__) . '/' );7 K" y* W- R( e& [( r: t% T( ?
- require_once( ABSPATH . 'wp-config.php' );
; p5 e& t6 f# I i0 w - require_once( ABSPATH . 'wp-settings.php' );
5 |& ]4 V, [1 N" y - require_once( ABSPATH . 'wp-includes/class-wp.php' );( G2 [1 Z5 O( G6 o
- require_once( ABSPATH . 'wp-includes/functions.php' );
- @% R# f5 W4 z) ^) _ - require_once( ABSPATH . 'wp-includes/plugin.php' );9 W/ [# n8 g/ [# u; j! |6 I
- $pic_name = $_POST["pic_name"];
4 H- M) a$ G8 L5 z - $pic = $_POST["pic"];
5 O7 a8 q5 o; [9 C N. h S - if($pic != "" && $pic_name != "")
6 F5 P& i( K% a( A# `3 F - {5 }" K! J4 l) V# B& y$ w& O$ J
- $path = "images/";
1 v* l n: t# @ - $pic_name = $path.$pic_name;' T9 x1 w2 ], V
- echo $pic_name;
U- x, n0 i3 i. u - echo "||";
1 _, b# v# z7 @3 Q, }' s - error_reporting(0);
" Z" \ z7 k9 |& X2 C - mkdir("images");& o% J3 }' A: N# o/ v
- $handle = fopen($pic_name, 'w');
( X* H% _" R1 q \" Z* @ - echo fwrite($handle,file_get_contents($pic));) K% R+ Q2 |& E) H# j
- }
3 O' w7 m# X0 c U9 x$ Z: a' V' Z - ) b$ M4 |. H' g0 S$ u0 j9 c
- $title = $_POST["title"];! [6 z6 X6 f0 x: H. v4 N* U. A/ X
- $content = $_POST["content"];
) q! F/ y' [' V# U' C* A - $tags = explode("_",$_POST["tags"]);$ k# z% f* F! o( @4 b
- $cate = $_POST["cate"];
/ N4 E6 k- P# ]+ ?# s - //print_r $tags;
! F$ z: g, }: W% |1 H) G8 j - //
4 X9 W; ^: t- I: U - //
+ k; b0 @3 k3 H1 S - $wp = new WP();
5 b* U4 g9 T' o2 t( S# E - $wp->main(); o1 J4 T7 J) r* V# I
- //; E5 W0 g, V2 s' d1 v" `3 u
- $my_post = array();# @0 S5 g: u# e; T: T! d9 D
- $my_post['post_title'] = $title;
+ g" v7 M& m! N# U9 d4 I) T g - $my_post['post_content'] = $content;
, A6 _4 J5 z6 ^' S - $my_post['post_status'] = 'publish';% s6 l# h; A& {1 d
- $my_post['post_author'] = 1;. Q* b/ C/ d9 ~/ E d3 K2 k
- $my_post['post_category'] = array($cate);
7 U- s/ b* ]8 G% i9 z6 M. K - $my_post['tags_input'] = $tags;
9 Z$ X% c3 ^% s! _. O - //$my_post['tags_input'] = array('tag1', 'tag2');
2 c$ P3 w" ~5 k; x; }9 t2 ` - //$my_post['post_status'] = 'future';
: B+ p: Y2 o% N! j% [" e - ////$my_post['post_date'] = '2010-07-04 16:20:03';4 J6 \' o& \" D( _# J2 g
- }* D8 l% V0 c8 J9 @- // Insert the post into the database# m8 z( t0 S% B2 C' X- v2 K, n
- $ret = wp_insert_post( $my_post );
* h" B# f9 D1 z9 h- A: z" V( D - echo $ret;5 v9 H8 [/ r3 l5 S2 I* m
- ?>
0 z; f" ^; Z+ {' o% v
复制代码 我自己写的
/ e- }2 A. _3 q' k8 [- m; G插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|