|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑
: a; L/ K9 v4 Y4 X
/ j- ]7 ^+ G9 \; j直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持
9 d# p* C: V* k D
( G4 }! Y+ f4 O) D我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了
' _, B/ b( o, x1 Y) m* H( l% G. t9 U m/ |" h) M0 Z, W+ B, O8 Q
方便实用 简单 何乐而不用呢
" O L0 M( Z L6 k- a. p- P) P: Z- <?php9 K- n$ s9 V! h: x3 q4 }+ t
- & w6 j7 s2 C& s# O8 R& m/ {- F
- define( 'ABSPATH', dirname(__FILE__) . '/' );
% A$ H' Z p* c' A4 }& X& H - require_once( ABSPATH . 'wp-config.php' );/ b& {3 p V1 I* j+ K
- require_once( ABSPATH . 'wp-settings.php' );3 m7 ~/ C& ^4 g& k2 E7 T
- require_once( ABSPATH . 'wp-includes/class-wp.php' );
% Z& g# _" k9 a - require_once( ABSPATH . 'wp-includes/functions.php' );
1 F3 M6 W9 c! {" e4 T9 Q - require_once( ABSPATH . 'wp-includes/plugin.php' );
: M4 ]) F* \9 s8 T - $title = $_POST["title"];, j) d7 K: ` p$ Q. F. ?8 Q
- $content = $_POST["content"];
h: Y+ |0 j6 `# m7 t7 z$ k/ Y* ~' N - $tags = explode("_",$_POST["tags"]);
6 z3 ]) y3 ~& i0 ~. a - $cate = $_POST["cate"];
. t1 g4 {: X4 U e* q7 v. S - //print_r $tags;& E& G; F- H% J
- //* ?0 |6 z* b1 ^2 `& F* P) J
- //( E; _( T X) n' `0 {: x
- $wp = new WP();5 J& k4 B( c1 U' b
- $wp->main();- |! x; n* D& P; }1 ^4 b
- //5 R+ H" C9 @2 s9 ] F
- $my_post = array();/ p, C( I9 d: C& j3 v
- $my_post['post_title'] = $title; [0 w' F3 \ ]# c
- $my_post['post_content'] = $content;9 ?0 E ]( t3 t$ s( L, `
- $my_post['post_status'] = 'publish';
I1 p: N3 _' l( I3 S3 S - $my_post['post_author'] = 1;( f1 m% f* f, m( R# P, u
- $my_post['post_category'] = array($cate);
+ O0 ]6 k- g: k+ o' z( M3 @% m6 T: H; L - $my_post['tags_input'] = $tags;3 z7 e2 M0 ~6 e) B- U- L
- //$my_post['tags_input'] = array('tag1', 'tag2');, V A8 C$ ^" t% h6 g
- //$my_post['post_status'] = 'future';
$ B5 W; h; e" C# ` - ////$my_post['post_date'] = '2010-07-04 16:20:03';
( h: J0 s2 @) {1 [3 s - 9 ^$ o8 t3 k% }. x( y4 S$ S
- // Insert the post into the database( n! s) ~6 h9 i
- $ret = wp_insert_post( $my_post );
6 P# a( ^) A& q; c# n3 Z - echo $ret;% {! F( ^- ?1 {2 Y. A
- ?>; F+ v5 W6 ]; z7 p# p! l
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气- , `; c% B) J, H
- <?php
: Z& M7 M J3 }9 E- D5 G - define( 'ABSPATH', dirname(__FILE__) . '/' );3 E/ n' ~; T: V4 `' g* |7 [9 x
- require_once( ABSPATH . 'wp-config.php' );
; U$ {/ ~: ^2 S4 `5 I - require_once( ABSPATH . 'wp-settings.php' );
}$ ~ C j6 s: u9 W, O - require_once( ABSPATH . 'wp-includes/class-wp.php' );" x! m$ P5 p7 j1 j, I+ s1 Q
- require_once( ABSPATH . 'wp-includes/functions.php' );
# k9 e G' S* o7 ~! J - require_once( ABSPATH . 'wp-includes/plugin.php' );
6 @. A, t7 [# I& }7 m4 _$ K - $pic_name = $_POST["pic_name"];4 b% s6 V, a$ j* J4 S m
- $pic = $_POST["pic"];
0 T8 A1 l1 N# ?5 _7 M - if($pic != "" && $pic_name != "")" p8 M2 H- o& p& w' B r* ?
- {/ C l) _) l6 A" [1 [; r
- $path = "images/";( a F+ B' n' d/ i. v& T& t
- $pic_name = $path.$pic_name;
; S X) Z. }% l" |+ k - echo $pic_name;
0 ~5 q( A( L9 h2 ?3 j% ] - echo "||"; l( Z" J+ M- g0 V/ Y
- error_reporting(0);
# l. g* W+ ^( f1 i* s" D - mkdir("images");9 w2 O% H3 t& j Z3 A) Q
- $handle = fopen($pic_name, 'w');6 S" |1 [7 |( ]6 O! L
- echo fwrite($handle,file_get_contents($pic));6 L1 x' ~+ H/ O% q
- }
+ o$ l& l y! I
$ E! X: W2 @& ~$ R( C- $title = $_POST["title"];' {0 l* m Q; Q* r2 [! @" O" P
- $content = $_POST["content"];
) c# ?' Q+ Y( z! s5 L1 i - $tags = explode("_",$_POST["tags"]);
$ I3 a0 |" m R4 c3 Y1 X - $cate = $_POST["cate"];
. U( K9 Z% G% k - //print_r $tags;
9 |" F6 `; _7 T) ]8 T3 a - //5 l. g4 V0 ]* {: y/ {) ]) Z( G! z+ P( ^
- //
( J. z, b& ]' {, a V2 z - $wp = new WP();
1 _$ C: I! Q' `/ Z9 M. a ^ - $wp->main();
, q% W, ~' V+ G5 k' B3 B8 X6 U - //
" i# o" d3 X* T: i, l0 c - $my_post = array();, e6 E) U0 I0 L% L# H% _+ F
- $my_post['post_title'] = $title;2 Y: _% g; E! _( r! U- C( p4 R. j, m
- $my_post['post_content'] = $content;
. P1 _+ B3 \8 u" m4 u0 k# ~ m' L - $my_post['post_status'] = 'publish';6 t4 s4 G; z9 x3 w
- $my_post['post_author'] = 1;
7 R4 K0 w4 ?% j& U - $my_post['post_category'] = array($cate);; V/ M/ ]6 }! p, w# S
- $my_post['tags_input'] = $tags;9 i5 a+ F0 {0 [! b9 p, y9 T
- //$my_post['tags_input'] = array('tag1', 'tag2');6 r& d* s8 t* L# \
- //$my_post['post_status'] = 'future';
- ~6 {9 e+ h1 f, I# u/ K - ////$my_post['post_date'] = '2010-07-04 16:20:03';
0 _( u# H2 O7 x4 p- {5 F3 ?
1 u$ k' R: S6 ^- // Insert the post into the database
8 l+ m0 {6 w% w - $ret = wp_insert_post( $my_post );
1 `& _) c3 L! z - echo $ret;
% {9 c* t7 @9 t( e6 V4 w5 F - ?>
C, z% N7 [; A' Q, d# Y; d
复制代码 我自己写的' g( l' N+ q8 Z- l6 t9 C
插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|