|
本帖最后由 finder 于 2014-12-28 15:13 编辑
4 E: w+ _5 f6 l* O. V
2 O4 `; l$ c" L, T5 P* `4 f) o直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持
' b: a3 Y4 a5 l" t6 S- y4 N y
# Y; E0 r$ b. W Y0 D" Z我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了
, a1 @, v6 T7 }% f4 Q5 e
9 o; C/ K! Q9 N, E& E# k方便实用 简单 何乐而不用呢
6 J9 V" O# S8 U( E+ a+ Y4 m- <?php
% R/ u( v5 f- w( q' j5 M - 1 C9 V0 W# y" Y8 Y/ h$ F# t
- define( 'ABSPATH', dirname(__FILE__) . '/' );- g* _3 ^$ @( k* `
- require_once( ABSPATH . 'wp-config.php' );
: s- Q. j2 _3 } G# }7 A: c - require_once( ABSPATH . 'wp-settings.php' ); k% u% E. R( y0 _4 y, o
- require_once( ABSPATH . 'wp-includes/class-wp.php' );0 s: {/ c. B8 E" W3 A3 m
- require_once( ABSPATH . 'wp-includes/functions.php' );. t# ~ d3 V' w$ Z9 Y7 x* V
- require_once( ABSPATH . 'wp-includes/plugin.php' );
9 t" V( c/ b, ^ - $title = $_POST["title"]; g5 l0 [4 q6 L9 Q+ g, P: u# J
- $content = $_POST["content"];
4 T- D1 R$ V3 F8 `* F - $tags = explode("_",$_POST["tags"]);3 ~% } ?, J# X* t( p
- $cate = $_POST["cate"];( c7 \& d% l) A/ O5 p' G6 j8 s3 {7 A
- //print_r $tags;
3 V a, D' | s! w1 @$ p) r - //$ t5 I& q6 ?; v% R4 Z8 x
- //
6 P4 [+ a( w9 M0 @1 \& ~ - $wp = new WP();: H, x) P3 E8 I; A
- $wp->main();
( o! I9 M" F( Q5 K - //) l* E) L& z6 ]4 r! l
- $my_post = array();' @0 Z, {# e+ O6 s- y( W
- $my_post['post_title'] = $title;1 J7 N! J% [0 S$ ^6 F; g' P
- $my_post['post_content'] = $content;
8 ~4 `) l, P6 A$ O" v7 T: d! Y - $my_post['post_status'] = 'publish';5 G: i! x: ^. E' F5 C( `7 L
- $my_post['post_author'] = 1;
' v$ F" N- o' L; ~- K - $my_post['post_category'] = array($cate);
& S4 r% ]* Z5 F; j% I! p7 X' J - $my_post['tags_input'] = $tags;: b. o/ n, Z! y5 x) J
- //$my_post['tags_input'] = array('tag1', 'tag2');5 G6 `" l, z% o% z+ U% d) H: G
- //$my_post['post_status'] = 'future';
' r2 u3 D2 a& c) a - ////$my_post['post_date'] = '2010-07-04 16:20:03';
c1 Q+ }" c8 ^6 r
$ _9 m8 o" L7 \* W4 S' l- // Insert the post into the database( x. S4 Q b& @5 C7 U
- $ret = wp_insert_post( $my_post );) A3 w6 e% B4 d! t, A# B0 G
- echo $ret;" d( B2 E- ? _9 I# h t! Y: D
- ?>
8 B" y7 F* M8 Q1 I/ d' I
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气
7 d7 F) N7 I7 t z, b- <?php4 \( u2 r' u5 w% k9 v* F% }
- define( 'ABSPATH', dirname(__FILE__) . '/' );5 K* x6 f) r9 C( [6 t6 U! ?& V
- require_once( ABSPATH . 'wp-config.php' );2 V' r, u) X3 h9 v
- require_once( ABSPATH . 'wp-settings.php' );% C0 D: K2 ?, A( K/ Y
- require_once( ABSPATH . 'wp-includes/class-wp.php' );
% X0 e$ L J6 v+ \, n - require_once( ABSPATH . 'wp-includes/functions.php' );" V' a2 N( M/ E; s; C- H
- require_once( ABSPATH . 'wp-includes/plugin.php' );3 d+ ^+ b& m. S4 v
- $pic_name = $_POST["pic_name"];' z2 J: Q7 o1 ^5 p8 n6 U
- $pic = $_POST["pic"];
+ H/ [! v+ {* H, W- l+ b, z - if($pic != "" && $pic_name != "")
6 D- Z/ T1 v% @$ l' @ - {- i* T' L2 i' d" P" i9 [
- $path = "images/";1 l Q/ @, i- E
- $pic_name = $path.$pic_name;
8 S; E- H3 k6 e! C6 K/ H - echo $pic_name;
# ?( X. ~, M: y8 V/ w. ?# K$ r* ^ b - echo "||";; U( P6 k( f! @, ?8 `
- error_reporting(0);
& h5 Q5 n' _( G8 ~! q2 v0 \ - mkdir("images");1 H' ~( V4 s3 M( v, T) @
- $handle = fopen($pic_name, 'w');
8 c0 x$ v6 w5 D. M- u C" P - echo fwrite($handle,file_get_contents($pic));% I( E8 @- x4 o! c' Y" r1 p! S
- }
- ` k) s# U1 J9 [5 Q* H - & y% R3 O6 v. K
- $title = $_POST["title"];0 A5 e1 @/ w- i& ^( b$ I( J7 ~
- $content = $_POST["content"];% @' j( M: |* ]
- $tags = explode("_",$_POST["tags"]);' ]5 |. u" u u0 K6 v) a3 e
- $cate = $_POST["cate"];
, r; ]7 A0 w w7 d3 \# G; h - //print_r $tags;
& U1 o: l8 @, D( Z2 ` - /// t0 S# g+ z; }: O: l
- //- A5 ^2 T; S, F& k0 G
- $wp = new WP();! Q% C: T$ P4 U( t* \
- $wp->main();5 ]+ |2 I8 T5 y% v/ i P; a
- //' T+ Z! } e- `/ ?6 s" r- L; Y
- $my_post = array();
4 v( v: \% w$ _- F; S3 V* U* p - $my_post['post_title'] = $title;; N4 X' \) P/ M! g( \' `4 m' {
- $my_post['post_content'] = $content;
1 U. Q. @4 Z! C. \% ]. T4 p - $my_post['post_status'] = 'publish';. a/ h* u* Z+ w4 w3 P: f+ x0 P
- $my_post['post_author'] = 1;
: y- y ~+ U) _2 z - $my_post['post_category'] = array($cate);
: l" T/ h* V2 q; X - $my_post['tags_input'] = $tags;
* P1 r _" W, ^& u+ W& K. ] - //$my_post['tags_input'] = array('tag1', 'tag2');
6 G0 L# @" I2 S& E% d) A - //$my_post['post_status'] = 'future';( q2 d8 X* L6 W( r1 F6 J# G
- ////$my_post['post_date'] = '2010-07-04 16:20:03';, y& f* ]( c) V" t/ ]
8 y3 G3 o8 B8 E8 y d. o+ \" I+ Z0 d- // Insert the post into the database1 U7 ^6 X+ W8 Y/ k# V$ o, \; u. _
- $ret = wp_insert_post( $my_post );
- l5 }- e, x% J! d. M9 F - echo $ret;
; n h4 j- J) R3 J6 Z - ?>* [, E$ ^, _! Q- k! g
复制代码 我自己写的
7 ]9 U+ W; ?# ?插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|