|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑
: }& v$ l6 S" W% `' [4 P" Y; w, o& X# A
直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持
" c. ]3 ]2 a2 ~% Q" p, Z0 t3 a; @
" G/ n" F% D" w z我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了
7 M1 k# R6 U' p- [3 k, _2 k" Y: ]1 \1 T4 s7 N% g3 ?3 {1 w4 y# u0 b5 h
方便实用 简单 何乐而不用呢
' \0 @4 R! P: W7 g& |: z- <?php
9 k% T6 V v# J: w8 R$ Z' c, s - " d/ U& i0 t+ \% @) P
- define( 'ABSPATH', dirname(__FILE__) . '/' );
; i2 d- G! x7 {- L0 `- b - require_once( ABSPATH . 'wp-config.php' );) X; E( g A1 f0 D2 A3 O
- require_once( ABSPATH . 'wp-settings.php' );
0 C8 Q; w6 \; l5 i$ [ R9 g - require_once( ABSPATH . 'wp-includes/class-wp.php' );
* u# U* `6 p- D4 u5 J - require_once( ABSPATH . 'wp-includes/functions.php' );" w) r: _. T# E3 ?& }
- require_once( ABSPATH . 'wp-includes/plugin.php' );
7 T" ?- g; d3 I- f+ a6 J - $title = $_POST["title"];! r n3 f6 t1 U( q- T% l2 E/ E
- $content = $_POST["content"];
3 c7 N% x: ~ a1 d - $tags = explode("_",$_POST["tags"]);6 g0 n! x O' R# }: g2 k; w
- $cate = $_POST["cate"];7 Q0 U! r: Y; u
- //print_r $tags;0 S7 |2 j* _1 R+ O; r, e" S
- //8 _5 ~8 }& o2 E$ ^, i. i" P
- //+ K( E4 [ [) E Y' p9 {! L
- $wp = new WP();
8 D' z9 u& }2 s* I$ B! ], Z$ e - $wp->main();8 a5 v% y" E ?1 e. z9 }9 c
- //! L6 y4 M ^- P7 d( R( N1 h
- $my_post = array();5 r! ^( \- m& |
- $my_post['post_title'] = $title;" H. S# U7 j( M( H
- $my_post['post_content'] = $content;
/ I# X$ e e1 g; ?' V6 z' d - $my_post['post_status'] = 'publish';# u1 x' i/ e/ a0 [( `
- $my_post['post_author'] = 1;8 \' {. s) o, U* k: I( h! O; b4 Z
- $my_post['post_category'] = array($cate);5 i5 n K. q4 c# Q
- $my_post['tags_input'] = $tags;
! E. |% ]0 q4 N' z: W; \ - //$my_post['tags_input'] = array('tag1', 'tag2');+ N& R v; P. Z- w( h
- //$my_post['post_status'] = 'future';
% |1 p# n5 T5 q6 N9 V - ////$my_post['post_date'] = '2010-07-04 16:20:03';
+ Y" \$ S, o/ o* M9 o! p
1 [- T1 P6 G" L* ?6 k) T- // Insert the post into the database/ P1 d9 x+ [1 F/ L& e! ~8 W8 R! S
- $ret = wp_insert_post( $my_post );
7 |1 |2 f: V9 A! r: f1 j - echo $ret;$ G+ z+ j$ w! {2 O* G1 x- J0 G
- ?> G5 r+ T$ r5 U/ \* X _6 o; ]% t1 K
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气
/ r/ n: L+ g d& ?! m# p- <?php( K7 ?3 }3 P! i1 X
- define( 'ABSPATH', dirname(__FILE__) . '/' );; j" e2 y7 ?! j& U# K0 Y
- require_once( ABSPATH . 'wp-config.php' );
7 e/ Z1 M3 |6 x) f5 P - require_once( ABSPATH . 'wp-settings.php' );9 y2 z9 w- }9 q) t, T( C- Y. Y
- require_once( ABSPATH . 'wp-includes/class-wp.php' );; I2 n+ t% k& s
- require_once( ABSPATH . 'wp-includes/functions.php' );; Z6 h0 S9 n+ v0 Y; ]9 [* o
- require_once( ABSPATH . 'wp-includes/plugin.php' );! d, L2 w4 B+ g0 b" T
- $pic_name = $_POST["pic_name"];
8 r( h3 g, s/ K2 J! T: D2 Z - $pic = $_POST["pic"];
1 b+ j4 D9 y7 x) g - if($pic != "" && $pic_name != "")
& }7 Z3 a/ O8 U" i8 ? - {% b" c* L0 O7 O: W) ?6 z
- $path = "images/";! K2 W3 d1 ?' J7 g- B' H% Q& {
- $pic_name = $path.$pic_name;' c3 |8 O7 }% Z9 |" x7 x; r
- echo $pic_name;
1 Q* ]( n5 x2 _. Q- C( e - echo "||";* I! m1 ]# f" r! y' e0 l" R. R
- error_reporting(0);1 Z1 Q4 l5 P2 X8 n) ?1 S8 [
- mkdir("images");: z0 q9 b: [* b0 x
- $handle = fopen($pic_name, 'w');
" W$ t" y5 N+ f9 J# Q - echo fwrite($handle,file_get_contents($pic));
1 ?0 f# f7 ^- O - }
* B1 w; J1 ?/ [* _9 ^& C8 w. y& G - 6 }7 {- ]4 f% Q: S, H
- $title = $_POST["title"];. B7 M' W) N9 j1 E
- $content = $_POST["content"];0 j+ `- k1 S2 `2 z
- $tags = explode("_",$_POST["tags"]);
! F% K+ [ w2 f - $cate = $_POST["cate"];
9 c" H- k5 J' V0 d0 J - //print_r $tags;. V; Z6 E' J, u: r0 ?; `
- //0 Y* `" }% ]' l1 P: i8 }
- //
" o% ]# u% F. u - $wp = new WP();! s6 z( {& ~. s
- $wp->main();) E- D2 C, a: i# K1 W( G
- //
; T5 A3 ?; a. o* m$ r - $my_post = array();! o& C! U _1 I+ m4 U
- $my_post['post_title'] = $title;: \$ M, A* I5 P- Q( J0 g0 v# m% I' E8 @
- $my_post['post_content'] = $content;
' t9 t8 v! |# L- \+ i, W" d, I - $my_post['post_status'] = 'publish';
( q! \9 t, \& G. F - $my_post['post_author'] = 1;
, _4 z* X3 O5 q- ^( ^ W K+ u& U2 \ - $my_post['post_category'] = array($cate);
( v% H3 O. M: j5 w - $my_post['tags_input'] = $tags;
" @$ j' w& n0 D* D3 C: w - //$my_post['tags_input'] = array('tag1', 'tag2');
) Y2 M. E0 p" W# `- Z - //$my_post['post_status'] = 'future';
7 ]2 p9 V2 G: ^1 j% J - ////$my_post['post_date'] = '2010-07-04 16:20:03';' ~( H0 {' x# ^; F
- 9 M6 W4 U1 ^7 L3 E& ]
- // Insert the post into the database
6 g$ d% j/ i4 ~ - $ret = wp_insert_post( $my_post );
7 {1 ~* ^! O, |+ [+ z7 G( E) F5 _ - echo $ret;
) c/ b5 v( H0 ?! {4 \ - ?>
/ v8 |, ?) L* @. d8 c t1 Z/ U% R4 ~% C& \
复制代码 我自己写的
3 A; ]. v u4 `% y; w( E/ R i插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|