|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑
5 C" @* l6 p- S, U0 {
; e! ?# R$ d7 ~1 C3 }% k0 Y% B5 w直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持+ B+ A8 r) |2 H% [4 V# I
' b# a8 f% r( `' X7 H4 Y& `# l4 r
我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了
( N1 X& d: D. U! i
' ~0 {) f! v* p8 r1 t- M2 f方便实用 简单 何乐而不用呢- , W2 I6 o/ f7 k% J- k4 L1 _$ h. l
- <?php
0 C9 D+ w2 M9 L, H( O
6 I7 D$ m% A1 ?) R8 r9 K# z- define( 'ABSPATH', dirname(__FILE__) . '/' );
L" ~; M' K7 X9 S' z' ^ - require_once( ABSPATH . 'wp-config.php' );9 c3 z. A$ S4 v, v( w
- require_once( ABSPATH . 'wp-settings.php' );
# z3 X: q; o' H - require_once( ABSPATH . 'wp-includes/class-wp.php' );6 U6 T6 {# Z0 f
- require_once( ABSPATH . 'wp-includes/functions.php' );
6 g4 ?* L6 `# |& Z% }% N - require_once( ABSPATH . 'wp-includes/plugin.php' );
2 F2 e% |6 ]4 k7 W, \4 b$ U7 W - $title = $_POST["title"];
?. ^" P' u4 J% h4 Q: S0 h5 t - $content = $_POST["content"];: Z' o: P2 S7 _ p/ J$ D; _
- $tags = explode("_",$_POST["tags"]);
5 Z) S' Y. U3 A* T% W( Q/ p - $cate = $_POST["cate"];( L* M9 p7 h) G( V6 }, r% c2 f5 M; ^
- //print_r $tags;9 r7 ^ q1 Q: g) v
- //
" s" E* n4 C( V( _. i# [8 X X% [ - //
9 V8 d' S6 \1 f8 |7 b% X; j - $wp = new WP();. F2 w5 ^) `* `
- $wp->main();
5 b& b. u9 ?* W2 n6 `2 N - //
- B% o ?: R7 h. X - $my_post = array();
! I1 T" x+ w3 \ - $my_post['post_title'] = $title;' m* q/ n5 f' {9 ~: k9 r2 J6 A
- $my_post['post_content'] = $content;* {& A: W5 [0 Z) j+ Y. u+ c0 F
- $my_post['post_status'] = 'publish';1 b8 s0 `! S9 Q; y- ]- p8 G5 Z
- $my_post['post_author'] = 1;$ G1 O N( h3 O& i+ N* N0 s
- $my_post['post_category'] = array($cate);
, i3 D; K4 X# n3 _/ D) y s - $my_post['tags_input'] = $tags;
& d8 p1 V C( x; i/ K - //$my_post['tags_input'] = array('tag1', 'tag2');. n) T+ T9 q; N X
- //$my_post['post_status'] = 'future';+ m, X! c$ T7 l
- ////$my_post['post_date'] = '2010-07-04 16:20:03';
! y- i( v' Q$ [9 `% n - # H' ? F: h: f0 J* x
- // Insert the post into the database
$ I. s: C! C; D( C" f/ C - $ret = wp_insert_post( $my_post );
5 P+ T* Z: h, A4 J$ v" m: e - echo $ret;
4 p: b& n0 E' l, S/ G& ?4 z, B - ?>+ u# C2 f/ T, h
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气- : F5 M$ j& P/ z) }& N1 {
- <?php% C3 V$ w2 N7 X6 ]5 \) m' z% S4 |8 t
- define( 'ABSPATH', dirname(__FILE__) . '/' );+ _7 m$ N! b* U5 |9 a* K- E u0 E
- require_once( ABSPATH . 'wp-config.php' );, L9 Q+ w% }$ |4 W$ c; C2 f- l
- require_once( ABSPATH . 'wp-settings.php' );$ T6 v g* Y- {0 P
- require_once( ABSPATH . 'wp-includes/class-wp.php' );
/ D/ ?, y- _2 t! X$ N6 h7 _ - require_once( ABSPATH . 'wp-includes/functions.php' );
8 o! ~$ m1 t, h3 e- ~( \" | - require_once( ABSPATH . 'wp-includes/plugin.php' );- V4 W% h8 R( H$ e; J, |5 |5 G
- $pic_name = $_POST["pic_name"];
4 L ^9 d5 o4 [3 I! ^, K, b - $pic = $_POST["pic"];
5 V0 q3 n2 _1 L - if($pic != "" && $pic_name != "")
* s: K" y0 k4 D$ Z4 H7 n - {
: e( K W/ |6 T - $path = "images/";. p; M& r A) g5 G7 a
- $pic_name = $path.$pic_name;
n+ O+ l+ d3 S: `4 C/ u$ c - echo $pic_name;
3 k- j: `0 R3 M" r5 ` - echo "||";' J, M5 {6 g$ p7 i, M7 I
- error_reporting(0);; H# O2 @0 u- ?. O, W+ |
- mkdir("images");: Z' a; U& C% B) r0 T
- $handle = fopen($pic_name, 'w');
a7 }9 P/ B: {- V- q/ N# W8 T - echo fwrite($handle,file_get_contents($pic));& e+ W j. S& g' l; O) a/ q1 |" J
- }
# Z0 B+ o- C7 s) G3 Q
6 E) \: v" m2 K6 Q- $title = $_POST["title"];/ d9 ^2 ?& Z, D( h7 y* d
- $content = $_POST["content"];# Q# ?/ s- G% ~2 k2 [- @4 z
- $tags = explode("_",$_POST["tags"]);4 G2 z: I' H) L
- $cate = $_POST["cate"];: Y% k, p9 d% p+ G
- //print_r $tags;
7 }/ I) I; K9 K0 g7 ~. t - //) _* S; O% x$ A5 l5 Y4 Z3 A$ }
- //( o; O( I( \0 ^' V' F
- $wp = new WP();. l: x1 v7 ^( r+ }& j I' ?
- $wp->main();5 [9 [6 T c# r9 u
- //
' p {, u6 F2 k/ }- [5 H# x - $my_post = array();% s4 @# n2 O( P7 f. k; n
- $my_post['post_title'] = $title;
- X! [6 g$ _% q" C4 M - $my_post['post_content'] = $content;
% K2 r1 r J7 A8 b, w& Y - $my_post['post_status'] = 'publish';* T- ^: k( G& U% W/ t+ E. o. q, r* ]
- $my_post['post_author'] = 1;
( e8 U; g3 M2 w+ w - $my_post['post_category'] = array($cate);: H q$ R: n5 C' i" F
- $my_post['tags_input'] = $tags;; H7 h4 E0 i2 _; W" X# P
- //$my_post['tags_input'] = array('tag1', 'tag2');
3 f, [5 W$ V) u2 U4 @ - //$my_post['post_status'] = 'future';
) D6 y6 Z" ]: b3 F# L! e1 ` - ////$my_post['post_date'] = '2010-07-04 16:20:03';
8 m5 E/ V2 ?5 C. T& A: \+ R' j - 4 ~8 V* s2 F* ^7 S' D
- // Insert the post into the database$ }2 }9 G- O. o) v, [: H. q
- $ret = wp_insert_post( $my_post );
# T, P2 {$ G$ j. R8 n - echo $ret;
/ c) N+ T' Z9 u9 e' q - ?>/ [8 G! b+ j9 Y4 d& b
复制代码 我自己写的4 @; D% n& W7 a' Q! `) d1 k* V
插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|