|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑
1 C4 G. O9 k& I T: d( T2 v
+ f% G0 J$ _. d3 A7 z( u9 i/ P直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持 I3 n7 A) i4 g3 ^. G' ?+ \
+ L6 q0 g D4 U0 H3 v, |$ t6 z
我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了
# {& O a3 K, X; Y
& E* h6 K/ l" N7 d& U, i! W5 ~方便实用 简单 何乐而不用呢
# d* u0 M+ g: N8 [- <?php; L7 _- s/ Y0 ~
- P5 ]3 c, \, {/ Q- Y, s: h
- define( 'ABSPATH', dirname(__FILE__) . '/' );' F6 V6 Y) j3 y3 t& Q; W% D
- require_once( ABSPATH . 'wp-config.php' );
* a7 T/ \. v- \; S4 D7 C% \ - require_once( ABSPATH . 'wp-settings.php' );
. B: V# w2 m, y }' V - require_once( ABSPATH . 'wp-includes/class-wp.php' );% i f. n1 a3 D$ g- t+ q
- require_once( ABSPATH . 'wp-includes/functions.php' );/ k* o8 @: H4 U n
- require_once( ABSPATH . 'wp-includes/plugin.php' );% r7 a& M! D& D
- $title = $_POST["title"];
1 J/ Y' \# C) g; i8 K% K; b& A4 x) h+ A- r - $content = $_POST["content"];, _; i& u7 L9 T. y
- $tags = explode("_",$_POST["tags"]);
# B9 A( L$ r8 a3 k1 Y - $cate = $_POST["cate"];
* A* a7 h. J" Z& t& V9 z - //print_r $tags;& [4 o2 K& J6 E7 f- L- M# F
- //
/ e% w: [9 o! e6 c& Z5 d - //$ [3 c" R! c, I/ f( q
- $wp = new WP();) p* G0 B6 W$ i8 ?: w3 F- l
- $wp->main();
) U) A2 Q& H) }% k" y- O - //6 O2 v W/ V+ p/ L+ ~; [& P
- $my_post = array();
% [6 w6 V# q6 H, h3 C# ` - $my_post['post_title'] = $title;
8 U. L- R* N- T! ?& T4 m/ Z - $my_post['post_content'] = $content;
1 m3 L% w4 p9 I! ^8 ]( `9 v+ T - $my_post['post_status'] = 'publish';+ r) A- o |$ ^- V$ y
- $my_post['post_author'] = 1;( D1 F5 }2 {. M% C- T9 c
- $my_post['post_category'] = array($cate);! z: c' G/ A# D& |
- $my_post['tags_input'] = $tags;' \5 D, l* b5 b6 A# I6 a0 K% c' u
- //$my_post['tags_input'] = array('tag1', 'tag2');
( x9 G7 B/ }$ C - //$my_post['post_status'] = 'future';5 W \7 o& b q* O, U8 R" C+ |
- ////$my_post['post_date'] = '2010-07-04 16:20:03';
# w0 F/ v: Y! r p/ O& S
8 s& O- P' e7 v s2 t) E' L$ P3 Z- // Insert the post into the database) D9 S* W8 O" E# F }( K" g. L- d
- $ret = wp_insert_post( $my_post );
+ `8 X" r, f2 n" ~2 U) f0 \ - echo $ret;8 W7 j2 _3 k" Y6 r) m& S% ]( F9 C
- ?>- p7 M: {# e7 r' K% d2 Z" v$ y
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气- 8 i. O3 V3 G n+ |
- <?php- A: {; [. p9 W8 a. l8 l7 z5 Q
- define( 'ABSPATH', dirname(__FILE__) . '/' );% L0 t" p4 F6 O( d/ Y$ s. \
- require_once( ABSPATH . 'wp-config.php' ); V/ e) m2 H, ]! S* U
- require_once( ABSPATH . 'wp-settings.php' );0 D) X8 X X; x0 V: q" Q9 k' l
- require_once( ABSPATH . 'wp-includes/class-wp.php' );4 d; n. I& L* I0 Q
- require_once( ABSPATH . 'wp-includes/functions.php' );
" \- P, n4 R$ r$ [. O) V% h - require_once( ABSPATH . 'wp-includes/plugin.php' );
5 V& N6 f& {2 G: r( E3 Z7 Q - $pic_name = $_POST["pic_name"];
S# b1 E( H; f3 k4 K1 ^" H, v7 _ - $pic = $_POST["pic"];2 \/ C) z( H b* E
- if($pic != "" && $pic_name != "")
- d# f0 \4 S0 x) o - {& s! d5 ~& s( A2 E& B( g
- $path = "images/";
, w9 l9 H! ], w2 y: D - $pic_name = $path.$pic_name;
" B2 C3 m: y: h+ u - echo $pic_name;
5 o: l% M2 t# P$ o* h - echo "||";
0 g& G$ a5 e3 j+ C# R/ g - error_reporting(0);
* J' P. `! _# _# s7 @/ | - mkdir("images");
% x$ t( B/ U1 m0 \/ }+ m9 h: s - $handle = fopen($pic_name, 'w');
3 x: I4 W+ Z7 Q' F7 ~) c+ r2 K - echo fwrite($handle,file_get_contents($pic));
, Y6 D4 [$ ]) n6 S9 d - }
, e5 ?7 u: F$ G$ N5 M" J. X; U
" D) `% ~" X2 S- $title = $_POST["title"];
" O$ C2 {! _3 D. Z2 T - $content = $_POST["content"];
4 d" b; I9 y/ }4 w - $tags = explode("_",$_POST["tags"]);
" \' Z9 z8 C+ p- y, C - $cate = $_POST["cate"];9 C' C0 V/ w6 { {4 R, g* d
- //print_r $tags;( B$ j& z9 \$ H1 T( p, q
- //
1 y3 [! D1 h' e1 [$ q# s - //
: ]9 i, T& Q; p2 g! V# [# b F - $wp = new WP();
/ n6 w) }2 L" b8 B - $wp->main();, |+ k) W$ q* X5 G3 e
- //7 i( ]$ C8 o1 n- _, B7 o
- $my_post = array();
* \$ P1 R+ u% u6 F) `" a6 { - $my_post['post_title'] = $title;! X1 v9 |7 Z: `
- $my_post['post_content'] = $content;
2 o: I0 {/ @6 @* I* R4 h8 u3 o1 y - $my_post['post_status'] = 'publish';" ]5 } a* Z2 d) Z4 g2 |
- $my_post['post_author'] = 1;
- ?! V A0 l3 ~; v p w - $my_post['post_category'] = array($cate);( d" L6 U% N z' r, c( K4 ]/ F5 N
- $my_post['tags_input'] = $tags;
! j% j9 k" ?1 Z2 }& k+ ^# ` m - //$my_post['tags_input'] = array('tag1', 'tag2');# B9 C) U% F) X3 a
- //$my_post['post_status'] = 'future';
3 b# }" q) F0 q! a& x- s' }2 s& m - ////$my_post['post_date'] = '2010-07-04 16:20:03';' q, t% q1 q/ B. l) _3 c3 e% T
( y+ i4 p0 _% \3 E G- // Insert the post into the database. J& g' t, N% y e
- $ret = wp_insert_post( $my_post );% \. Z9 S! Z/ V/ Y! ?8 p5 |! y
- echo $ret;
N, k# \. ~, d) U# Q6 C0 k - ?>
5 ]" t% y) }2 X
复制代码 我自己写的! x8 o3 j' F' p) e# m3 z
插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|