|
本帖最后由 finder 于 2014-12-28 15:13 编辑
& M; M: `8 p! b y' s4 |4 D* p S" Z# ?4 E
直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持
+ G3 K2 U G: j6 C3 S, W+ e
/ @" s# h' _+ C- V( _4 s我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了
3 o. U, {. B1 Q* ]0 @
) W) g: B e! F方便实用 简单 何乐而不用呢
$ X- l& S7 F3 { [" |- <?php
; o' [" _6 X% |5 g7 V - 7 M s/ s y' r5 K8 S3 E' N s4 b% M
- define( 'ABSPATH', dirname(__FILE__) . '/' );: @2 r- M7 \$ H
- require_once( ABSPATH . 'wp-config.php' );
4 ^% S J0 w( A9 a1 O* @+ G. H - require_once( ABSPATH . 'wp-settings.php' );: e. M/ _. T( g, x* V- p
- require_once( ABSPATH . 'wp-includes/class-wp.php' );9 D3 I' D* x" y
- require_once( ABSPATH . 'wp-includes/functions.php' );
8 p9 m. O0 A1 a - require_once( ABSPATH . 'wp-includes/plugin.php' );
w4 L$ ~ a4 Z$ |7 \$ `, C - $title = $_POST["title"];/ p6 l' ?/ [$ f% G6 D( U8 f& C3 Z" K
- $content = $_POST["content"];' t" S8 O+ B6 g1 k( F
- $tags = explode("_",$_POST["tags"]);1 Y r+ _! U5 `
- $cate = $_POST["cate"];
* h- G' u, b* S% r6 L- d& d - //print_r $tags;& M4 B* {" q7 v" ~ ]6 ]9 H
- //
- J6 N, y3 R Y& b: ] - //
0 J8 g) e- U. [3 o8 U - $wp = new WP();3 k* t. t3 i* ^2 O
- $wp->main();% `! d% E9 L p) ?
- //
) t- b; x4 I+ K! }0 j0 J - $my_post = array();& S9 H8 f5 S9 Q5 G" f) m, I; A1 R
- $my_post['post_title'] = $title;
2 x7 a& m. [9 f! q) q - $my_post['post_content'] = $content;( f# z1 @8 d' N1 ?
- $my_post['post_status'] = 'publish';8 H5 y$ H9 s. \# t& U1 j/ l
- $my_post['post_author'] = 1;
" Z- v# i1 |, Y1 f' n7 w& I8 ^ - $my_post['post_category'] = array($cate);
0 d: c* j# n4 v- w - $my_post['tags_input'] = $tags;6 l' B2 U1 \7 J
- //$my_post['tags_input'] = array('tag1', 'tag2');
3 s& B# q: c2 x2 ? - //$my_post['post_status'] = 'future';: Y8 E- k4 Y; k) c1 I5 p
- ////$my_post['post_date'] = '2010-07-04 16:20:03';
$ x% I; T$ W. y- Z: [' A
8 C; J h7 E8 L X/ V8 q- // Insert the post into the database) h# P/ F! }4 `' u4 e
- $ret = wp_insert_post( $my_post );
' p( X2 U% J/ p& ?$ Z - echo $ret;
3 ^7 [' j/ }7 d - ?>% V. R, ]: V/ b! A6 `* \
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气
; z# Q8 T% G3 h4 w7 p6 j9 W- <?php
/ l; j3 {, u. q) V x/ ]! i- }8 U - define( 'ABSPATH', dirname(__FILE__) . '/' );
1 v6 c, t# e) j7 G. o/ } - require_once( ABSPATH . 'wp-config.php' );/ v- e4 N$ @5 Z1 B9 U
- require_once( ABSPATH . 'wp-settings.php' );
9 j. p! `8 {+ P1 ] - require_once( ABSPATH . 'wp-includes/class-wp.php' );+ p: j+ ]- h) I o
- require_once( ABSPATH . 'wp-includes/functions.php' );
# N: ^- e) N4 N - require_once( ABSPATH . 'wp-includes/plugin.php' );7 p- ~/ \0 _* E9 I. U
- $pic_name = $_POST["pic_name"];
: r6 K0 K, X! M9 b9 I - $pic = $_POST["pic"];
: X: b, t" z2 l6 U+ o$ c2 s - if($pic != "" && $pic_name != "")
; }' _. y7 l' h - {- O$ o5 n# h/ Y( U2 ]6 L8 f: N
- $path = "images/";) U& n8 N" X: z3 H4 ~7 I* a: s
- $pic_name = $path.$pic_name;7 {4 f7 f6 C9 i$ o w8 D
- echo $pic_name;2 _5 y/ T- m% ?5 v
- echo "||";
# p8 {7 S/ F& L# O - error_reporting(0);
; S* `- d1 ^4 ]% e$ `8 Y: E5 A - mkdir("images");, J, v$ s. G- A" d
- $handle = fopen($pic_name, 'w');# f C" ?3 L; Q1 M0 A# s: }
- echo fwrite($handle,file_get_contents($pic));
& |$ |. p w+ @: t - }
& @- P; A' J/ _8 d0 D - 8 a+ l4 D3 r$ U& e9 [
- $title = $_POST["title"];: ]. w; D& t% O' @
- $content = $_POST["content"];7 I* L1 P( q! I" u: E3 {8 I! d
- $tags = explode("_",$_POST["tags"]);" [% G8 z! z. Q4 F8 k7 Y1 q. G
- $cate = $_POST["cate"];1 V6 p/ z3 O4 K7 r1 ^+ G0 J- U" a3 A% r
- //print_r $tags;2 a ^4 q& p3 i+ S7 @
- //* g8 }! g' @5 J/ t$ j
- //* b7 ]. ^8 y* R+ Z
- $wp = new WP();6 n7 |! [8 a- i' ` N8 Q E% d
- $wp->main();
: h i6 W. u% v1 e+ z0 @" l S- b; s - //" W v, W/ c6 D5 ]% c/ _; R( t/ \
- $my_post = array();! C9 I) r( E3 c( Z
- $my_post['post_title'] = $title;7 l3 d$ j! b5 P3 c: f
- $my_post['post_content'] = $content;
7 ~! X+ t8 R9 _3 D8 S+ T+ d5 k - $my_post['post_status'] = 'publish';7 ` @0 Z; o0 ? n! ?8 r6 _, @
- $my_post['post_author'] = 1;
5 Y+ p+ ^) X# v: {) z! P& z - $my_post['post_category'] = array($cate);
4 ^" j$ v* a$ q8 E( ? - $my_post['tags_input'] = $tags;
4 p0 d9 f; o: g | - //$my_post['tags_input'] = array('tag1', 'tag2');' ^" e1 k2 O; |
- //$my_post['post_status'] = 'future';) V; p# \8 |) ~$ k" ]
- ////$my_post['post_date'] = '2010-07-04 16:20:03';7 P8 o9 W( ]+ Q' ~
- . L" q% d5 ^2 m5 W
- // Insert the post into the database; O. v/ t8 U+ r
- $ret = wp_insert_post( $my_post );
0 v _1 T _- R5 R4 }9 I9 W* Z1 H - echo $ret;3 f; c3 g& b$ w0 T# Y6 I! C
- ?>/ i, c# ` K S) D: \- t
复制代码 我自己写的2 @7 _: p5 O) j( k Q3 T
插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|