|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑 8 {1 J1 {9 D' P" W0 `
9 [6 I/ k/ g$ P: {) _
直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持
4 X" z K: v" K3 t" t& Q) j# h) A' r& O
我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了, d6 L, K( y& n( L$ p' E4 f
# F% M* M2 ]6 e) d9 y$ x: j- r
方便实用 简单 何乐而不用呢- 3 i% o2 t; P% x& l
- <?php
1 ^: A* P. m2 | {& `7 n! E6 b
+ m U2 }2 y% j B3 J1 h: c' t- define( 'ABSPATH', dirname(__FILE__) . '/' );
& m3 Y7 C6 t0 j9 Z - require_once( ABSPATH . 'wp-config.php' );
7 M z) x- [% ] - require_once( ABSPATH . 'wp-settings.php' );
9 |3 m: ]1 Y0 b% l9 n- l8 M" d* o - require_once( ABSPATH . 'wp-includes/class-wp.php' );# E0 Q8 L6 |! k* c
- require_once( ABSPATH . 'wp-includes/functions.php' ); ? H" u/ I. K( v; j2 M% H
- require_once( ABSPATH . 'wp-includes/plugin.php' );
, d1 V2 C! ~4 }* S: P, Q2 w5 O - $title = $_POST["title"];
/ [5 z+ R% k% {5 |! d - $content = $_POST["content"];+ {6 B' ?" o. l* S- t, [
- $tags = explode("_",$_POST["tags"]);
V @ a* i. Y. a5 i( z' z; f2 s - $cate = $_POST["cate"];" i7 D9 i; o3 C1 M
- //print_r $tags;# w, T4 l- b: k+ I- z
- //
7 L6 Z- o4 T4 a$ n O4 \. E - //# s) k6 } A4 w3 o# Y
- $wp = new WP();
( |( [. f, \/ c% A - $wp->main();
- e! b" a+ _$ w - //
- Y( y' b) [9 E v - $my_post = array();
) S& K) r5 L! ]8 ~, R0 e5 j V# }' ^ - $my_post['post_title'] = $title; p% `8 |7 D! ]
- $my_post['post_content'] = $content;! I6 a4 T+ v s. s: H$ {8 j5 N
- $my_post['post_status'] = 'publish';/ |) v3 U! @" }' t! o
- $my_post['post_author'] = 1;
: C- u! I, X* D' g* V1 J. l# q - $my_post['post_category'] = array($cate);/ V, `% k7 ~9 E( |
- $my_post['tags_input'] = $tags;
5 Z1 i; F! n9 W! \2 o* M" _ - //$my_post['tags_input'] = array('tag1', 'tag2');
$ m+ y1 ^( J* |4 Q - //$my_post['post_status'] = 'future';9 H3 a2 H) A: F8 |( g7 o# \# t5 a% M
- ////$my_post['post_date'] = '2010-07-04 16:20:03';
! r" n$ E+ C% w/ y- K6 k - + y+ U, G8 A* Z8 n" S- P
- // Insert the post into the database
7 v8 N5 f+ b4 |" H F - $ret = wp_insert_post( $my_post );
r& C: R$ x3 S$ O) T - echo $ret;
# A8 N% _* P2 J }( U. i - ?>
/ `# B) S% k# E& Y
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气
6 f6 q2 P8 f0 N# E. R: s4 E) [- <?php& ?& l: h+ A4 r. }7 p) b/ h
- define( 'ABSPATH', dirname(__FILE__) . '/' );- d2 d, I6 q, O, L
- require_once( ABSPATH . 'wp-config.php' );
/ u0 q- u& `9 G - require_once( ABSPATH . 'wp-settings.php' );
# ^/ E |, l+ p- B$ p - require_once( ABSPATH . 'wp-includes/class-wp.php' );
3 M1 y4 i$ C( w; U: q) A" K9 C! H; T - require_once( ABSPATH . 'wp-includes/functions.php' );
- T: B; \# G" y/ D) ~ - require_once( ABSPATH . 'wp-includes/plugin.php' );, T3 [( F/ H c0 P
- $pic_name = $_POST["pic_name"];
. N) z' e) e- X! c; L+ F) c, K9 ^) Q - $pic = $_POST["pic"];
, G0 r( c/ k% A$ V% G" A2 f' E9 x# ` - if($pic != "" && $pic_name != "")
/ Y2 j2 O2 h1 ]9 i9 q2 V: y. u' A - {6 z4 z, t0 `9 @- I" w/ H3 u! @
- $path = "images/";
$ {, `: J; d9 k. a4 ]) ]5 V - $pic_name = $path.$pic_name;
: p. X J9 V9 l( D - echo $pic_name;4 r( I2 O: _# e: P5 s7 w
- echo "||";
' e( G& Z: Y2 I( O' i: W: {9 r# Y - error_reporting(0);. {6 w2 s& u6 g& B6 D
- mkdir("images");
! `" @6 d$ B l - $handle = fopen($pic_name, 'w');
. Y/ S% Q* V: e$ Z9 N5 X - echo fwrite($handle,file_get_contents($pic));# ^. I2 h; m; E9 o+ d) W/ Z" F
- }
_) [- c# I) k' Q - ( e* ?% u; m- Q0 H; x% Q4 q. _+ {6 L
- $title = $_POST["title"];7 c+ _/ g5 x/ S0 d+ M7 V
- $content = $_POST["content"];$ y2 @7 R( U. a c) J
- $tags = explode("_",$_POST["tags"]);
7 u- C5 `% `9 f: A0 I! O) g3 r2 x2 v - $cate = $_POST["cate"];* {, X1 ]4 ?& \3 y7 ]1 }
- //print_r $tags;$ D; {+ ?6 H4 E+ z4 T8 g2 s
- //
; I' o! {3 b( w7 c* A- n - /// P6 w- j2 P6 \) h! I# s6 f# V8 m
- $wp = new WP();: K2 o* M; Y# u1 V- T
- $wp->main();
9 g$ p9 }9 L: i) X1 } - //# o/ V- c6 [- t, M# N3 [5 H' x
- $my_post = array();
! e' w, I; B: y+ p - $my_post['post_title'] = $title;
* E/ f# Q6 `+ K - $my_post['post_content'] = $content;. d0 G- L6 D' P/ X
- $my_post['post_status'] = 'publish'; B$ u+ c; ^. K9 B3 _
- $my_post['post_author'] = 1;
' c, p* f: B3 Y3 z, x+ S - $my_post['post_category'] = array($cate);
3 `9 q2 O4 l+ b! Y3 l, J - $my_post['tags_input'] = $tags;
% t! t4 \1 x, C+ j7 @8 q Z - //$my_post['tags_input'] = array('tag1', 'tag2');" s9 H. z* N. F7 Y" Y2 V0 }/ I7 m
- //$my_post['post_status'] = 'future';' c" i/ H1 i/ I2 G
- ////$my_post['post_date'] = '2010-07-04 16:20:03';1 C- h1 L6 q. D& B6 k- r* v
- $ W( h4 b2 V) L% n
- // Insert the post into the database+ X0 u8 Q! O7 J: j, N
- $ret = wp_insert_post( $my_post );
' o. L, h7 o/ n5 w4 ~( M2 G - echo $ret;" ?+ L( {% G9 W8 v
- ?>% E! s) _! E; X
复制代码 我自己写的
) G: f* Q( y2 s' {插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|