|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑
9 m1 W! \$ W' `' v
$ n/ W9 W+ ]7 S# L! i1 f3 H. O8 [直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持) n2 m9 |# i# y* c
- u9 p: V7 R! O5 t* v
我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了8 f3 J3 w) Q5 }2 W* ?0 O& y) k0 w* E
- X$ ^4 g' g# C( a9 N q7 O方便实用 简单 何乐而不用呢
% S- G, Q2 t" |/ P: h. A- <?php: l9 ^; Q7 V U Q# @. o% Z
- 4 o6 _+ C0 ?) L0 ^1 g7 A4 U
- define( 'ABSPATH', dirname(__FILE__) . '/' );
! x2 b5 e) [7 l! u9 \- y - require_once( ABSPATH . 'wp-config.php' );6 I9 A8 q6 {* H" i& z
- require_once( ABSPATH . 'wp-settings.php' );
2 x0 U) I. \* k9 ~ - require_once( ABSPATH . 'wp-includes/class-wp.php' );
y5 q. d& K6 ^( {) M5 `/ y1 S - require_once( ABSPATH . 'wp-includes/functions.php' );( z7 [) M* v4 t2 t
- require_once( ABSPATH . 'wp-includes/plugin.php' );1 M G& d! {' |; ]9 R6 U5 o) t
- $title = $_POST["title"];2 v6 v1 z& ^5 `/ r8 m; U I
- $content = $_POST["content"];& v- @ K7 b. Q/ W% f6 C. u- o* b
- $tags = explode("_",$_POST["tags"]);, E5 o; l; q$ I' q6 _5 K
- $cate = $_POST["cate"];; }0 D: ~- J9 y% E( Q
- //print_r $tags;
* _4 O( K9 z2 x7 {& v5 z - //8 A; T+ ^7 ?# U: c( _
- //0 G: x# u' J4 A! \. i/ B
- $wp = new WP();
' c) h! C- q6 ?$ l, ^ - $wp->main();
1 `" e9 | A6 m6 o - //- t2 m* o# {0 N" C4 `$ d: w: ^6 Q
- $my_post = array();5 k% U. D/ S u( v8 W. b. u
- $my_post['post_title'] = $title;2 l! _3 g; t8 E0 [* d- F) @
- $my_post['post_content'] = $content;
9 \6 K ?- O! U% i: z7 h# i - $my_post['post_status'] = 'publish';
% Q$ C: j! Y7 d4 B3 U4 |* T - $my_post['post_author'] = 1;
: P) T" h8 v, y2 l! W - $my_post['post_category'] = array($cate);
3 T9 G1 S5 V0 i$ Z; S - $my_post['tags_input'] = $tags;
1 E3 b% I+ W. c* d p - //$my_post['tags_input'] = array('tag1', 'tag2');% `; \; p; [7 z
- //$my_post['post_status'] = 'future';( j2 V, U* z/ E
- ////$my_post['post_date'] = '2010-07-04 16:20:03';0 C6 g2 [6 C. q. L5 E. H5 k+ x& }
6 ]! T1 Y2 D2 l7 \( P4 B- // Insert the post into the database$ u9 }3 e3 D3 M" G6 w7 Q, N$ f
- $ret = wp_insert_post( $my_post );
7 H z# x: b7 w8 @( H - echo $ret;
4 C$ M7 R* m5 L0 N& q - ?>" X# v+ d& X5 z5 N" Z
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气
# [$ C! o0 r" Z3 G' L- <?php
) {/ I% |- }& C& c4 d2 e - define( 'ABSPATH', dirname(__FILE__) . '/' ); C+ I5 O7 ~, b" j9 D
- require_once( ABSPATH . 'wp-config.php' );2 m. Y' I- X; F0 Z" ?; R" b
- require_once( ABSPATH . 'wp-settings.php' );
- e7 S" e. x$ |/ k# C# T! C B - require_once( ABSPATH . 'wp-includes/class-wp.php' );
7 g: Q; b3 |: \- f- X4 g - require_once( ABSPATH . 'wp-includes/functions.php' );
4 u6 z( Z2 x3 T: @1 I- q$ J9 A - require_once( ABSPATH . 'wp-includes/plugin.php' );
; `, p; {# l9 B+ l k$ } - $pic_name = $_POST["pic_name"];
4 B% l1 M' p% F9 B3 N' d - $pic = $_POST["pic"];/ L1 F A0 A4 ]% A" v0 K
- if($pic != "" && $pic_name != "")
8 R5 A& y- ]* r' F - {# D, N6 K ~8 V
- $path = "images/";- s, j6 ~! p. j6 V
- $pic_name = $path.$pic_name;( a+ |: O& v# u. s s
- echo $pic_name;6 O6 ?" }0 z) ?$ x
- echo "||";5 c6 G" {/ [; w/ ^6 N
- error_reporting(0);
2 N& b; V+ \1 R2 V; | - mkdir("images");! E+ E" O$ d( f: R; p
- $handle = fopen($pic_name, 'w');: Q. x$ U( ?) ]/ l' D7 ?. D
- echo fwrite($handle,file_get_contents($pic));
8 h! {6 e/ H$ {7 N - }
( @4 H( @; o$ s8 n
D) | n' i4 G9 @3 Y& S- $title = $_POST["title"];
$ P- M. c# q2 Z' \3 {* H' f - $content = $_POST["content"];! Q+ u! ^& P6 g6 S8 ~) T
- $tags = explode("_",$_POST["tags"]);8 H+ w- T0 y1 S7 j( r; P
- $cate = $_POST["cate"];
9 d% p0 t0 Q5 ? - //print_r $tags;9 F; z' o, f5 I* [+ {9 F
- //* m& `6 j$ G3 j
- //
1 v. k: i+ B& p+ ~- r - $wp = new WP();, l' s, P' @; ~
- $wp->main();
1 @- M3 [4 X3 E3 o - //
# W; x# C7 B! m - $my_post = array();
/ ^/ \/ t* w, [# g; |! ]& I- B - $my_post['post_title'] = $title;
8 N, K, x4 I! J7 f8 m& T2 t - $my_post['post_content'] = $content;
. {0 i8 A3 B+ x0 T: T: e+ ? - $my_post['post_status'] = 'publish';+ a1 N* U; y1 [
- $my_post['post_author'] = 1;
$ A2 _9 [6 G/ d - $my_post['post_category'] = array($cate);7 p9 Z8 c3 Q% b: y
- $my_post['tags_input'] = $tags;
% j- S# y7 l! a# W+ J9 \ - //$my_post['tags_input'] = array('tag1', 'tag2');0 h) s P5 W. n
- //$my_post['post_status'] = 'future';
( H) h. u. {7 A - ////$my_post['post_date'] = '2010-07-04 16:20:03';2 f$ I! T$ _3 b3 v( ^1 T
; @* E6 h+ Y6 G4 _; a- // Insert the post into the database
0 R/ _9 e0 q1 ^0 h( b- F - $ret = wp_insert_post( $my_post );, Y" Q* n1 R6 h( m8 E8 t
- echo $ret;
, ]$ u; k. Z, f0 _ - ?>
+ g% V# y( x- I* b1 e: ~9 E
复制代码 我自己写的
3 j) s$ U+ _: i插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|