|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑
3 h" _( t" A( B& s; e; M, g D" D- q4 Z# l/ n ]! Y C! z
直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持% P' P. h" I3 I1 }' n7 X
0 j+ V& R" K$ i7 V$ N* B. q
我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了2 q+ V& v1 J! _( m k
7 W. S5 X% v" d0 h4 c- E: V
方便实用 简单 何乐而不用呢
, G5 K5 ?' _$ [6 \# R. S- <?php
& [) t! Y3 T+ L3 P. E - 4 T; S, w4 y+ g( q; n& `
- define( 'ABSPATH', dirname(__FILE__) . '/' );
7 E: l5 P8 q) T: @ - require_once( ABSPATH . 'wp-config.php' );! \) n4 p4 `4 p6 a& g
- require_once( ABSPATH . 'wp-settings.php' );
0 y U8 a! N/ O - require_once( ABSPATH . 'wp-includes/class-wp.php' );
4 j4 J+ c ]# Y' e- ` - require_once( ABSPATH . 'wp-includes/functions.php' );! Q) F: ^2 s: }3 x3 |3 n
- require_once( ABSPATH . 'wp-includes/plugin.php' );
M7 y; e- A) }0 d - $title = $_POST["title"];
9 |& r }% e$ C4 x/ L2 x7 I - $content = $_POST["content"];! g' S" B# \1 Y* q. Q
- $tags = explode("_",$_POST["tags"]);
8 B) f+ ?) F8 w5 W, P1 l - $cate = $_POST["cate"];# {+ p2 |6 Q8 ^7 x" E+ y
- //print_r $tags;
3 c6 i) j/ E4 P: q2 d, x: B! p! j - //0 T# r! A6 X$ H) I" j0 K# `
- //( l( D( z M7 l V! J7 Z0 U
- $wp = new WP();
. M. [ p" _% b8 P3 X - $wp->main();
( A2 [! V8 _) c" f0 |3 A - //1 N5 F& Y* i/ ^/ u Y
- $my_post = array();: A. _7 f( u- Z$ V; j2 \
- $my_post['post_title'] = $title;
" }8 Z3 W! j# A" ^ - $my_post['post_content'] = $content;7 v2 j. T9 e9 W9 l9 _5 F. L7 M B
- $my_post['post_status'] = 'publish';% R6 ]6 V, q& J, F
- $my_post['post_author'] = 1;! D: N5 L; @/ F; }- {( \9 i3 L
- $my_post['post_category'] = array($cate);
( C1 @- j/ M8 G0 L5 O. |8 b - $my_post['tags_input'] = $tags;
+ d! ^7 ^; W; B4 b& t6 v) ^ - //$my_post['tags_input'] = array('tag1', 'tag2');
1 L6 [6 m+ E0 H" J ?1 P - //$my_post['post_status'] = 'future';
: A% ^9 M( K8 E8 u/ ` - ////$my_post['post_date'] = '2010-07-04 16:20:03';
0 Q- j$ |/ d7 O - - f7 a3 |5 a2 O
- // Insert the post into the database7 \5 f1 m8 S# x9 T1 L
- $ret = wp_insert_post( $my_post );
* c- e1 ~, ?0 U) t - echo $ret;* l" c8 w2 t0 S i6 a. x
- ?>
1 ^- I. P, ^4 q O
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气
" u( x+ J" u$ z* d6 J- <?php' ~* r# j, s) R; S
- define( 'ABSPATH', dirname(__FILE__) . '/' );- [5 T& [6 J% P$ ^8 I1 b
- require_once( ABSPATH . 'wp-config.php' );1 u! J+ R4 e8 j8 `* y4 e
- require_once( ABSPATH . 'wp-settings.php' );8 y: B; F# D1 E2 M& `+ |
- require_once( ABSPATH . 'wp-includes/class-wp.php' );
a' i: n5 O' M* q& s - require_once( ABSPATH . 'wp-includes/functions.php' );
1 [4 l, F1 e$ n6 m6 Q( ` - require_once( ABSPATH . 'wp-includes/plugin.php' );2 \+ y$ T( E. D7 U7 l
- $pic_name = $_POST["pic_name"];5 ?. |- r8 g4 J
- $pic = $_POST["pic"];! f( I, D$ G2 A4 i+ S. w
- if($pic != "" && $pic_name != "")
- A9 ~: ^( x9 n' g# ?8 L - {
4 N$ g/ q$ S$ M6 \0 l - $path = "images/";0 K7 j4 U: d( c4 [" l3 i6 r% W
- $pic_name = $path.$pic_name;
( H* P# c( N5 G2 R) V+ o- Q% l5 |$ g6 ? - echo $pic_name;4 |5 d9 z6 t: w0 n! Z* A# o1 Y7 U
- echo "||";. B% F. X) X" i3 ^% X# c
- error_reporting(0);# b6 H7 Z+ K h7 s% T
- mkdir("images");: v4 ?% y6 ^. F8 Y. G9 H% K
- $handle = fopen($pic_name, 'w');9 a7 J/ n) M4 G$ G% L
- echo fwrite($handle,file_get_contents($pic));
) s& C# }! o1 X - }% Y/ D5 D- p. t2 r' d, P% U
- ) \1 u7 i0 _6 d; N0 n
- $title = $_POST["title"];
" |8 Y6 s( D! k9 r0 _2 J - $content = $_POST["content"];+ N8 _5 R' H' z/ t$ X' k2 y% ]
- $tags = explode("_",$_POST["tags"]);8 D' d9 j9 F! g) w9 `
- $cate = $_POST["cate"];
0 |, k& C4 S' B+ O4 w* I1 N/ z - //print_r $tags;
7 M, j2 k G- e `- J9 Y0 W4 O - //) G. Z0 \3 R' G# K6 H& B7 s
- //
' Y$ F1 b- _/ Z; D; `: Q - $wp = new WP();* A$ n5 T$ D6 i
- $wp->main();1 R, R A0 m R; ~& T
- //
0 [* D# f- t" Z& `0 v4 o m - $my_post = array();
|; K; N; c* {. D7 U# ?6 A+ A - $my_post['post_title'] = $title;' n8 g9 x9 g. c' J3 R- p- {* {
- $my_post['post_content'] = $content;
$ U1 u0 V( [3 h6 A; m0 k1 n2 z - $my_post['post_status'] = 'publish';
8 @7 M6 i! ?) e: \/ a. F - $my_post['post_author'] = 1;* B% Y {, u; r6 L6 s/ e
- $my_post['post_category'] = array($cate);
- C! ~* B8 v: ^% {0 u - $my_post['tags_input'] = $tags;5 |: X& U( M8 v" V% m& v8 Z
- //$my_post['tags_input'] = array('tag1', 'tag2');
" P6 q$ l* O: O r# N9 K - //$my_post['post_status'] = 'future';3 ?9 W3 U N% b3 M7 D j
- ////$my_post['post_date'] = '2010-07-04 16:20:03';
! b" U% Q F: T5 F! s6 r - 4 \& |7 l, o3 c7 }; g# l7 q
- // Insert the post into the database" O( I1 J! U& I1 w
- $ret = wp_insert_post( $my_post );
/ A9 B8 @' P, t# p1 q - echo $ret;
5 U. {; i6 `# L$ N5 e, `& `5 K( q' L - ?>
& ]9 G: E9 [! N; e7 \- {
复制代码 我自己写的
, J1 ^! M' o0 G+ G" ^1 [, A插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|