|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑
# K" L. t4 S. ]# _1 D; S/ r0 G
- o( j s$ U1 U( R" ^直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持% q$ T2 r2 P. U( g( I0 P) X) t
; h( d4 J* P& l
我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了: n; O- n& D' a1 ]4 f" g* ~! h; Y4 `$ l
9 l* g- X2 |& L# H7 D0 _方便实用 简单 何乐而不用呢
0 h- d r9 L) n2 r- <?php
/ W$ s. ?) S. V: a o. e
7 P9 N# u7 x9 ]0 ~9 o- define( 'ABSPATH', dirname(__FILE__) . '/' );
) h+ T! [0 S a1 N. t% y7 i - require_once( ABSPATH . 'wp-config.php' );/ k0 Z7 [$ f) a% s6 T0 C* m' B
- require_once( ABSPATH . 'wp-settings.php' );
/ C' o9 M: @1 ]0 q- E/ T5 T - require_once( ABSPATH . 'wp-includes/class-wp.php' ); S8 @. \' Q& ]7 e. `5 [' o
- require_once( ABSPATH . 'wp-includes/functions.php' ); l5 V0 D1 ^5 Z( a
- require_once( ABSPATH . 'wp-includes/plugin.php' );
6 w3 J: z8 G& n4 ~ - $title = $_POST["title"];
0 b7 T) ^4 V8 s; A - $content = $_POST["content"];- \4 ~- R3 {' S7 b* V
- $tags = explode("_",$_POST["tags"]);
s6 K# @* u0 Z8 |2 R' i - $cate = $_POST["cate"];" G/ q% E5 R3 s- g
- //print_r $tags;
4 G8 S2 b$ ~5 I% `" P - //
7 p" f0 J5 N! u6 Y) f! N1 n1 T - //4 B! k5 D+ o, R& o9 Y
- $wp = new WP();9 k7 r9 X+ }/ r
- $wp->main();
5 C1 r% U! R/ |, N9 ? - //
( b8 b m# M+ f6 @ - $my_post = array();
Q" \4 j3 B7 A0 ^* f" F$ y - $my_post['post_title'] = $title;' k a& X- E [8 K! q% c, X
- $my_post['post_content'] = $content;
6 ]2 P7 O9 k4 a% W; n% _+ i: u- P9 b - $my_post['post_status'] = 'publish';5 Z: l, Z# t6 C- u- O
- $my_post['post_author'] = 1;
1 m( Y6 V2 n# |# v4 Q# e& J4 K, n - $my_post['post_category'] = array($cate);
l4 i& h4 W/ D. V5 B - $my_post['tags_input'] = $tags;- s, {/ J" j0 e# Q8 j- n K
- //$my_post['tags_input'] = array('tag1', 'tag2');6 f( U. |6 Z2 X; c/ |1 n" i
- //$my_post['post_status'] = 'future';
! q5 U/ H' q; `3 j - ////$my_post['post_date'] = '2010-07-04 16:20:03';
# k) h: L$ f7 S5 Z) q/ k1 [
9 e/ T( y5 o; x* _: ^- // Insert the post into the database
5 _, m5 V2 G) g ]! c1 j - $ret = wp_insert_post( $my_post );
N, y7 x( u9 f( O8 O2 p* r$ \! y; X - echo $ret;
; X1 {" l |7 e% c# X - ?>
) F* I1 ?8 N. G( H1 O
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气
9 M' `4 L( q3 C! U- <?php3 [2 K5 n4 l1 Q1 f+ ?) ^
- define( 'ABSPATH', dirname(__FILE__) . '/' );9 o' V5 ~- s' e
- require_once( ABSPATH . 'wp-config.php' );. K/ N# }) _5 E3 ?
- require_once( ABSPATH . 'wp-settings.php' );' }9 G: T! u0 S8 a* f
- require_once( ABSPATH . 'wp-includes/class-wp.php' );
6 `8 l) t- l6 q& W" Q ]+ S! k" K - require_once( ABSPATH . 'wp-includes/functions.php' );
$ v G& W* I T - require_once( ABSPATH . 'wp-includes/plugin.php' );7 \3 H g! q% K u. T
- $pic_name = $_POST["pic_name"];4 z* k4 C6 Q. U9 o' J L+ }
- $pic = $_POST["pic"];) G* S' V9 Z4 i% a0 l. s. x4 `
- if($pic != "" && $pic_name != "")- e, I$ V& ^) M X
- {
' j& n4 d; a5 V( L" b8 Q - $path = "images/";
( o8 ^- I P( ^ A - $pic_name = $path.$pic_name;1 U% Y1 m1 i, @" E( i
- echo $pic_name;
D' z8 x7 B5 U! @- E - echo "||";
. z. Q1 k* I, O! P3 H - error_reporting(0);8 i8 `: t5 I( U# i$ V9 p7 C, h
- mkdir("images");0 F6 `3 [( g) z* K8 W& \/ @& |
- $handle = fopen($pic_name, 'w');
1 i* C4 b7 p/ o; ` - echo fwrite($handle,file_get_contents($pic));$ b0 Y% V @( Z
- }& f; I8 }! e0 G
1 l6 ^* {$ ]! f: `- $title = $_POST["title"];" T, X$ `9 H0 p9 \, ] L1 W
- $content = $_POST["content"];
. D: E9 w( A+ b1 R - $tags = explode("_",$_POST["tags"]);% x0 p8 I* b0 \
- $cate = $_POST["cate"];
# `' |3 j+ H! ~' r1 O - //print_r $tags;2 A3 U1 t* P) J) c$ Q8 E! I
- //% ?5 T) N! D; z; G6 G7 Q c9 D
- //5 Z+ `1 D8 u( X* s( W) r5 e# b |
- $wp = new WP();
' @; b0 ^7 O" z$ c - $wp->main();# ^6 V; u% t7 A# y5 _1 e! K
- //7 ~4 E% T' H. t( T( D6 k' [
- $my_post = array();+ C0 ?# _: F1 \' Q7 c7 D( U
- $my_post['post_title'] = $title;# d0 ?4 o, a* ~9 d
- $my_post['post_content'] = $content;
q& ?; X! e' u! v - $my_post['post_status'] = 'publish';6 ~9 a, F5 h. ^$ U0 d
- $my_post['post_author'] = 1;0 ]) s1 b; K# G0 R, a
- $my_post['post_category'] = array($cate);
3 a7 Y5 q A1 x1 t0 _ - $my_post['tags_input'] = $tags;
! @) Y& r- _3 ? - //$my_post['tags_input'] = array('tag1', 'tag2');- x v2 L; P# G+ \
- //$my_post['post_status'] = 'future';' l/ `0 H; r/ q* `
- ////$my_post['post_date'] = '2010-07-04 16:20:03';# ^) d2 j$ n. T7 F& G7 L- J
- m( K. ?( k* t4 W; f& p- // Insert the post into the database
" X8 @! H! [7 A Z* S3 W4 y - $ret = wp_insert_post( $my_post );
/ ]2 z$ W6 I# J - echo $ret;8 ~" a% \: _# n) G/ {
- ?>
9 M( v- P. t! _( b4 R- w3 m
复制代码 我自己写的
1 M3 H; u0 @$ j9 s插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|