|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑
1 K7 W" J1 L+ I! h# \& G8 }* O. b& I7 i- h9 }/ t' n- G
直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持
5 r2 R8 o4 u) ^9 Z: H
' e3 U4 q* ]* U- n) {; Q$ r3 ^: c我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了+ F9 P$ i3 q" ]5 E2 i8 S0 E& v( p( z( J
N6 G, D) N8 Q0 I# U7 L' s方便实用 简单 何乐而不用呢- 2 c0 D6 ^+ j9 J5 |
- <?php
# F4 J* j2 N9 K |
- a( d- {# _6 M- define( 'ABSPATH', dirname(__FILE__) . '/' );
7 a! D& S$ l% d - require_once( ABSPATH . 'wp-config.php' ); |6 l1 J) r( o# \! O0 d+ u* o6 ^
- require_once( ABSPATH . 'wp-settings.php' );' a7 e2 c7 I/ D/ Y6 _* d
- require_once( ABSPATH . 'wp-includes/class-wp.php' );
: p! R% ?# {6 i5 p+ y4 T8 Y# y - require_once( ABSPATH . 'wp-includes/functions.php' );5 ?; Z* r8 ~, S, v" f' X
- require_once( ABSPATH . 'wp-includes/plugin.php' );
4 X: V6 r" b7 o K - $title = $_POST["title"];$ }/ a1 Q/ a* O
- $content = $_POST["content"];" C) ^) o; b7 S" |: {
- $tags = explode("_",$_POST["tags"]); m! w& Y6 x' b p+ L, \; M% G- g; S8 j' Z
- $cate = $_POST["cate"];0 t5 v2 ^ a( ^2 p. d8 W& C
- //print_r $tags;: k7 E" ]: V5 M8 j
- //' p/ D6 I, x; ]9 p
- //# Q$ h7 R, T$ r6 I% l
- $wp = new WP();* K( \! W: O* e% t1 l
- $wp->main();; L- [ Q* |1 M
- //
: _- @+ j* [2 c+ Q( h - $my_post = array();; g9 e' j5 ]. h" S4 ], [
- $my_post['post_title'] = $title;: l$ W [4 M7 P6 X5 P. U7 M/ X( E
- $my_post['post_content'] = $content;
) ~. n% z X4 D& p/ K3 ]3 ? - $my_post['post_status'] = 'publish';! W8 T+ u- x* Z* B: Q7 u6 H+ M
- $my_post['post_author'] = 1;
3 q5 |7 H: ^$ N' @1 x( x- V - $my_post['post_category'] = array($cate);# F# @! d) f, G
- $my_post['tags_input'] = $tags;$ Q! J- w( f% i+ L8 T' l* F
- //$my_post['tags_input'] = array('tag1', 'tag2');
# p7 t. n! b0 x; U1 i" c1 w - //$my_post['post_status'] = 'future';) U9 b" l& k. Q- I/ V9 D' d
- ////$my_post['post_date'] = '2010-07-04 16:20:03';. V1 h6 V( d4 d7 d! z6 U, S1 g
$ w' Y, R* m3 B8 ?; I M- // Insert the post into the database
: x+ T0 d0 U. [# s* O7 I - $ret = wp_insert_post( $my_post );
. D+ C7 `5 ^8 f - echo $ret;" L8 ?9 S8 f: g" _1 e7 |
- ?>
8 Z2 ~& f# V2 P! N; N
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气- 8 Z/ I! b5 W5 S# N3 `6 I
- <?php
/ X+ x/ @! X/ c8 ~* N - define( 'ABSPATH', dirname(__FILE__) . '/' );8 q# }, ~3 N' R
- require_once( ABSPATH . 'wp-config.php' );
3 [0 F1 V0 n" Y% @3 e1 e - require_once( ABSPATH . 'wp-settings.php' );: _8 j. @- @# H/ O. U, y& F
- require_once( ABSPATH . 'wp-includes/class-wp.php' );
- K0 |7 S8 x& ] - require_once( ABSPATH . 'wp-includes/functions.php' );
3 o, y' H. m* Q) @& L! w) {" O - require_once( ABSPATH . 'wp-includes/plugin.php' );
" s" Y: m8 v( F' S* @ - $pic_name = $_POST["pic_name"];
- {8 V& o; k. ?/ A - $pic = $_POST["pic"];0 I& G k* Y! u0 k3 i0 d
- if($pic != "" && $pic_name != "")
5 D& V, T5 s: K0 f* K8 J1 U - {
+ [. d! N9 V8 D f5 |% S% [" `" U - $path = "images/";9 A; _# t2 U( f% r: V3 Z- g
- $pic_name = $path.$pic_name;) y) G2 p }. l3 g z
- echo $pic_name;
* k( |6 n* p" A3 S7 ? - echo "||";9 ] P: I# j' u- M; v5 ~
- error_reporting(0);+ V9 ^- o9 u) i0 f. G
- mkdir("images");: T0 L- [/ E( ?+ F. T$ l; A
- $handle = fopen($pic_name, 'w');
, J% J' S* @3 I! T- J/ a - echo fwrite($handle,file_get_contents($pic)); t3 e# `" W9 K& N" Q& ]
- }
# V' u6 y' g0 o" p# _( ~" m# U
7 J3 \& I/ p% B- $title = $_POST["title"];# T* ?/ U; Q7 f1 T9 t( I! }
- $content = $_POST["content"];
0 V# i2 }* _; K9 X6 }) P - $tags = explode("_",$_POST["tags"]); G c% @" ~; t( Q% i' f! d
- $cate = $_POST["cate"];
' u4 D' X+ ?. o( Y) } - //print_r $tags;$ Q2 t* m8 H' ]+ y8 L) g2 j
- //
9 ? g- p" O4 T- p( ?, n7 y - //. I. s4 v g1 [+ D7 J
- $wp = new WP();6 ?# ]7 [# q' f/ B+ Y+ v
- $wp->main();6 K: A$ K; e+ l; o: o% }1 B
- //9 }4 m# N7 D2 [( u1 A
- $my_post = array();6 l& E# s! _( h! Y
- $my_post['post_title'] = $title;, B! `; M6 L ^8 |/ M+ B, G3 s z- [
- $my_post['post_content'] = $content;
5 g* G- i7 ~, s/ |! L- T - $my_post['post_status'] = 'publish';6 q! ?! Y' J8 d% [
- $my_post['post_author'] = 1;$ ~' R# _# z, z) F6 d1 }$ b$ W% N& ]
- $my_post['post_category'] = array($cate);7 ]9 A5 H2 m1 y+ C% _
- $my_post['tags_input'] = $tags;1 x6 d/ P" w1 R9 z
- //$my_post['tags_input'] = array('tag1', 'tag2');# x0 k! c( ]2 p
- //$my_post['post_status'] = 'future';
- m3 @8 p! h. F( f$ ` - ////$my_post['post_date'] = '2010-07-04 16:20:03';
2 s6 l' P; @; [( \9 m - / v! ^# n6 |) S2 k; s! X% ]
- // Insert the post into the database
4 u! P: Q2 ?* a F1 h8 K - $ret = wp_insert_post( $my_post );# H& [3 q1 M- U6 `; {8 x4 N
- echo $ret;- l7 ]' b. B0 T
- ?>
( P; F$ v( E( y& S
复制代码 我自己写的
& K* P" Q4 }, s4 h! U$ j P! m5 a L插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|