|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑
, o# K- Z9 [! M9 v: J8 k! M* c6 k9 Z4 J( i
直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持6 x) M( M) J# r: ? R
& U; n- v6 n) {- W我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了( m: O3 h& U% w
* _' x0 r5 G6 {8 n% V方便实用 简单 何乐而不用呢- 2 Y! o9 q0 k- _. H, x
- <?php
. u- g% @6 e9 O5 p- T - + c2 `$ o7 Q+ v2 r! `( a
- define( 'ABSPATH', dirname(__FILE__) . '/' );
, u9 U" T3 |- T6 {8 ^: N- g0 z - require_once( ABSPATH . 'wp-config.php' );
3 o$ L9 Q v" A7 U0 Q - require_once( ABSPATH . 'wp-settings.php' );
; [& t3 b3 T' |% {* o - require_once( ABSPATH . 'wp-includes/class-wp.php' );( a. `1 y6 E& @# R" z
- require_once( ABSPATH . 'wp-includes/functions.php' );* z; j. d3 B& [0 I5 P. w
- require_once( ABSPATH . 'wp-includes/plugin.php' );
% t( }; B" Z* I0 T) Y - $title = $_POST["title"];
. f6 D6 f/ v1 S. w - $content = $_POST["content"];+ ^+ U- N: S; `! a
- $tags = explode("_",$_POST["tags"]);
+ w% z- {/ S+ z6 c - $cate = $_POST["cate"];
0 O8 V4 L0 I" z; M, }! N* a - //print_r $tags;" n' ]+ _# @. v* A$ L E: D! N
- //, l2 X* O" i( W& p* R4 b' `
- // B' y5 @: o. X4 s3 a" C, p
- $wp = new WP();, M! Y7 u; s- q, k+ R
- $wp->main();
7 h3 O8 R; z3 e - //
) E' J/ F7 S) {, G1 n1 ]( o( S - $my_post = array();
' \+ X& c' @. Z& l( R4 g& c/ \% k - $my_post['post_title'] = $title;/ W' Z' G+ o0 t4 i9 _+ U
- $my_post['post_content'] = $content;
3 ?9 _0 v* f, d* h+ {! ~ - $my_post['post_status'] = 'publish';
) m0 y9 S' G' P/ B+ l4 ]# v - $my_post['post_author'] = 1;9 s* ~: O8 a4 |; v
- $my_post['post_category'] = array($cate);) f6 z2 \: h+ ]' z3 k
- $my_post['tags_input'] = $tags;9 j4 ?5 a7 r8 [5 I" D% w
- //$my_post['tags_input'] = array('tag1', 'tag2');5 g! r2 U* T* u! b1 F6 N
- //$my_post['post_status'] = 'future';
/ Q7 [; L5 i) u) L" \ - ////$my_post['post_date'] = '2010-07-04 16:20:03';7 Q3 y5 `+ C3 R4 l; N2 P. @+ j* p
- / W) y, x- K0 {: e
- // Insert the post into the database K' M: `4 i3 \: k H
- $ret = wp_insert_post( $my_post );+ ~ ~7 d. d, S* K
- echo $ret;
0 i; D. T7 i( r - ?>( l0 T9 y8 {+ a
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气
7 t- c, n+ o7 ]- <?php# Z+ ]: x0 w0 w9 Y8 l) M
- define( 'ABSPATH', dirname(__FILE__) . '/' );9 N$ ?6 J# x" O* V% Y9 U
- require_once( ABSPATH . 'wp-config.php' );; t |" h3 D; [) a9 w! K
- require_once( ABSPATH . 'wp-settings.php' );
- M% j) a) C) p: { - require_once( ABSPATH . 'wp-includes/class-wp.php' );/ ^3 N* s+ k! s' c) S
- require_once( ABSPATH . 'wp-includes/functions.php' );/ T9 H8 `/ d" D3 S# y% r: a- S
- require_once( ABSPATH . 'wp-includes/plugin.php' );# Z }" {, q3 P/ I
- $pic_name = $_POST["pic_name"];
* \; D# D4 i6 o% V2 W5 Z - $pic = $_POST["pic"];: R7 n1 ?) N a7 b5 ~) r
- if($pic != "" && $pic_name != ""). f* q. x; V4 T% l+ \
- {5 d1 n$ q& h5 F' q8 V9 h1 K
- $path = "images/";- P8 g* I6 s$ d4 i
- $pic_name = $path.$pic_name;( Z( f2 [6 g1 O& d
- echo $pic_name;0 \( h: C6 X* m- p
- echo "||";
0 t! y; p3 x, J( k! N/ z - error_reporting(0);& p# J0 u- B. e! l" D1 N
- mkdir("images");
; Y; d% J* B; L) z% X7 a2 e) u; S - $handle = fopen($pic_name, 'w');: O' u* m, W0 U! Y4 [ l) x
- echo fwrite($handle,file_get_contents($pic));% C, M( V6 r0 X& u
- }
* E ?$ j7 b& K9 l- N; a1 V) Z - % m6 P, R5 j6 s; b; j
- $title = $_POST["title"];- J9 c9 g" p s% g/ s
- $content = $_POST["content"];
' Q+ s) a0 J' r7 I - $tags = explode("_",$_POST["tags"]);
0 u5 P# r+ ?/ m: g5 i - $cate = $_POST["cate"];) R) Y7 o/ F. d
- //print_r $tags;" ?$ ]& N: G/ h7 O" p4 `, g" S$ ^
- //' U4 K: o6 t/ z3 R
- //+ U% ~: \* `. f, P
- $wp = new WP();
& l7 E& |, c0 x. m v - $wp->main();/ q/ \4 {% P' C- m |" C/ h
- //
" T; ~: {$ {, z; ~2 D! M! e - $my_post = array();% C7 f3 d* p/ w) l! z: J
- $my_post['post_title'] = $title;# Q5 l! }! @9 \) c" K0 @+ A: P
- $my_post['post_content'] = $content;: l+ W2 f+ C7 W8 H
- $my_post['post_status'] = 'publish';
0 K( n8 W/ d1 I) H - $my_post['post_author'] = 1;
~, F0 j+ r3 z0 ]# t5 z* ?0 L - $my_post['post_category'] = array($cate);
/ ]- Z3 A+ Z- Q, L/ f: I0 N! l% U* l7 ? - $my_post['tags_input'] = $tags;5 Z( R5 k) k1 g* E+ O
- //$my_post['tags_input'] = array('tag1', 'tag2');
9 |2 I# B9 ]# g' _3 I$ [ - //$my_post['post_status'] = 'future';! n# S, S# j0 t6 R8 o
- ////$my_post['post_date'] = '2010-07-04 16:20:03';
8 B2 ^$ G5 X1 P7 W$ i5 c3 r' I
. ]6 }( |7 z* v2 k3 A+ A- // Insert the post into the database
4 p/ @) P8 j+ t4 G \; l! |3 z - $ret = wp_insert_post( $my_post );
* R1 {! e5 T" v" y7 _ - echo $ret;# {9 M1 S- W9 H* T2 K+ p
- ?>
0 b) D& m& S7 i, l" Z" b" w1 i
复制代码 我自己写的% e" c9 U' A3 {6 H2 B) b# s& T
插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|