|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑
( a* s) O1 K7 [ M( e; c% `
' ]5 z0 y! Z0 f# I, U0 j直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持$ Q# R3 `- D$ ?
5 t( y B* I. \( c; G( ]我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了4 U$ L3 j6 d0 i2 {, g# n( f+ C" e
j% z6 ]# q$ @6 i
方便实用 简单 何乐而不用呢
( o9 _, S7 A% s$ A* J1 J- <?php
$ C! @' T+ k# P, Z. e
# @8 \' Z* I) S6 V, E$ f- define( 'ABSPATH', dirname(__FILE__) . '/' );. P' K4 D$ \! S4 e
- require_once( ABSPATH . 'wp-config.php' );- t% Z' f; u: ?
- require_once( ABSPATH . 'wp-settings.php' );
8 T8 b/ |4 C& n3 Z/ x/ S) Z# X& o: B - require_once( ABSPATH . 'wp-includes/class-wp.php' );7 J+ [" L, j# o3 r% y( K
- require_once( ABSPATH . 'wp-includes/functions.php' );% z1 V( p5 P8 b* [! O
- require_once( ABSPATH . 'wp-includes/plugin.php' );
1 f0 e. K5 @/ E q: v( K7 | - $title = $_POST["title"];
' {; A* [5 K( ]2 k9 G! h - $content = $_POST["content"];1 q* \+ V+ y7 o9 l7 q
- $tags = explode("_",$_POST["tags"]);
/ e9 M: ]; v# r1 z- w1 [ - $cate = $_POST["cate"];7 [- o4 f" a6 c5 X
- //print_r $tags;
; v) B# Q$ G# l. s - //# @+ \4 T9 A0 R9 L- l; I. o6 q# d) m2 m
- //3 p/ F3 N3 p) u% j
- $wp = new WP();
+ l% o8 C. @1 y( F/ L! A) j+ u$ x - $wp->main();
* p# L, K3 M0 i - //% r5 F7 e. Z; P, y; I0 K; o0 A
- $my_post = array();
8 f0 W2 N9 O$ h$ C# s% F/ G - $my_post['post_title'] = $title;
8 c4 a7 Z& Z7 {2 O, X+ e5 o6 V - $my_post['post_content'] = $content;
& ?6 F+ I+ u- Z2 n) [$ g. E' V$ H - $my_post['post_status'] = 'publish';
7 `6 X& @1 o( U6 e - $my_post['post_author'] = 1;
" `) z% N2 v$ ?5 I7 P& |4 B - $my_post['post_category'] = array($cate);
. V& y* l3 A' D$ k- x. p - $my_post['tags_input'] = $tags;
! O) Q1 N, V* s: J4 U0 j - //$my_post['tags_input'] = array('tag1', 'tag2');, J% Y5 b6 @9 D1 G8 n3 k* z% q1 @
- //$my_post['post_status'] = 'future';
- c/ S: ^0 d6 _9 U- k+ l - ////$my_post['post_date'] = '2010-07-04 16:20:03';
& P2 J: _. ^2 }; T; w+ j+ i0 a* ] - 4 R# b A3 e. E3 C& e
- // Insert the post into the database f0 T1 K* _' P3 O4 E) m- `% T3 F
- $ret = wp_insert_post( $my_post );
% w3 e8 V9 h4 y# F6 i - echo $ret;1 z4 d7 Y/ t. Q. o
- ?>
1 A, y9 [0 C3 w# U: m/ R# G
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气- & k" J3 H2 b4 F7 ~0 _" e( n
- <?php
6 p: z& t* T3 g - define( 'ABSPATH', dirname(__FILE__) . '/' );: k+ A# ?+ |1 [+ Z+ J5 S
- require_once( ABSPATH . 'wp-config.php' );
3 L2 r1 m p7 u* u - require_once( ABSPATH . 'wp-settings.php' );5 h% C: g4 v7 P4 Q
- require_once( ABSPATH . 'wp-includes/class-wp.php' );5 I% Y% O7 [$ e2 d6 R( }
- require_once( ABSPATH . 'wp-includes/functions.php' );! c7 [7 e7 W+ W7 u( v+ [
- require_once( ABSPATH . 'wp-includes/plugin.php' );0 B0 p7 |. L: M4 c
- $pic_name = $_POST["pic_name"];0 e1 l/ A# p. m x- b5 u! k! w) X$ |
- $pic = $_POST["pic"];- P4 [, R5 J) ]' S* e8 Q' b
- if($pic != "" && $pic_name != "")0 k$ D( i; |' L) O- D" ]
- {
2 \- U+ ]8 ~/ Z+ z0 o* ~$ y+ m' d9 b - $path = "images/";8 [- h8 m6 x7 \2 W' A% Y5 P
- $pic_name = $path.$pic_name;' X# J3 z+ s' a4 [0 K. I) W
- echo $pic_name;
2 e$ `5 S! f- [( x6 @2 z9 f: g - echo "||";) z H& T5 }6 i% v$ `2 }
- error_reporting(0);
8 A: J6 l/ F$ o v# b5 `, q - mkdir("images");3 b, g9 | m: U( K6 y" }$ N
- $handle = fopen($pic_name, 'w');$ j1 M! h5 a9 O% ?/ s
- echo fwrite($handle,file_get_contents($pic));
: `" w: \6 l: q( E' X! S! o* ~7 K - }4 w, [ M$ z5 P3 f2 ~8 e
1 m6 w: _0 Q- P& k- $title = $_POST["title"];' I+ ~, E1 r- j8 f/ f6 v
- $content = $_POST["content"];
2 r6 e4 z) {; f8 G- X2 u9 ` k - $tags = explode("_",$_POST["tags"]);+ `3 f8 Y2 v: @
- $cate = $_POST["cate"];
* T4 ]+ D0 I* B" |( f5 K) T6 F - //print_r $tags;0 M% h o3 ` S2 }! a
- //( f1 s4 P0 l8 ]& ~. K" C& L# Q
- //$ t0 W& b; r& Z4 O }: g
- $wp = new WP();
1 j* C1 f3 Z: D) t f - $wp->main(); \: P4 D' k4 ?& L8 N6 B+ T( V
- /// g$ |$ j$ E. \/ k6 _) n7 A" p1 {
- $my_post = array();# ~7 y, Z! p/ F* k5 [# _: j
- $my_post['post_title'] = $title;
# u4 h6 g: n( \( ~$ S7 Y - $my_post['post_content'] = $content;
0 J! G/ v8 A& b) ]3 b+ Z) G, k - $my_post['post_status'] = 'publish';
$ z$ E+ w/ S/ C' O& P' b/ f: T$ a" p - $my_post['post_author'] = 1;
$ g* W3 c( C9 Q5 S7 N - $my_post['post_category'] = array($cate);
4 ^( q7 d3 A7 Z t& J* C0 D( J - $my_post['tags_input'] = $tags;1 Q4 N) M/ n6 s& V* c6 e5 Y
- //$my_post['tags_input'] = array('tag1', 'tag2');
) h! v" `" q0 j; g3 D - //$my_post['post_status'] = 'future';. }, J/ c; i! n/ v. c
- ////$my_post['post_date'] = '2010-07-04 16:20:03';7 N5 T( A8 K( \9 V) x9 g2 t* ^
* g& n; ~4 w& f) k2 r# u- // Insert the post into the database* w6 C0 _5 b0 l9 w! g% P
- $ret = wp_insert_post( $my_post );
0 B- N; B4 a: V# `1 g2 n - echo $ret;$ f7 t3 f9 x& Y+ ]$ {( W% [7 l
- ?>3 \( \9 r/ }: R. X2 u9 c1 ?. q( `% m
复制代码 我自己写的$ N0 z U: Q* t* `7 t5 k( r/ p3 {: f
插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|