|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑
2 ^& b# X/ k; c# J
8 D9 B9 d9 F: ~6 ], l直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持
' W) U* ?8 D! J% @: g! c3 S+ A- _
我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了
# t" i( J0 L7 Q& Q4 B# O/ ^2 w
- C7 G' K1 A' f方便实用 简单 何乐而不用呢- : }) R% y7 ^- G5 D% U$ J# S @
- <?php, B) q% h# v( y1 d# ]& h4 E& @8 y
- 1 T7 ]- K! u6 G) g4 O0 q. o
- define( 'ABSPATH', dirname(__FILE__) . '/' );6 U" O' h! w. {# v' Q6 u
- require_once( ABSPATH . 'wp-config.php' );
: a# g$ @- [; z5 [6 {6 ?- ^; h5 V - require_once( ABSPATH . 'wp-settings.php' );! r5 R& n% ^+ d- t6 z& e& A7 V$ V% v
- require_once( ABSPATH . 'wp-includes/class-wp.php' );& b0 b% F! }* H/ ^8 f: \
- require_once( ABSPATH . 'wp-includes/functions.php' );
# H+ y4 A1 p) P - require_once( ABSPATH . 'wp-includes/plugin.php' );1 ]. T; o8 d" v% Z) G( Q, q
- $title = $_POST["title"];
/ i E1 n; N6 I: ~# B! g+ K( i1 [ - $content = $_POST["content"];
$ o- c# O# A% o! c. @3 j - $tags = explode("_",$_POST["tags"]);
( b4 o! d9 t8 V) N9 y - $cate = $_POST["cate"];7 Y: V, j; W" t2 P+ n) l7 j
- //print_r $tags;. Y3 t" H# |: e. o0 n7 P" j
- //" X& _" i! F. w2 \8 h) n* U
- //
& l# R' g$ I9 G - $wp = new WP();
- z9 t) |6 r4 W( U; g2 c - $wp->main();/ M# F, v" V3 B
- //
5 B# L2 J0 f" ?+ H8 i - $my_post = array();+ i7 }: T+ Y8 g5 \+ U. |$ f$ |, i
- $my_post['post_title'] = $title;
\7 B/ x+ B: U' R - $my_post['post_content'] = $content;
; j$ d z- [/ n- G t - $my_post['post_status'] = 'publish';
; z. P! @+ T7 o9 r( t4 L+ {$ Y - $my_post['post_author'] = 1; ?6 w. S8 k! H) p7 ?
- $my_post['post_category'] = array($cate);
2 \7 Y& ?0 S Y" e, C) a4 r - $my_post['tags_input'] = $tags;
/ \7 q- z* e& Z0 |. X# v - //$my_post['tags_input'] = array('tag1', 'tag2');
" S$ K$ ~5 y7 n4 b, x# T - //$my_post['post_status'] = 'future';% Y9 x, v2 s+ R/ l( l
- ////$my_post['post_date'] = '2010-07-04 16:20:03';- q8 S7 w- S2 J- v+ |/ M
- c v# [4 i) H5 \1 a4 `) b- E# F# a- // Insert the post into the database
0 `; e, X t8 d8 J9 ?, H7 R. x% k - $ret = wp_insert_post( $my_post );" k! {. v+ y* [5 V4 ?
- echo $ret;5 y8 q, X9 _) m. b/ L: Y- W+ V
- ?>
, o* q3 T. O, e- d
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气
* W, a# }1 n; y/ A- <?php
, M4 p1 K' s* b/ y: ` @+ L3 B - define( 'ABSPATH', dirname(__FILE__) . '/' );: p5 m* v# t4 Q' `
- require_once( ABSPATH . 'wp-config.php' );+ O8 d. F1 N4 \+ u
- require_once( ABSPATH . 'wp-settings.php' );
: x0 G% q! c) W x+ p5 u - require_once( ABSPATH . 'wp-includes/class-wp.php' );/ \; O1 s2 F4 y+ j( ]5 [! _ v) u
- require_once( ABSPATH . 'wp-includes/functions.php' );
( O: r; r T+ j' G$ n - require_once( ABSPATH . 'wp-includes/plugin.php' );- S3 \1 k9 R+ k r7 i# F; p5 o
- $pic_name = $_POST["pic_name"];
5 @' a/ D: s" q& q4 [- g& c - $pic = $_POST["pic"];
5 L7 P) `0 {8 y1 S9 R* L - if($pic != "" && $pic_name != "")5 b& d6 Y" N1 c. D: m
- {: N/ E. w4 |- ?0 y# c% l- e
- $path = "images/";9 `4 `7 M) A `1 l. l0 @
- $pic_name = $path.$pic_name;
1 ] u. I0 X1 G) t e' c' v - echo $pic_name;8 k" s7 V2 ~4 `. T! w
- echo "||";
& K. N0 F$ \4 T2 d' a: A - error_reporting(0);
/ s( r5 r& b5 l2 h* P7 w# [1 y - mkdir("images");/ C0 a. f" {, [. ]1 s
- $handle = fopen($pic_name, 'w');8 S9 n6 t8 [! h
- echo fwrite($handle,file_get_contents($pic));" G: A# ]$ m3 c( D
- }) D; ^( q7 U% w5 N
, }0 l8 Y. r9 P n0 `! o4 X- $title = $_POST["title"];
. B& w I/ B3 ~1 ]; | - $content = $_POST["content"];$ j9 d0 Z# l: X
- $tags = explode("_",$_POST["tags"]);* G: w2 C. F7 Q, ]4 \8 C; s* V
- $cate = $_POST["cate"];
' D' N; b+ U7 t; y' N9 S3 k' q4 l( e - //print_r $tags;
1 F5 R: d8 `, V - //' R/ L2 J. V$ H2 N2 o8 H
- //
1 V$ ]) r* g$ f( w: Z8 L' T5 T5 r - $wp = new WP();
2 e4 D( k% K: i0 K$ ?6 z - $wp->main();6 F: W, V ~" x9 s9 h/ q
- //
& }4 E- N1 G1 }) R- D, l - $my_post = array();: w5 g, `7 p: {. ]9 n& e
- $my_post['post_title'] = $title;
. E* m0 w6 b( P" u - $my_post['post_content'] = $content;
9 s4 {1 Z) h0 k4 x# q! P) a - $my_post['post_status'] = 'publish';' ]' [, k1 O& R1 a, p D* i9 i! x1 n
- $my_post['post_author'] = 1;# I5 F7 \5 t7 ?( [1 B. m
- $my_post['post_category'] = array($cate);; ^( J! J$ B: E: e5 m. z L* p
- $my_post['tags_input'] = $tags;. P7 I' ?) S# V- m; o R
- //$my_post['tags_input'] = array('tag1', 'tag2');
: m# B4 Q5 F1 B8 d5 W - //$my_post['post_status'] = 'future';
8 \0 D1 h( I" d- J - ////$my_post['post_date'] = '2010-07-04 16:20:03';6 Z @+ w6 }3 k7 p
- * i2 g( D. r0 D3 y4 t" e" ]
- // Insert the post into the database1 g2 s/ w" f0 l
- $ret = wp_insert_post( $my_post );9 o) |" s' v' l: f7 p' ]" T, Q
- echo $ret;3 t8 O# \% X5 m
- ?>; `) f' b) q# [4 X
复制代码 我自己写的
6 {0 |1 b0 m2 ~" R* J插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|