|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑
/ h" {; J3 J$ B7 V7 W% x) P5 h3 D/ Q6 q* D9 o3 Z8 K, I
直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持, y1 c: d3 V1 ?5 Q5 z/ o, {
' _" B! t/ P" J+ N2 l5 H
我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了
: U$ m s# t6 G$ `) h2 x+ [# G" G) D/ x# e
方便实用 简单 何乐而不用呢- $ D) A1 V' Q0 J1 p% D
- <?php! [: Q5 f( v8 u% T* h( J- K1 M& v
; l% \. T/ S/ o9 n% b6 b- define( 'ABSPATH', dirname(__FILE__) . '/' );. R. l* s9 N2 L* m* d+ }. ]
- require_once( ABSPATH . 'wp-config.php' );/ A6 M8 t9 J Q' W/ Z
- require_once( ABSPATH . 'wp-settings.php' );4 |7 \" ~6 R* |/ | e+ |
- require_once( ABSPATH . 'wp-includes/class-wp.php' );7 g7 p! u7 S& p0 y0 x
- require_once( ABSPATH . 'wp-includes/functions.php' );: t. M) i+ a# b7 h* g
- require_once( ABSPATH . 'wp-includes/plugin.php' );& b: J0 j/ F8 M4 [3 i$ Q
- $title = $_POST["title"];
: C* O5 Q2 p% f3 {' A0 w - $content = $_POST["content"];
# V! z5 I" ~$ E) v - $tags = explode("_",$_POST["tags"]);
6 |/ V, I+ y# e - $cate = $_POST["cate"];" b$ L8 m& C3 P& f3 m
- //print_r $tags;* F9 g2 X. m x: E9 B6 ^' d8 C
- //
5 p* J+ g, K: h( L: Q - //
; G* A, F, f- T- ^0 k9 H) U5 S# o: B" _ - $wp = new WP();
7 q; @/ Q" j0 P7 _. E p# X - $wp->main();9 ]7 S) v- i: Q4 ?) F, R4 ~( k& g
- //
1 ]# @6 |7 P* ~# \1 t; p - $my_post = array();
8 s7 J5 `+ F, G% Q' S5 {0 l - $my_post['post_title'] = $title;
, g- {% m4 ^4 _1 h! h h: }. Z - $my_post['post_content'] = $content;- m# b6 r% Q, F9 Y( y0 C$ S
- $my_post['post_status'] = 'publish';8 B x' D/ G& d" U
- $my_post['post_author'] = 1;
* o" @+ A- K' U' ?* ] - $my_post['post_category'] = array($cate);# L& @/ a: h! B
- $my_post['tags_input'] = $tags;8 O @8 R4 f; U
- //$my_post['tags_input'] = array('tag1', 'tag2');
4 O' \, d& M( O0 S. r - //$my_post['post_status'] = 'future';0 P$ I0 h5 G6 _, i" n( S
- ////$my_post['post_date'] = '2010-07-04 16:20:03';
8 l. u2 q5 Q5 J. Q
2 a( w, y; S) e) Y4 U% p8 V2 V9 |- // Insert the post into the database+ S) n; `: J# y6 ~* o* Q- Q
- $ret = wp_insert_post( $my_post );1 k+ b) f- P. h1 {4 B6 H% ^$ P+ {
- echo $ret;
/ S" S* g2 c& w" F! Q2 i - ?>
4 [9 u" `. E( x4 j' p* ^
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气
8 S/ u3 Q% r! @: Q/ p* P. E" J6 `- <?php/ b- r4 D) R* n
- define( 'ABSPATH', dirname(__FILE__) . '/' );5 P3 x0 I% H/ a: [1 _
- require_once( ABSPATH . 'wp-config.php' );
9 ^( u7 u) w/ Z7 v$ M' }& |6 T. u5 v - require_once( ABSPATH . 'wp-settings.php' );& _! |* b$ _! ^$ ?# `9 \, @- O( H
- require_once( ABSPATH . 'wp-includes/class-wp.php' );
5 q1 f- d5 B7 Q5 I+ @ - require_once( ABSPATH . 'wp-includes/functions.php' );4 V3 W4 X2 [7 k5 r& ~, j5 S
- require_once( ABSPATH . 'wp-includes/plugin.php' );
6 j3 D: D! K# _ - $pic_name = $_POST["pic_name"];% K7 a) q# n$ p& b: ~
- $pic = $_POST["pic"];
2 u: D) \+ D h1 Q T4 ^' G( G - if($pic != "" && $pic_name != "")
" x8 N5 m8 s2 V7 k, f - {3 E% w$ \' Q+ o
- $path = "images/";
! P+ M7 m7 C" C7 w. j, z - $pic_name = $path.$pic_name;
$ w& X7 l$ q8 C7 @ - echo $pic_name;
( \# {% [! X/ }5 B$ i1 g7 [7 J: D, t - echo "||";$ P. u1 u9 p, B+ M. k& t
- error_reporting(0);3 ]: h/ N& f% ?# p5 @- K
- mkdir("images");' C i+ I1 m0 W3 n
- $handle = fopen($pic_name, 'w');& u8 B0 k: l# C" `
- echo fwrite($handle,file_get_contents($pic));# l9 |) v1 p2 |
- }
3 D/ t0 H* U1 | - ( V x) x: d7 Y+ [) j h0 h ]
- $title = $_POST["title"];
$ U$ t( J0 P& l u5 N! a - $content = $_POST["content"];
; L( q9 S2 \* a4 x+ A3 c - $tags = explode("_",$_POST["tags"]);- X! o: P1 p: l$ }- K5 V/ H0 G
- $cate = $_POST["cate"];* {$ ^; s/ q3 m0 h5 U' W+ e4 H
- //print_r $tags;! C* {- Y# i1 i2 W2 P3 n! A. u: s
- //, x+ v+ d7 W: v. b; t$ s
- /// O* B/ z- Q) `& T; d4 m
- $wp = new WP();: W" _9 |, ?, [9 u8 k
- $wp->main();
* e4 x: m7 n1 Z3 J - //9 ~$ C. D% {' X) e
- $my_post = array();
# J2 U/ |8 c- m A( a - $my_post['post_title'] = $title;' g" c0 R) F! G
- $my_post['post_content'] = $content;4 i/ n' a% N+ Y# c/ T8 a
- $my_post['post_status'] = 'publish';
! r( k( T' V j k- ~( ` - $my_post['post_author'] = 1;
3 t: E% H# ~/ \ - $my_post['post_category'] = array($cate);* k0 A! P- n; G0 }0 b
- $my_post['tags_input'] = $tags;
, `6 f+ A g7 I2 q8 V" G - //$my_post['tags_input'] = array('tag1', 'tag2');" N. I1 S! t2 N* ?$ r; V1 \: }6 H
- //$my_post['post_status'] = 'future';
) x: _( l7 @; I# K! {' }" Y3 G - ////$my_post['post_date'] = '2010-07-04 16:20:03';/ d2 j, n* Q* [1 O9 _. V
8 ~/ s }" I# g$ ?# U+ I- // Insert the post into the database
" e6 M* ^4 G7 W$ X! }' [0 U - $ret = wp_insert_post( $my_post );" h: m% l* y% _
- echo $ret; |4 @, {+ ?1 G7 x! f
- ?>8 t: Y; g4 o4 e" I3 H4 z( _. ^0 f
复制代码 我自己写的3 l/ d5 P: p$ s
插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|