|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑
- P0 v3 R# Z6 ~" q( C+ V( L% a/ X+ o- D! k% o2 x
直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持* u8 g( N1 K7 c% w8 Z& P+ A
6 I& x* X6 v( `( b# G5 [0 V
我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了' F1 E/ B' v* W O* S; C& t% G5 K, \5 y1 p
$ A, s9 I n2 K# O( p" \0 k
方便实用 简单 何乐而不用呢
; {7 Q. l- N! [' S' @# C, R0 G- <?php
0 D. D3 t$ f7 d - D+ j& `% q4 [; {% `
- define( 'ABSPATH', dirname(__FILE__) . '/' );
' n0 d& h$ L' p5 X: [' g( j - require_once( ABSPATH . 'wp-config.php' );' B, {" F2 h) Z
- require_once( ABSPATH . 'wp-settings.php' );2 N" O2 ?3 V8 W
- require_once( ABSPATH . 'wp-includes/class-wp.php' );
. T# Z. P/ N- b3 x F5 T2 ^ - require_once( ABSPATH . 'wp-includes/functions.php' );
4 M5 i6 N- b/ X+ @" f6 N7 I2 K9 i; K - require_once( ABSPATH . 'wp-includes/plugin.php' );) F. X. O; r) Q+ M, R/ r' _
- $title = $_POST["title"];7 @, B6 k4 J$ H1 W6 H
- $content = $_POST["content"];
5 j: z e' S2 U" X" m% K - $tags = explode("_",$_POST["tags"]);
" V1 M# K3 S/ [, J - $cate = $_POST["cate"];
; m' M- l# t/ I8 b+ [9 y9 t6 T/ [ - //print_r $tags;, B3 {# @$ { H; \. y/ \
- //
+ b9 [8 Y$ x J) E) w$ i - //9 @' }& \ R5 f* }( V0 r" F
- $wp = new WP();
2 H( U/ X/ _' K* o - $wp->main();8 N, v! q% k: \# ?
- //
6 Z5 _2 ]0 Z3 f1 v! Q( T - $my_post = array();
4 N) h: w3 X4 j+ f3 |( | - $my_post['post_title'] = $title;) b' a( F9 V4 x; L/ ^3 W
- $my_post['post_content'] = $content;
l; g+ l! m( ~ z" U# ]2 l/ c$ e - $my_post['post_status'] = 'publish';
* q8 K0 a4 I9 n - $my_post['post_author'] = 1;/ ?7 C2 v/ Z! \( L9 ]4 O6 s P
- $my_post['post_category'] = array($cate);
) B4 z5 C) n5 L4 d+ k6 z* L$ j - $my_post['tags_input'] = $tags;
# ` @* q t4 q! `3 k4 s - //$my_post['tags_input'] = array('tag1', 'tag2');8 x, [; [3 U, M4 y( r) e
- //$my_post['post_status'] = 'future';9 f% x0 d& o0 p* ^, @* q7 ~" t8 W
- ////$my_post['post_date'] = '2010-07-04 16:20:03';3 H0 v4 B# e; W# c3 U- M
. s2 q3 l) F8 }1 b- // Insert the post into the database5 `7 k/ W4 E# R$ {- w
- $ret = wp_insert_post( $my_post );4 L! ^ }; D$ j2 v0 }$ g% [
- echo $ret;5 u' I0 s5 A. k
- ?>) L, |3 V/ I/ C3 u2 t
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气- 6 B! Z7 l# ~7 E+ f9 w: Q
- <?php
. x( @4 q! K6 e! y) I2 B$ ^% K2 z, n; } - define( 'ABSPATH', dirname(__FILE__) . '/' );
' ?7 x" N- [# }% l. K7 h$ l - require_once( ABSPATH . 'wp-config.php' );
' ]! D' O" \& g9 x5 Q - require_once( ABSPATH . 'wp-settings.php' );4 V: X+ _$ r2 L& B' e/ f
- require_once( ABSPATH . 'wp-includes/class-wp.php' );: _5 X9 q2 a1 }7 Z5 N
- require_once( ABSPATH . 'wp-includes/functions.php' );
) |; K6 [ m6 U6 a9 C - require_once( ABSPATH . 'wp-includes/plugin.php' );; t, d4 u( j, S5 X! f% g! r5 f
- $pic_name = $_POST["pic_name"];) ~/ x& K3 |' h6 Q
- $pic = $_POST["pic"]; P: k. O. l) K% H1 _! W7 _
- if($pic != "" && $pic_name != "")0 t* K4 I3 ^% f- L
- {4 N$ n& `1 F7 |3 a# }0 r
- $path = "images/";* D' D7 Y( f' k/ P
- $pic_name = $path.$pic_name;! R; b W( D' Z* Y% l R
- echo $pic_name;+ \4 g3 g; {. P; O
- echo "||";
! p6 }; Y9 e# F9 x1 w3 E; X& E - error_reporting(0);
5 E9 |% ` L. A7 k& U% R9 c( P - mkdir("images");2 _* E0 q$ N" ?; ^: ?. J
- $handle = fopen($pic_name, 'w');
, n. o- f( k! F/ z; b g# T - echo fwrite($handle,file_get_contents($pic));
0 G) V; h+ u4 M- N% C& Y% P: F( D - }) W! T5 ]" L) M7 e
- - T# r7 d7 q# Y" ~
- $title = $_POST["title"];) y W* e0 {; k
- $content = $_POST["content"];
5 G1 ?' f' K9 } V& ]1 \ - $tags = explode("_",$_POST["tags"]);0 l/ {8 p- t4 N& U
- $cate = $_POST["cate"];
V3 m6 M6 v# l' ^- C - //print_r $tags;6 P. A. m, S7 t, x! G5 Y6 V' K
- //( s! s/ H( c2 @) |: m6 ?* }
- //
: l: F8 x( {/ i- v9 N. d1 D - $wp = new WP();: Y& [1 w) b# s0 {/ n
- $wp->main();+ a( G7 |% x9 k- Z
- //
5 C: t7 D2 K" {1 _* K0 e - $my_post = array();
2 U- V. J" W: z& D - $my_post['post_title'] = $title;; s j |0 h" K0 F
- $my_post['post_content'] = $content; n& c7 I" @8 C; Y5 B4 }6 `
- $my_post['post_status'] = 'publish';
& W2 L+ j: U+ I - $my_post['post_author'] = 1;
2 t2 m/ q4 }! i* p( X8 p' o - $my_post['post_category'] = array($cate);
" [, a/ A! g1 i0 u! u4 b - $my_post['tags_input'] = $tags;" c' \- Y1 [. x1 F5 Y, r
- //$my_post['tags_input'] = array('tag1', 'tag2');# v6 a5 }3 @6 d
- //$my_post['post_status'] = 'future';- d+ T7 u( f- Y" W6 O) `
- ////$my_post['post_date'] = '2010-07-04 16:20:03';
; l/ C' r9 {6 {0 |4 t: G! h" J
: |0 }0 }" O. a. q- // Insert the post into the database
1 `* l' h/ E) Z, v - $ret = wp_insert_post( $my_post );; k: d, ]( s8 @( i7 X1 z( J m
- echo $ret;# y8 n5 B, y+ u7 M. c
- ?>
$ M! s4 _# D! {( X
复制代码 我自己写的* C. m; a ]9 ]# y& {0 E! i: n8 @
插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|