|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑 $ D2 t! k8 T( Q: {! O
0 G2 d1 E! w8 E0 y- z* m$ n. G' ~直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持
& g# \/ `. F* o5 Z4 ?# r6 e8 `- z( Y& {) ?- a
我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了
/ g2 F- y# _0 p. B" i: l8 \( T% a( x: L
方便实用 简单 何乐而不用呢- 7 U3 Z- X8 R/ z1 _" B0 ~# }
- <?php
; P! q4 M& I# q! n
+ C- d6 W- j. u0 A0 I- define( 'ABSPATH', dirname(__FILE__) . '/' );
' I( R }: e2 z1 B: ?# A - require_once( ABSPATH . 'wp-config.php' );
& R, n1 k7 ]' a - require_once( ABSPATH . 'wp-settings.php' );8 f, T2 S8 b- T! A1 s- V8 q) X# L
- require_once( ABSPATH . 'wp-includes/class-wp.php' );( y5 z) O; k- T
- require_once( ABSPATH . 'wp-includes/functions.php' );* J' B8 ?( j7 P h8 _' V
- require_once( ABSPATH . 'wp-includes/plugin.php' );6 K- t, {- l9 f' K, X
- $title = $_POST["title"];
0 J( q, |( n1 V$ y$ l' {0 K) D - $content = $_POST["content"]; y# ^* j. h: u# Y9 H E( J9 [
- $tags = explode("_",$_POST["tags"]);; {1 Z! g% @; `8 W; p
- $cate = $_POST["cate"];, `& ?" {3 K! t$ B) C
- //print_r $tags;! U! c+ X$ t# [, q9 L6 e3 Y
- //
6 h! B1 V0 P; O! S7 Z6 M1 u3 f - //
, P& ^- ^* K. D' k; t/ b - $wp = new WP();6 H( Z: [0 P* S. _; j
- $wp->main();, k8 l( ~8 s5 m6 j& z& D. a% V
- //
, w4 l/ g& p8 s- Z( N- w% p0 K0 K6 m - $my_post = array();. P: \) {0 L4 S/ P- x0 F
- $my_post['post_title'] = $title;8 i# A7 R4 p+ l
- $my_post['post_content'] = $content;
0 Y' H* h% g: L: t+ I+ d - $my_post['post_status'] = 'publish';
. z6 a' d4 Y! {# G" a - $my_post['post_author'] = 1;
* ~: Y5 |0 n8 Z( z - $my_post['post_category'] = array($cate);
0 G1 `7 b" E: ?! i; c5 g - $my_post['tags_input'] = $tags;
& ] j& n: c* |( q9 X3 C - //$my_post['tags_input'] = array('tag1', 'tag2');
5 `+ X2 [! _& \ s U: G: R - //$my_post['post_status'] = 'future';. q" N2 {' g- V: D" n& m
- ////$my_post['post_date'] = '2010-07-04 16:20:03';
; y5 }' `1 U1 u
5 X3 T7 b2 }3 }( y7 ^3 b- // Insert the post into the database" T& R1 C" y% r1 O
- $ret = wp_insert_post( $my_post );
1 K) @. Q8 [& d' s6 U( h% x' v - echo $ret;
- Q, X/ {; c+ W7 _* ~& i! }) x! B - ?>
) Z7 u9 ~6 F" C, a {1 [. @5 J
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气- . D, F6 L# O8 T1 R6 k
- <?php0 G& [) Z i0 w; i9 z* S
- define( 'ABSPATH', dirname(__FILE__) . '/' );! j0 b* M5 n: {
- require_once( ABSPATH . 'wp-config.php' );( E9 Z" Y8 D- u( o8 Q
- require_once( ABSPATH . 'wp-settings.php' );) f" z0 a5 N0 v4 x
- require_once( ABSPATH . 'wp-includes/class-wp.php' );+ E( G4 B- N2 U6 }$ t- X2 ^
- require_once( ABSPATH . 'wp-includes/functions.php' );
a% i4 z! n5 p8 f - require_once( ABSPATH . 'wp-includes/plugin.php' );
' p: u1 w$ ~. Y9 { - $pic_name = $_POST["pic_name"];' S* k! s3 [$ Z- `; T1 m
- $pic = $_POST["pic"];- B5 j% V: t/ N% R) s" J5 h
- if($pic != "" && $pic_name != "")) N3 H" w9 G3 i7 c
- {6 t; B; K! L7 S. H6 Z6 x
- $path = "images/";9 Z! \) C6 k- \+ L3 w
- $pic_name = $path.$pic_name;' j2 M8 E# q, l% d9 z) Q0 K
- echo $pic_name;. e- [& I. Y5 f6 n- ]; f
- echo "||";: f; d, h& V. I" W: s! J& ^
- error_reporting(0);
, k: E2 ~$ b9 w - mkdir("images");2 e% c" f1 C2 [/ |! T; f3 d
- $handle = fopen($pic_name, 'w');+ S5 o* V8 U% ^9 }6 P7 H
- echo fwrite($handle,file_get_contents($pic));
5 s* \6 o/ D3 n7 I - }
1 Q. \. Y4 F) H2 M; O- }* ^
0 b8 Z2 F' p7 _) ]/ E) `$ |- $title = $_POST["title"];- U/ M) w/ a1 Q1 x) W, W
- $content = $_POST["content"];
- d# {) i! T- |) {% g( g" I- |& @, p8 F$ @1 Z - $tags = explode("_",$_POST["tags"]);/ h" s" @7 ]) H, X
- $cate = $_POST["cate"];8 s( S/ K; s! g: f/ K8 V+ j2 e: |
- //print_r $tags;/ e9 C B0 Q% Q! z/ B; x
- //* c9 `/ ^, p( b1 n& [
- //' o) b% h0 G! m+ @$ T
- $wp = new WP();
& F2 L6 t' B& r' c' V6 d0 \ - $wp->main();
* r( }' r9 d: S& v - //
. r1 F0 y) m: @ ~! s- _: K - $my_post = array();
1 Y( C( R7 ~/ k( \1 p" G6 l - $my_post['post_title'] = $title;
$ A& s8 n9 C: ]+ u/ t+ f% d# s - $my_post['post_content'] = $content;
2 s$ t0 C% G/ W ~, p# @ - $my_post['post_status'] = 'publish'; y. p% t- _6 Z6 K
- $my_post['post_author'] = 1;, S6 T6 Q$ v- u" N: v/ o8 T7 K
- $my_post['post_category'] = array($cate);
. [. B- l1 c6 i6 } - $my_post['tags_input'] = $tags;
& e$ \! X0 c4 o. v+ `3 u - //$my_post['tags_input'] = array('tag1', 'tag2');, r9 ^% F6 D* a( N# H
- //$my_post['post_status'] = 'future';
# ^+ k! c' ^; x0 d - ////$my_post['post_date'] = '2010-07-04 16:20:03';" `9 G0 }- e+ `* ?7 f' `
- ( Z3 p; C! f7 s% R3 ?1 j
- // Insert the post into the database. ?8 \- ?2 x) o8 {- Q- s
- $ret = wp_insert_post( $my_post );
' R2 Y, s# w& f1 q3 W$ X" N W1 R - echo $ret;
5 f, }3 U7 |& N8 k& H0 z - ?>
2 D; @% r# g) C) W* S9 }
复制代码 我自己写的6 O* @+ J4 U; V+ a7 ?' T
插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|