|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑 4 g+ A" G, u4 ~; ]+ A% Z
" S' Z- Y# j; e* V, P6 h直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持
: Q8 }3 E! o4 X" ` G* v) @, c3 J! M! R: o
我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了% n# ^4 \3 G( i. A8 o* K
/ A" b/ y/ h: Z3 i5 Q! e9 Y3 t( u
方便实用 简单 何乐而不用呢
! f* i, n+ g% f& e9 i2 b# C- <?php
5 X; d B8 u* F5 e; f% K
+ B: C$ w% p) u; A' C7 O1 l8 @- define( 'ABSPATH', dirname(__FILE__) . '/' );% _! c( G9 a! V
- require_once( ABSPATH . 'wp-config.php' );
; Q! ], O% |. n8 [: u/ u4 K* Q - require_once( ABSPATH . 'wp-settings.php' );
: B# T$ \% ~3 E6 R( q - require_once( ABSPATH . 'wp-includes/class-wp.php' );
$ G5 h R7 m. c8 r: x5 C* } - require_once( ABSPATH . 'wp-includes/functions.php' );9 l1 W; ]) \3 Q/ j H
- require_once( ABSPATH . 'wp-includes/plugin.php' );
; z' A' `- n, W' W/ _5 e* G - $title = $_POST["title"];" u9 f6 l* V. p( N' o
- $content = $_POST["content"];! q9 J- Q. [' z
- $tags = explode("_",$_POST["tags"]);3 q: u8 z% b1 p9 u: D Y
- $cate = $_POST["cate"];
1 n4 W0 g" V/ c9 k( b. B - //print_r $tags;4 e- i9 H5 k: Y& K1 v
- //
J6 p# ?5 N _ - //
F! U% O+ J+ a) u5 M- `$ P - $wp = new WP();
& {; R3 M3 O4 r) [ - $wp->main();( g S. |0 s9 T7 b8 }
- //# f1 Z" f, L! F! w/ g
- $my_post = array();
, G* i c8 Q9 i8 w - $my_post['post_title'] = $title;' y3 q, V; [ f6 b+ l; J R
- $my_post['post_content'] = $content;. K6 N: j; t3 ~6 r8 d- p
- $my_post['post_status'] = 'publish';
' @) I6 [) B; W" l - $my_post['post_author'] = 1;
* R" t1 [0 L; T: {' D) C" B - $my_post['post_category'] = array($cate);
$ H: s& u: V7 p. L% E; U/ H+ } - $my_post['tags_input'] = $tags;
6 F% Q% d% b v - //$my_post['tags_input'] = array('tag1', 'tag2');
: t) q- E" S* V$ r - //$my_post['post_status'] = 'future';+ m$ _2 k9 J0 {
- ////$my_post['post_date'] = '2010-07-04 16:20:03';0 n9 B# _& a5 c6 @9 ~! t
$ i) j" A. o/ }. D1 T- // Insert the post into the database
5 H" F6 `: y9 ~5 x u1 Z% m7 y! } - $ret = wp_insert_post( $my_post );, P) V5 y) C. k, i3 ]4 `1 `
- echo $ret;* s% X9 H3 L7 q. A
- ?>
0 E$ A9 r# M; G. p: ?
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气
8 [6 X, j0 R9 `; D" n9 m* T- <?php) X+ j6 ^/ D! P8 N* Q' W
- define( 'ABSPATH', dirname(__FILE__) . '/' );) Y1 Y2 C) G7 f6 `8 C
- require_once( ABSPATH . 'wp-config.php' );
$ t; |* g# n) f& l$ {* y W! v - require_once( ABSPATH . 'wp-settings.php' );
# j6 s+ k c. B: \0 [8 T8 {7 M - require_once( ABSPATH . 'wp-includes/class-wp.php' );
0 a7 ~. l0 E# o9 O: [, A6 r - require_once( ABSPATH . 'wp-includes/functions.php' );' u& `" T8 b: B+ t
- require_once( ABSPATH . 'wp-includes/plugin.php' );
4 P8 @7 k# v3 U+ C/ [! K; B* D1 w - $pic_name = $_POST["pic_name"];
9 A) i# C* I% ~7 T6 R& s B - $pic = $_POST["pic"];) R8 _7 H' t4 `- c, O& k: Q
- if($pic != "" && $pic_name != "")% h- t: m2 Y! D. N/ w
- {/ v, \0 |( @9 C) ] \) n' G/ S
- $path = "images/"; T$ ?( w2 j/ O. Q8 A, y/ {
- $pic_name = $path.$pic_name;
+ ?- g; R3 ~' ?# g9 T/ C" d; i8 E - echo $pic_name;
; q% b2 w2 b$ M! X - echo "||";6 e, r6 Q/ j p# d0 U& m S. Z3 W d
- error_reporting(0);' g4 X. U% D N$ `: Y+ \" @2 v
- mkdir("images");
7 B2 X( f$ _, L, q7 K - $handle = fopen($pic_name, 'w');1 }, q5 Q8 v* H- C0 M% t9 N) }# F
- echo fwrite($handle,file_get_contents($pic));
& f- u- F$ p: k. u7 g7 ~ - }7 Q7 X. O4 r, A3 y G4 F
5 r' f( k8 L. `- v% h( w- $title = $_POST["title"];
! N6 {/ L! e/ z' M0 G$ n/ X - $content = $_POST["content"];
4 M" _! a% p+ z U! S* p - $tags = explode("_",$_POST["tags"]);- A( w( h/ ~0 Q$ c
- $cate = $_POST["cate"];
1 n. y* g7 O! x2 O9 F' H( x - //print_r $tags;0 n6 s, ^1 v& Z0 P* |& v/ j
- //
' Q- L U/ b$ D; y+ z0 X5 } - //
8 U: o, H4 }* L% y2 Q" U. r/ i3 ?' E - $wp = new WP();
' U, K2 D+ `" J8 t) H - $wp->main();
9 z2 q$ l6 y" |! Z2 E, \ - //
9 ^* e% G# o. D' B: U8 U - $my_post = array();
( x! E" R% w% m+ a5 U" D* Q, P - $my_post['post_title'] = $title;
0 ?7 P; R( B' p; G - $my_post['post_content'] = $content;
0 y3 y- Q3 J; P9 @/ ^ - $my_post['post_status'] = 'publish';
* \4 b0 Z7 a/ E5 B* b1 v7 s3 G - $my_post['post_author'] = 1;2 V6 ~& q! }& v& A, h
- $my_post['post_category'] = array($cate);$ k( s( z D4 s+ ]: \8 v7 Q
- $my_post['tags_input'] = $tags;# H- T) `: | z3 h; `! ]
- //$my_post['tags_input'] = array('tag1', 'tag2');& G, |8 T5 J3 {0 ]2 X
- //$my_post['post_status'] = 'future';
) E6 ^2 X' x, P+ Z5 B$ p5 M! E! | - ////$my_post['post_date'] = '2010-07-04 16:20:03';
6 w8 O( e3 Z, T! X$ f: {) a - " n! \' U$ R4 ^1 ?2 w5 l4 w. Z6 i' M: V
- // Insert the post into the database
% M( \2 E& n" U) U! U - $ret = wp_insert_post( $my_post );
& u1 D6 I4 _' X i - echo $ret;
& L* [" w# u, O% |( J, n - ?>' w$ L" D7 j* k0 |4 A
复制代码 我自己写的+ V. b0 o% a( \! R& r* W* P
插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|