|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑
* L; L+ i* X5 ?+ c: J0 I- J* o% @7 v
) X4 }, R: s" A# u4 k T直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持& k/ i4 u% X- }" \: @
0 H# q' F; s. M8 E7 N我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了
* `9 a- E. M4 v3 {4 [" K* ?, I. t! b! n Z! p
方便实用 简单 何乐而不用呢
% l6 p# s* ~& [% @- <?php
! R+ P8 ?9 |& @- c0 x7 R" a
* |# `+ d# G5 W+ r- define( 'ABSPATH', dirname(__FILE__) . '/' );
0 `! q* i, h5 _% J - require_once( ABSPATH . 'wp-config.php' );' _! J" o# i; R% P* D2 I# P8 K
- require_once( ABSPATH . 'wp-settings.php' );
0 @. @0 y: a. T0 U* X& p) T - require_once( ABSPATH . 'wp-includes/class-wp.php' );6 |( F* n, o9 E# I2 f9 v6 f
- require_once( ABSPATH . 'wp-includes/functions.php' );
+ z5 P0 _+ `6 t( a% d& K: x1 X - require_once( ABSPATH . 'wp-includes/plugin.php' );7 ?" o, A8 j3 ^* z, z9 D
- $title = $_POST["title"];
, E5 T0 B2 x" Q$ y/ O& q( J w3 e - $content = $_POST["content"];; {1 Y* |4 S( u
- $tags = explode("_",$_POST["tags"]);
$ u) M e1 |5 m3 c( p7 G8 X - $cate = $_POST["cate"];- S: L, v; A5 e3 O$ ]1 V2 a
- //print_r $tags;$ T! B( m& a, l( {+ i* ~
- //# c9 T" Z$ \1 D( L/ Y( t
- //
! A% [7 u+ c) B - $wp = new WP();
% y+ U' j; g9 F$ ~0 l# x o - $wp->main();( [4 }% y2 f* G) ^% g# g4 ]0 p
- //
' h3 f- z, i) F) ?2 w' M - $my_post = array();4 x. u+ V4 s. B4 r
- $my_post['post_title'] = $title;
9 C0 s6 Q4 @$ K. {$ f& K - $my_post['post_content'] = $content;
# n- e2 d* C, l: b; D1 I7 z - $my_post['post_status'] = 'publish';8 q( \2 a9 w( |- n/ J, u4 i
- $my_post['post_author'] = 1;
- j7 y( Q9 E0 b- D( P( B - $my_post['post_category'] = array($cate);
* l+ M2 J+ D9 C) U4 k3 h% `; a, L& M - $my_post['tags_input'] = $tags;
% }; j( c9 Y n! s - //$my_post['tags_input'] = array('tag1', 'tag2');' U E; v6 q- t+ y0 H* C( h
- //$my_post['post_status'] = 'future';5 M4 i) s# g4 D- x
- ////$my_post['post_date'] = '2010-07-04 16:20:03';
& Q9 W4 x7 n- `9 k/ w) L. j
- c* m. Z- s1 V0 G6 h7 t6 }- // Insert the post into the database
! f$ {! i' ?! Q9 C - $ret = wp_insert_post( $my_post );2 A* c( S5 F0 x* k0 J
- echo $ret;+ d! l8 ~8 X5 |, d0 l0 i. M& u
- ?>
& p, e* V; Y X2 F4 u2 k n& Q
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气
! N; ~6 a( w( G; o) V% v# `* c, O, ~- <?php
8 T8 {1 S2 P( P5 t' x4 v - define( 'ABSPATH', dirname(__FILE__) . '/' );
3 ~. E7 s5 S1 v f! z9 A, S - require_once( ABSPATH . 'wp-config.php' );! b; a% I; y6 }4 F0 n
- require_once( ABSPATH . 'wp-settings.php' );" ~0 F: H* q# n5 m% V
- require_once( ABSPATH . 'wp-includes/class-wp.php' );( t" z8 x$ z& x' U
- require_once( ABSPATH . 'wp-includes/functions.php' ); j. G9 u2 h, a0 J) ?% f( c* b% K
- require_once( ABSPATH . 'wp-includes/plugin.php' );
, s7 g- \( F# D. z; C% p - $pic_name = $_POST["pic_name"];
2 W, u1 v( | e, V! I0 q - $pic = $_POST["pic"];
) j; ]4 q7 A, z! ]8 w# b: y - if($pic != "" && $pic_name != "")0 Z/ v- n* a5 Y$ S0 g/ V! Q
- {
& r" J! i9 |$ p2 B8 Z( t2 [6 r - $path = "images/";, w( A8 [5 O1 L5 ^: }# [- N: ?
- $pic_name = $path.$pic_name;1 @8 `" c# x! B
- echo $pic_name;9 N: ]5 v' J. E: d* o$ j
- echo "||"; j# t0 I+ z* m8 W. U
- error_reporting(0);( z$ v) o/ o9 M' S& N1 ]- ?1 B& }
- mkdir("images");0 [# J3 C" ?0 @$ S$ ^4 F
- $handle = fopen($pic_name, 'w');& v, k/ N" d4 A: W
- echo fwrite($handle,file_get_contents($pic));
8 |- F# P l2 c$ }6 h: } - }% o4 V8 I. W* ^/ Z* k
- : r/ r$ L0 F' R, S% d
- $title = $_POST["title"];9 A6 P2 w, y9 T
- $content = $_POST["content"];
: T5 J7 @# S. v7 O5 _$ O - $tags = explode("_",$_POST["tags"]);
( K5 c8 c( s, x: s. A3 c - $cate = $_POST["cate"];+ J& d6 H3 [7 r+ D, o& G5 l3 {
- //print_r $tags;
( V5 F. e& k6 u) ? - //+ z' q6 j( e* ~' \5 ]+ W5 |/ U' e
- //
7 c) w3 Z h/ c R9 U$ D - $wp = new WP(); I% S6 k( e/ P. R/ O
- $wp->main();
2 R8 i2 _/ k2 b+ J2 n - //
9 g3 S6 [5 P+ ^% F U' P - $my_post = array();( W" N9 w+ W6 ~( f' N8 l# O7 B
- $my_post['post_title'] = $title;
& O( o1 F8 {7 t% o; Y8 j) L - $my_post['post_content'] = $content;- Z' L1 t1 r, n( G! M9 s
- $my_post['post_status'] = 'publish';
7 F" ]+ Y; c/ u: _) [8 J, g' T" z$ m - $my_post['post_author'] = 1;. ^- G( z$ c2 {1 {
- $my_post['post_category'] = array($cate);; I$ l- R0 c; _1 ~ I! Q7 n
- $my_post['tags_input'] = $tags;6 a4 h5 k% }& P6 E
- //$my_post['tags_input'] = array('tag1', 'tag2');4 T3 y4 _+ p! C
- //$my_post['post_status'] = 'future';
7 U L' ?; @' G# p; K1 M( I# n# N - ////$my_post['post_date'] = '2010-07-04 16:20:03';6 o- V- I; R4 d0 M8 e( Y% M# q
- 2 r( E6 {9 ^1 s8 E1 ^
- // Insert the post into the database4 D1 [- X: N S8 {+ V: B" I
- $ret = wp_insert_post( $my_post );: k/ V" {$ b3 s4 u0 R
- echo $ret;
3 q) ?6 {) T2 _- G. i7 V1 Z. J3 A% b - ?>2 |6 x2 u7 g+ p2 ]& d
复制代码 我自己写的
. S+ V# ?% t5 `: P2 w. W! {插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|