|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑 % Y$ }% g# T0 V
* ?6 y& V" k. D6 B/ b' a直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持. T; m2 z$ P c$ m
* a9 |6 Z; o8 h: R, }我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了
& J, Q ]$ ^4 L5 J7 S/ I! p
1 _5 T% R% A) K$ u. K H: N方便实用 简单 何乐而不用呢
7 D4 ^ P# u+ y6 `- <?php
* N+ F5 B+ e% ]! C9 d# t9 u% A - / R" x$ {4 b8 B
- define( 'ABSPATH', dirname(__FILE__) . '/' );" u& i! H2 D! u! u" }0 Y9 [7 ], u; @
- require_once( ABSPATH . 'wp-config.php' );
# X$ [9 x1 c+ P. q3 S8 D - require_once( ABSPATH . 'wp-settings.php' );
% \# |: V r# q+ K- E# K - require_once( ABSPATH . 'wp-includes/class-wp.php' );
g1 [1 r+ V, { - require_once( ABSPATH . 'wp-includes/functions.php' );
% S' N3 ^) n6 @' x) _+ v - require_once( ABSPATH . 'wp-includes/plugin.php' );
/ g5 z$ l5 W; Z& B- Y7 Y: e% N3 e - $title = $_POST["title"];# z+ w9 W1 C! [. H
- $content = $_POST["content"];1 m: j' M4 z1 e5 q/ N# B
- $tags = explode("_",$_POST["tags"]);
" q8 G( y: e1 E, ]) \6 m4 F - $cate = $_POST["cate"];
- b( {9 p9 V' `! ^, Y9 U - //print_r $tags;
. b# x0 h- j3 z, O2 K8 L - //( u; q4 [% i& B3 h! w
- //
! J0 z9 ~2 C7 _9 s, Q - $wp = new WP();
; i" Y0 }2 { `& l( }& M - $wp->main();
+ B( m5 x# g3 }) E# g - //3 i; o( `! I- h% V
- $my_post = array();
# W2 z0 W, |' }. U - $my_post['post_title'] = $title;/ V* q5 \( G2 Y \
- $my_post['post_content'] = $content;
e2 l; h5 m2 Z) h. M - $my_post['post_status'] = 'publish';
6 @ @4 V, d. ?% i$ n - $my_post['post_author'] = 1;' M3 _4 I' g" i9 V G4 M5 N3 [! m4 ~) o
- $my_post['post_category'] = array($cate);
7 ]& ~7 Z. e/ t+ w - $my_post['tags_input'] = $tags;4 x6 O* x1 k7 v0 h
- //$my_post['tags_input'] = array('tag1', 'tag2');8 ], \ j7 h& P2 P+ i/ c
- //$my_post['post_status'] = 'future';' b; Y* [2 s- K5 M' x
- ////$my_post['post_date'] = '2010-07-04 16:20:03';* y5 l8 n, k+ [
+ D7 C0 w- i `- // Insert the post into the database
& ^) e% n+ \8 e, ?' N, U - $ret = wp_insert_post( $my_post );
. u9 [) G" b6 Y6 ], R& Y - echo $ret;5 x3 R. ]) [7 I4 g9 Y
- ?>" t1 P+ Q( o" f& B; \: g6 S
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气- : Z8 c# |+ k' G2 U) ^
- <?php2 V0 m! ]1 W8 ?9 P3 E. |* G# B" _
- define( 'ABSPATH', dirname(__FILE__) . '/' );/ }4 z! O9 Q9 m) d' k: x! j$ A
- require_once( ABSPATH . 'wp-config.php' );
' i: C) O& K7 R' H! _6 w7 v - require_once( ABSPATH . 'wp-settings.php' );
- X, b3 M% i: r4 D& p: | - require_once( ABSPATH . 'wp-includes/class-wp.php' );
) B4 f% h( F% D2 ] - require_once( ABSPATH . 'wp-includes/functions.php' );8 ^) q6 u) [ c+ W& x* W2 F
- require_once( ABSPATH . 'wp-includes/plugin.php' );
. c) \7 W) ?" s8 _ - $pic_name = $_POST["pic_name"];+ X2 _/ W- ~4 Q- ^3 s; b, m/ e' s
- $pic = $_POST["pic"];9 [5 i9 i, r% R7 e1 B8 e2 P" a, P
- if($pic != "" && $pic_name != ""); U$ n: m9 C! W# t* k2 {
- {( P$ B1 N4 q+ n
- $path = "images/";1 O4 _" [& X2 I2 x0 F/ r" W1 Y$ [
- $pic_name = $path.$pic_name;2 p' q; B5 n5 M* q
- echo $pic_name;; }7 x: p0 h5 M0 F# \
- echo "||";
7 Z/ M j p9 I$ K6 U - error_reporting(0);
: u N$ f/ s; g# Z% N! V b - mkdir("images");% d# B6 \- s' d
- $handle = fopen($pic_name, 'w');; T" ] `9 E8 S& j! H5 c8 q
- echo fwrite($handle,file_get_contents($pic));
& w6 w$ c, {- \ ^2 w - }
2 n& V8 U) J, b8 p - 1 [1 v7 O h/ _; f8 H
- $title = $_POST["title"];" _1 Q9 o* g7 `8 y/ d) Z# {+ L
- $content = $_POST["content"];
+ U+ H% g1 Q0 Z- @: V# {" t - $tags = explode("_",$_POST["tags"]);
- M* Q6 i- H+ M4 E* W; r; N - $cate = $_POST["cate"];8 K3 R8 D% V; s K% K
- //print_r $tags;
/ S& ]5 c; g O; j! h c y: A - //
. @ G# n" r9 I2 D3 ]0 z2 n - //
- ?8 }2 N+ N0 |% Q - $wp = new WP();* `9 p* L6 [) V ^* x$ I! h
- $wp->main();
2 Z3 i, L' A ]2 f4 e6 C - //8 E3 `, s- `$ R. n
- $my_post = array();+ N4 L- ]% f1 }: t, l e
- $my_post['post_title'] = $title;
s, u$ Q7 k7 ]' w8 T - $my_post['post_content'] = $content;8 T4 i+ ~- s: f7 e
- $my_post['post_status'] = 'publish';
: H7 u: |; V' z* q* n; R - $my_post['post_author'] = 1;
* \6 S/ Q6 k) P5 p/ M - $my_post['post_category'] = array($cate);
' v+ W8 }, m1 `# ?* `& C9 H& C - $my_post['tags_input'] = $tags;
- A# y" j) N3 H5 y! c+ }/ Q4 s {: ` - //$my_post['tags_input'] = array('tag1', 'tag2');
& n" D! f- W# Z6 [" o - //$my_post['post_status'] = 'future';8 F! Z5 \" J1 l1 G
- ////$my_post['post_date'] = '2010-07-04 16:20:03';2 h5 w* A3 `* Z' e a
8 f: [3 D# L V. w- // Insert the post into the database
& K. ~4 g0 ~8 B( Y) q6 x2 d - $ret = wp_insert_post( $my_post );
/ n6 n% e; z: P9 S& V3 u* u - echo $ret;1 g' U5 `: O- h/ a" B. }
- ?>5 d) ]( c: w6 |& h: @5 H6 l
复制代码 我自己写的
% N6 }/ P, Y6 e& f8 \插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|