|
本帖最后由 finder 于 2014-12-28 15:13 编辑
3 f* R9 \( U$ u4 D* o. W8 Z! v B" P/ g( D1 F" M+ y# @1 t
直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持
) j c& X3 O# g2 Z$ }/ ~$ X' {
1 B3 H& \% m7 W8 c/ [1 Y6 `+ c我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了" K4 K3 S# [# O
/ v, `. W! v2 N4 I4 y6 q+ k
方便实用 简单 何乐而不用呢- 6 R% E2 E* S5 X& P. Z
- <?php$ M8 ]7 x3 c' t# W% ?
+ b! O( W, D- c4 @) W; k- define( 'ABSPATH', dirname(__FILE__) . '/' );' K' _7 P3 k" m9 M- H
- require_once( ABSPATH . 'wp-config.php' );' F" O. U6 q5 ]; q2 s5 S
- require_once( ABSPATH . 'wp-settings.php' );
! }% s, C. Q. Y1 r - require_once( ABSPATH . 'wp-includes/class-wp.php' );
% h7 ^% ]- s* t' o- F, y$ v3 H, x - require_once( ABSPATH . 'wp-includes/functions.php' );& f! w9 e8 f' q; t- g5 T
- require_once( ABSPATH . 'wp-includes/plugin.php' );
2 v1 v5 K5 D/ x( k0 I - $title = $_POST["title"];
7 W" {, @1 l3 ~9 u( R' G+ p- o - $content = $_POST["content"];7 q+ m7 e+ M8 d/ Y
- $tags = explode("_",$_POST["tags"]);: [: q6 k8 C/ o( I8 x& K7 _. E/ a
- $cate = $_POST["cate"];
4 f, h0 x6 F4 N. l - //print_r $tags;& H4 w3 {7 Y5 H' X. s
- //
* @- z1 L1 J2 E8 @ - //6 Y& @8 t0 F W
- $wp = new WP();
1 ]# F j0 w+ O9 \/ T - $wp->main();
* [6 A9 z) ?6 ~4 { - //& ]8 b% p8 h9 g7 H" o; M
- $my_post = array();6 l( o3 g/ N; [
- $my_post['post_title'] = $title;2 U3 a' j; P8 c/ H" V/ F
- $my_post['post_content'] = $content;) S3 S: \3 W2 ?6 r
- $my_post['post_status'] = 'publish';: y' v/ _% |3 _% d
- $my_post['post_author'] = 1;
3 f; Q2 P% n# P, C0 q+ Z7 ?4 o. E - $my_post['post_category'] = array($cate);
! L! w) K, {* h1 r - $my_post['tags_input'] = $tags;5 K( j( f6 ^( o/ z( g [5 V$ Y
- //$my_post['tags_input'] = array('tag1', 'tag2');
. }3 f0 \' S6 T K - //$my_post['post_status'] = 'future';4 w$ y9 X2 E! Q+ J6 h7 c& ]9 K, r1 _
- ////$my_post['post_date'] = '2010-07-04 16:20:03';% m2 s9 K/ N4 K/ u6 f0 L
- 4 j; ^- H) I" E- K' I3 T
- // Insert the post into the database
* Y; i4 q8 K8 |* J - $ret = wp_insert_post( $my_post );# |$ K* o, k- k! E9 I+ L
- echo $ret;& t. D0 o4 {2 q) N/ A' \+ M% S
- ?>
# F* f V; L( d
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气
/ l; I9 H- x3 @% ^- <?php1 n3 _, }" N8 t( C& K k# U
- define( 'ABSPATH', dirname(__FILE__) . '/' );
4 N3 D- g1 v* c' @ - require_once( ABSPATH . 'wp-config.php' );
- D0 w5 [3 @; l& u G- M - require_once( ABSPATH . 'wp-settings.php' );
) S6 N8 X# w$ [3 }9 Q5 b( ]6 p - require_once( ABSPATH . 'wp-includes/class-wp.php' ); ~) Z+ K$ F, Q- O) `5 E4 L
- require_once( ABSPATH . 'wp-includes/functions.php' );
- W1 |2 r4 X! J8 Y- ~( t - require_once( ABSPATH . 'wp-includes/plugin.php' );
( f5 z2 c4 X) z$ n* ?2 P - $pic_name = $_POST["pic_name"];
" y* s9 v" [+ O2 h+ ` - $pic = $_POST["pic"];) W5 ~0 s0 t. ^) X0 B. d9 v' @
- if($pic != "" && $pic_name != "")9 k8 m$ V' U. J+ [9 w
- {1 l1 t) M6 B" e4 N
- $path = "images/";
' q- x. M" d: r! B+ I0 z" X( o - $pic_name = $path.$pic_name;
" f9 i: p6 C& ?; V4 Y2 W - echo $pic_name;
8 N( b% [7 }/ i: x - echo "||";+ m% W7 t/ g5 g- J. s3 `) ~
- error_reporting(0);
, Q' A1 [" n; Y) M) l9 Y, }* I% K - mkdir("images");, d4 _5 r3 F d
- $handle = fopen($pic_name, 'w');. ?+ v4 j. k. V# q4 e* {0 Y$ s
- echo fwrite($handle,file_get_contents($pic));
' |% y0 f. g0 E- }6 w - }, z) B9 d% q# K1 P
3 {# m4 X( g2 Q% C' J0 B& e( r- $title = $_POST["title"];( P$ z# P& l3 l; s& M; j
- $content = $_POST["content"];
$ J/ [2 _/ ?2 S! A0 F - $tags = explode("_",$_POST["tags"]);
8 |6 \& `' T# |3 R - $cate = $_POST["cate"];
7 y% J; E w( \# @, _) B/ { - //print_r $tags;9 q- v/ a; P% R' ]% z
- //3 \2 c% H5 _1 o8 A5 T3 H
- //
, J* f1 I" H5 {" {0 P( k - $wp = new WP();" D) _+ b0 @% T
- $wp->main();" H1 ~# n- C R4 @* P! F& t+ X) C U+ ^
- //. K" h4 g L5 w. p) K
- $my_post = array();
2 N6 Q* Y9 Q! ]$ J7 ^) O) ]; \ - $my_post['post_title'] = $title;7 V. }( S- P5 q: b+ n* ~5 m
- $my_post['post_content'] = $content;
# Q3 i. c& N* m: z1 g - $my_post['post_status'] = 'publish';! r/ A$ w. x) u& `( d8 `
- $my_post['post_author'] = 1;
5 V9 P0 r b7 `) `4 o: A; ] - $my_post['post_category'] = array($cate);& O2 n6 Y6 T# [% ?- a7 x* w( m
- $my_post['tags_input'] = $tags;
6 W) d& b8 T1 X7 n& s" h3 H - //$my_post['tags_input'] = array('tag1', 'tag2');
5 z; [: [2 R7 C# G - //$my_post['post_status'] = 'future';2 o& t, B9 S" u* o1 X$ ^- V( W
- ////$my_post['post_date'] = '2010-07-04 16:20:03';2 C- T6 r1 h' n' _) r
- 0 a" W% m! ?# o9 h
- // Insert the post into the database2 M$ x& {- _1 o- [, G3 p
- $ret = wp_insert_post( $my_post );
% Z& r# m1 W9 M; l! A - echo $ret;5 m+ D" d7 A# ]( j: B; k& o
- ?>% ^& A. V* M- T* H
复制代码 我自己写的/ e3 |+ Z9 K7 q) z( A
插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|