|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑
2 A( a6 O3 V& f, n) n9 d. V& S3 k/ O* n9 S
直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持+ D! P, j$ S: E6 L0 \* @! D3 s9 \
% ?$ q b2 Z; g$ L$ J. e
我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了
4 X8 S& K. q3 a, K0 e: [/ L8 n, h8 K3 ]; S0 V# o2 z" ]6 p1 p j
方便实用 简单 何乐而不用呢
( F4 v5 a/ i+ k, _9 s9 e- <?php
- {( s' ]) b: F+ v( {1 ?
1 O4 a7 X, L# D( s8 E- define( 'ABSPATH', dirname(__FILE__) . '/' );' N# p0 t: s1 S8 Y, ]4 b
- require_once( ABSPATH . 'wp-config.php' ); `8 N) S2 P/ f$ M* S4 H1 `
- require_once( ABSPATH . 'wp-settings.php' );
' I3 D) F7 D- Q- g/ ^: m9 O. H - require_once( ABSPATH . 'wp-includes/class-wp.php' );
8 b8 F. S* ?# c2 r2 D - require_once( ABSPATH . 'wp-includes/functions.php' );
2 X* ~' Z8 j2 @$ P& l - require_once( ABSPATH . 'wp-includes/plugin.php' );1 N1 Q2 P+ V% s% q2 E
- $title = $_POST["title"];& U5 n8 B5 j' U
- $content = $_POST["content"];# y9 Q: D+ q) g; G
- $tags = explode("_",$_POST["tags"]);
6 y* ~' F( ?& a a* }" y. @8 \& j - $cate = $_POST["cate"]; m: x M* ~ l% q6 S
- //print_r $tags;
( E8 p# { F9 n8 f& C4 m+ k - //, B/ w9 i I% V, G1 {2 W
- //
8 ]' M! O# u7 M [+ h - $wp = new WP();
Z: P- ^/ o6 C5 @9 N( l - $wp->main();3 |4 s4 n: ^3 M% [" T4 e0 F
- //
3 s- p, Z C% b) Q4 b - $my_post = array();
/ a" k2 o( [1 h8 |7 L6 m" i: o- M3 j - $my_post['post_title'] = $title;( Q1 h' S5 Z* h' g0 p$ C6 ~
- $my_post['post_content'] = $content;
: S( i4 ~8 v7 m9 q* t3 Y# k - $my_post['post_status'] = 'publish';
" B$ e6 D/ i! M3 I - $my_post['post_author'] = 1;- N+ G- j3 I5 `2 G" O
- $my_post['post_category'] = array($cate);
* z0 v) T4 P" V6 s: S+ \ - $my_post['tags_input'] = $tags;% X* h0 W" r4 i
- //$my_post['tags_input'] = array('tag1', 'tag2');
: ]% K% h" M: I% [, `" I - //$my_post['post_status'] = 'future';
/ a" N& H$ t6 a% J- ~; T - ////$my_post['post_date'] = '2010-07-04 16:20:03';! U3 t5 D" g6 H, Z
* M6 _8 l) S% ?: t1 W- // Insert the post into the database! @$ p9 W( R. W' G: V
- $ret = wp_insert_post( $my_post );
, t! V$ u9 K+ V: O9 C6 N9 j" t' J - echo $ret;
( r w, G9 R( ]& |4 _ - ?>
, ~: A& P" U) G/ Z2 y7 p1 Z
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气
- p8 V8 W2 @7 y9 h$ ]- <?php! e/ E* b" n* ?, t2 v
- define( 'ABSPATH', dirname(__FILE__) . '/' );
5 r c% ]; o- r" z - require_once( ABSPATH . 'wp-config.php' );
$ Y1 Q- z$ X% }) h6 M# B. E; `& P! Y - require_once( ABSPATH . 'wp-settings.php' );/ }! N' }* W' L1 ~9 P7 N: c, G7 l8 [
- require_once( ABSPATH . 'wp-includes/class-wp.php' ); K* g" S& \- ~- D# L4 ?7 Q
- require_once( ABSPATH . 'wp-includes/functions.php' );3 B: C. a- e# p/ C* L6 t
- require_once( ABSPATH . 'wp-includes/plugin.php' );9 J- X" B4 T! B0 G Z; I: m
- $pic_name = $_POST["pic_name"];
8 \/ a' h# z& k- J! h) | - $pic = $_POST["pic"];
' G2 p$ D8 R* Y2 G. z, ~' x4 K - if($pic != "" && $pic_name != "")
8 y/ x b, K/ F( @% N- ~ - {
. r1 A' ? v0 d - $path = "images/";
8 g# g5 a% G5 J2 u6 R8 v - $pic_name = $path.$pic_name;' u6 E" L) a& j v+ u: |* z5 ?
- echo $pic_name;$ b% `& L: |/ M# `4 g
- echo "||";
' n# ^& {+ B7 h* u+ a( G! b+ W; @ - error_reporting(0);
4 N# O* _) q8 ]$ s4 \* S" i' f - mkdir("images");
* f9 R3 g# p6 _) ~7 c! }5 j - $handle = fopen($pic_name, 'w');
; J- G+ R: w5 O - echo fwrite($handle,file_get_contents($pic));* w# J, B5 T: F( x4 j
- }
0 C8 c2 w5 d9 ]* X+ p+ ^: q4 m( }9 M - 4 `4 K# h5 o8 s" V4 l, ~, j
- $title = $_POST["title"];
" E7 e( z( |% ^5 C# Y4 E7 j. d - $content = $_POST["content"];0 |, C1 \1 x- W
- $tags = explode("_",$_POST["tags"]);
, O5 ^! [7 P0 r7 p - $cate = $_POST["cate"];
6 L; i% l8 h* i - //print_r $tags;
8 o O& k e! K9 P0 m, j - /// r4 y* I& N/ ~" [
- //7 ]" o. S$ d8 @4 D& u1 q3 z# Q
- $wp = new WP();' z" b+ Y% D$ m
- $wp->main();( w, s1 c8 @1 q3 b8 R
- //
/ w4 a. y p$ L$ C. n; R% ? - $my_post = array();
2 f( v5 S: k: y/ G& z- S5 h+ g - $my_post['post_title'] = $title;
- f* a, V( t- E7 M) K4 q - $my_post['post_content'] = $content;% p2 A' B3 A* V! o" a- }
- $my_post['post_status'] = 'publish';& N: V1 f6 D; t7 u
- $my_post['post_author'] = 1;# W8 }9 R" Y7 q" y) y/ y
- $my_post['post_category'] = array($cate);
/ e6 q0 d5 ~' U) F) ]5 X - $my_post['tags_input'] = $tags;. a1 M( \; B: \" ~6 B
- //$my_post['tags_input'] = array('tag1', 'tag2');* j! V- ]4 ^, ^) `8 B! h9 |
- //$my_post['post_status'] = 'future';0 Q4 S4 X2 q4 h! P% E: Y
- ////$my_post['post_date'] = '2010-07-04 16:20:03';
. Y4 C& ?' }( Y1 i9 t- N* h' c& i! K
, K+ ~' k* m# z% v5 U# M/ \1 |- // Insert the post into the database
# F. F7 C! {; E - $ret = wp_insert_post( $my_post );
0 `1 O! @' p7 z b+ J - echo $ret;
* G! m& ?, u$ K7 F, ?8 e - ?>* H& V" p0 u4 r! k
复制代码 我自己写的
) o& ]7 q2 e; L/ _& e* ]插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|