|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑
# P* o7 q; W- u- E; u' m
# O: [: V" W- _9 K4 |直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持, t+ q3 N2 b* S% B2 e$ X% x2 U9 Q
8 \: t# E( E G" Q/ N; J我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了
' p" d. ~# e; M7 M+ U& Y" U) Q* x
方便实用 简单 何乐而不用呢- 4 }. V3 N* v1 F
- <?php
. L/ _& {4 E' [3 q: z - 9 x% _- H+ r$ T# E& M; ^
- define( 'ABSPATH', dirname(__FILE__) . '/' );' l' ~, O- y7 T: }5 w' p% I. }
- require_once( ABSPATH . 'wp-config.php' );
7 g1 F" b- Y6 l) T9 Z! X' b - require_once( ABSPATH . 'wp-settings.php' );
" Y* l+ H3 `/ C7 s1 e - require_once( ABSPATH . 'wp-includes/class-wp.php' );3 f1 z; l# x- x* L# C3 u: c
- require_once( ABSPATH . 'wp-includes/functions.php' );* r4 G4 X; f5 B. h. Z9 c) h0 P9 B
- require_once( ABSPATH . 'wp-includes/plugin.php' );$ u3 k% L1 g! {6 x9 u8 I3 L
- $title = $_POST["title"];
. w) v% d2 K; X - $content = $_POST["content"];
3 y: p) a( h8 `1 x0 o; h& x - $tags = explode("_",$_POST["tags"]);. V* q; J2 W+ t; b' p e1 C7 R
- $cate = $_POST["cate"];1 j+ B( W, o/ J9 d- U6 I- N
- //print_r $tags;
6 M* C' A; }& a. S - //
5 D2 {) D( m/ `6 { - //! z( n8 N# c2 b1 u2 g, F) h' F
- $wp = new WP();8 q4 ?" G, Z3 L% u
- $wp->main();" H- B) Q+ x6 i( q4 @) q5 P G9 D- @
- //
" Y3 I; p' H- \* j. W. P - $my_post = array();
1 a6 i: q; M' F9 B - $my_post['post_title'] = $title;2 v' C8 w/ q, L$ h: W
- $my_post['post_content'] = $content;
1 G% o! ^- K# c# z0 G! |7 k) K: a - $my_post['post_status'] = 'publish';
# a7 T3 X+ P7 H2 i - $my_post['post_author'] = 1;
* J% u- W% }/ ^' k - $my_post['post_category'] = array($cate);
, k4 s: o" r% z8 l/ t1 P - $my_post['tags_input'] = $tags;( c6 `4 j: d5 `. p
- //$my_post['tags_input'] = array('tag1', 'tag2');8 s" q. O9 D) L" G4 e
- //$my_post['post_status'] = 'future';
. `; D' `' V% A9 Z; o* ? - ////$my_post['post_date'] = '2010-07-04 16:20:03';2 v8 r" |& G j
- ; H/ T' X- }" f) G% m5 |* g
- // Insert the post into the database
) M8 h3 }1 N( `2 {0 x9 | - $ret = wp_insert_post( $my_post );
; Q3 t0 ]+ T$ D s7 f& k - echo $ret; Q3 V- F9 {- b
- ?>
( |3 c" m/ k4 N
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气- 4 `# \4 }; V% W; c- n+ E
- <?php
" ] c1 W9 W2 T# j+ [/ b - define( 'ABSPATH', dirname(__FILE__) . '/' );
7 `1 i6 }' y( y8 t3 [ - require_once( ABSPATH . 'wp-config.php' );
& m& q" a- n% e, [& R - require_once( ABSPATH . 'wp-settings.php' );
) J2 w9 W0 ?+ r* j" k - require_once( ABSPATH . 'wp-includes/class-wp.php' );
3 H' O# `7 N$ C( Z9 k - require_once( ABSPATH . 'wp-includes/functions.php' );; o5 u% S9 b4 a
- require_once( ABSPATH . 'wp-includes/plugin.php' );
* \. v. q" G1 f6 Y& J4 h - $pic_name = $_POST["pic_name"];# ], v9 _& _; `# f$ I5 Q
- $pic = $_POST["pic"];
o$ x% z& j9 W- @9 w4 A* K& I - if($pic != "" && $pic_name != "")& r* X" D+ P/ E, {' F9 X
- {
# e! T# j, f) N - $path = "images/";& G2 E2 P6 q; R
- $pic_name = $path.$pic_name;
! \' M, a) v4 C' }# m4 L/ q - echo $pic_name;
- m! L) A3 f; J - echo "||";
4 F2 }/ c& f; H3 E - error_reporting(0);
& Y0 m, ]$ e0 k) X - mkdir("images");8 D; F/ J. \$ a1 T. {
- $handle = fopen($pic_name, 'w');
/ O, P5 l' O2 y! X3 ]: O, }: S6 R! O - echo fwrite($handle,file_get_contents($pic)); Q9 _- r( [$ |, i5 b% Z5 L
- }
$ F2 r, V4 k, N$ q
* b9 Q- T( \% x. c( j! f: u- $title = $_POST["title"];6 X/ F2 ^# e: Y. b4 g
- $content = $_POST["content"];: r/ [, h# ?$ x& s
- $tags = explode("_",$_POST["tags"]);
0 m0 F- j& _1 V9 }# d; c! R# a, ?: f - $cate = $_POST["cate"];
' n) `3 s; d2 b/ S - //print_r $tags;
3 a! v7 U8 c ~3 e& O0 R - //* _, B$ H: u: p! M; u
- //
, z9 C3 t+ E- F2 p - $wp = new WP();
/ d$ h$ w9 n- {; y* t - $wp->main();% b- k% h" \: v
- //! ^% r' v% N+ Q7 G
- $my_post = array();
2 z7 a+ g4 r8 w% \( r1 u - $my_post['post_title'] = $title;
7 F t: U$ X+ T+ P' b. k" g) S" a - $my_post['post_content'] = $content;
9 j: q: g$ w: o - $my_post['post_status'] = 'publish';
2 U+ s" s! c& X0 \8 F - $my_post['post_author'] = 1;1 s% N# ~; A- O$ E2 G
- $my_post['post_category'] = array($cate);
7 W$ v% a4 }1 I( {4 n - $my_post['tags_input'] = $tags;5 Q% O( ^+ B" U0 K
- //$my_post['tags_input'] = array('tag1', 'tag2');
0 B3 L7 n! o2 c. C/ }3 ] - //$my_post['post_status'] = 'future';8 f0 Z8 r+ m5 q8 I0 x0 L+ T
- ////$my_post['post_date'] = '2010-07-04 16:20:03';5 C3 f6 W* s" c. x7 Q# {2 ~
- " I! W" [6 D& o" y0 K `
- // Insert the post into the database
2 n( h" M t( Y7 }2 x1 S: b# C- d - $ret = wp_insert_post( $my_post );4 Z* k T. Y# ?: \
- echo $ret;
- s* {1 b, _/ R7 ^! T3 T - ?>
8 S2 l: T% U! ]
复制代码 我自己写的
2 }4 w. z+ C. X7 r3 C/ \插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|