|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑 + O: [. [. t4 u! U: s( r( ~3 n
" m+ ]2 S! {( z- g; C5 }
直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持0 P, [5 t, m! h1 I3 L7 c
) G: A' c& t+ b
我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了# e- p" N8 C0 p3 R( j
% q: ~0 k) n/ {8 Y9 z( c( c/ z方便实用 简单 何乐而不用呢- + |- ^, q$ L- T
- <?php
2 m9 K; b4 z$ C; R
+ f' n3 q9 L \) @- define( 'ABSPATH', dirname(__FILE__) . '/' );# q6 k1 p- S; x( |
- require_once( ABSPATH . 'wp-config.php' );
* b: ^. Z0 ?: G s& w! M5 l - require_once( ABSPATH . 'wp-settings.php' );2 C5 b( P1 `) ?
- require_once( ABSPATH . 'wp-includes/class-wp.php' );% ?- K% `1 s* o8 f& t
- require_once( ABSPATH . 'wp-includes/functions.php' );
6 Y" D2 r* I; G i4 K - require_once( ABSPATH . 'wp-includes/plugin.php' );) c6 t, U1 X$ U8 p9 y7 ^
- $title = $_POST["title"];
# f& O( o! j8 R/ G' g+ x - $content = $_POST["content"];1 S% j+ D# U2 J, S5 D# @8 \
- $tags = explode("_",$_POST["tags"]);
) Q, W- E/ B* h; y" i6 {6 T3 e6 Q - $cate = $_POST["cate"];
+ x3 @. r3 y" S g6 O6 ^ - //print_r $tags;
: Y5 a, F5 E7 y, P; M3 ~7 | - //8 C m- t/ ?# J- B% g
- //
6 ~" W+ G! B" E. M1 ~" i6 Y9 S! Q - $wp = new WP();+ I7 S. u* q' W3 j
- $wp->main();3 e" q: }/ A6 G3 R/ ?
- //
" A# ]8 h+ }( z" D$ n7 @' W5 P. Q* V - $my_post = array();
1 t) L4 g+ m5 N9 x - $my_post['post_title'] = $title;4 [0 W4 }6 y5 t5 P! ~0 p* l" i
- $my_post['post_content'] = $content;& \1 x7 R7 Y+ F1 H) B9 n7 v, e/ |
- $my_post['post_status'] = 'publish';
. Q# t8 y: u( X M/ y1 K' b - $my_post['post_author'] = 1;
# s0 e {' t0 t6 q# l4 P: s - $my_post['post_category'] = array($cate);# r7 I. z4 X- M9 W& _2 H
- $my_post['tags_input'] = $tags;
4 ~; B; m8 r! i - //$my_post['tags_input'] = array('tag1', 'tag2');2 c5 {5 d6 d9 ~7 x9 W9 o
- //$my_post['post_status'] = 'future';2 e; K+ V0 p8 q4 B+ U. m9 Y
- ////$my_post['post_date'] = '2010-07-04 16:20:03';/ r F4 X6 N: Q6 r' B3 ^
- ) Z. Z$ ]9 Z' _0 L! E4 F
- // Insert the post into the database. g' ]8 G: S- V
- $ret = wp_insert_post( $my_post );, v5 X+ X: @1 \, [) E
- echo $ret;
- y( r/ b5 k( T" G0 C0 o7 r1 s# \ - ?>
4 V5 j- ^! E1 F' X O% P1 v
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气
& X; G6 ^ T6 `7 }8 {- <?php9 c3 S% J# Z# x0 X M$ g' ?
- define( 'ABSPATH', dirname(__FILE__) . '/' );
4 p g% [2 T$ c4 {/ S& e - require_once( ABSPATH . 'wp-config.php' );- y6 g% D: `0 D' z% h' s6 M
- require_once( ABSPATH . 'wp-settings.php' );
2 { \$ t2 z' ~3 O# G- Y& [* F' O - require_once( ABSPATH . 'wp-includes/class-wp.php' );/ b! E- l1 g& x2 t% A5 }9 U* N# Z
- require_once( ABSPATH . 'wp-includes/functions.php' );. t2 U. z2 a ~2 s5 X: S
- require_once( ABSPATH . 'wp-includes/plugin.php' );6 d, d6 R+ g. h( F0 S
- $pic_name = $_POST["pic_name"];( A" U# K* C; N! M- B1 V5 j
- $pic = $_POST["pic"];
; M+ K# r5 Q" |7 ^ \) ^2 [ - if($pic != "" && $pic_name != "")/ ^) i) n$ m0 R
- {
@. T" I9 c8 C/ o9 E W6 L, [ - $path = "images/";5 ?1 n( t) B9 G1 u) K; H4 E
- $pic_name = $path.$pic_name;! C7 L5 U3 A; G* l& f. v
- echo $pic_name;
M; {$ Q/ Y. o, P8 u6 l - echo "||";1 f$ p$ f) m, N1 F. k5 |4 ~
- error_reporting(0);
! {( D6 h0 H/ n7 y6 @: Q8 Q# E - mkdir("images");7 W, E% F' }+ q$ C
- $handle = fopen($pic_name, 'w');
2 {' }( r! F0 ?) z - echo fwrite($handle,file_get_contents($pic));
4 G+ S1 @) o8 e* n9 x y - }
1 M: H! L2 @/ J! t( c+ v5 ?' |: k& d
$ _. x' {' ?( [% ?2 f. P. b- $title = $_POST["title"];5 g" Y) n( e) ~' K: |. M4 Y
- $content = $_POST["content"];
: @9 K8 P0 q3 ?6 i0 M9 t* o - $tags = explode("_",$_POST["tags"]);2 f) W: X( y% b" D M- \/ J8 e, U
- $cate = $_POST["cate"];0 Y2 G/ {5 h8 W
- //print_r $tags;
+ z3 o3 p+ p4 q8 V1 y' i/ R6 q: U - //
% J& r1 d1 G- J( x - //9 |) S$ t% v8 N9 q: _. g& S
- $wp = new WP();
# g" R/ e# L& m; R - $wp->main();& [8 M& A& Q$ w
- //; s+ \5 Y0 _- e( _+ b* `/ a3 k
- $my_post = array();8 Q4 T$ u$ X4 E# _5 h# d1 Z* C
- $my_post['post_title'] = $title;
: H+ A8 O9 u$ J5 V: o% Q - $my_post['post_content'] = $content;
6 r) ^# W6 o' c: ?: {5 c - $my_post['post_status'] = 'publish';1 x9 w, Z( }3 S3 X$ P
- $my_post['post_author'] = 1;8 w4 i/ @( _9 K! \* w) h4 |& t. u
- $my_post['post_category'] = array($cate);1 R' g" p! C# Y1 d7 |$ n$ @
- $my_post['tags_input'] = $tags;* f0 r3 A5 q1 b( P) Z: e" i
- //$my_post['tags_input'] = array('tag1', 'tag2');5 o- w# a6 ], ~- P& R4 q. j
- //$my_post['post_status'] = 'future';0 s3 J6 i6 o F9 a
- ////$my_post['post_date'] = '2010-07-04 16:20:03';
& J& S. R! \" P' I( [1 l
+ |1 C* H. l7 |/ L/ {2 q- // Insert the post into the database
4 i; |7 d) |* _' d: B+ s7 O; E - $ret = wp_insert_post( $my_post );, e& U3 d$ P7 n5 O/ ^0 t( b% Z
- echo $ret;
& K: E% t4 q! a0 N! X - ?>5 h; v5 w& a I8 _ i) c+ h
复制代码 我自己写的
$ ~' w* o3 i. r/ H) X插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|