|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑 3 a3 q' M1 B) N
4 M2 x/ P& m5 B) k+ B. ~' t
直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持
! i9 v! K" p7 i5 B- h! v. e H+ d# D1 Y% l. }: o3 C% b- A
我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了' F/ D/ d% h1 C
$ a4 Y) }& e( v( b
方便实用 简单 何乐而不用呢- $ C i6 p' L: [, _
- <?php
7 T9 E( R3 j$ J" i( ^
4 C% O% J8 j" Q; r, P- define( 'ABSPATH', dirname(__FILE__) . '/' );3 Q/ Y; U! U) J" S
- require_once( ABSPATH . 'wp-config.php' );# @# [" B8 I' K
- require_once( ABSPATH . 'wp-settings.php' );+ A3 h2 e6 D$ j! r
- require_once( ABSPATH . 'wp-includes/class-wp.php' );
7 `: ~6 g4 Z3 p. F6 d9 L2 o# i - require_once( ABSPATH . 'wp-includes/functions.php' );( D& |" b/ Q) X8 O: V0 a
- require_once( ABSPATH . 'wp-includes/plugin.php' );
# V: S' s! Y' l$ i: a9 s& z1 I - $title = $_POST["title"];' p) P, V* K' ~2 @ I9 S4 i
- $content = $_POST["content"];
3 a8 ^8 m6 a( ~* j: I - $tags = explode("_",$_POST["tags"]);2 i* @9 D- x% `" q+ X
- $cate = $_POST["cate"];
4 Y E9 m6 n/ G) H0 s4 r - //print_r $tags;
6 |. t8 |- a" e6 n0 z2 R - //# a9 ]3 e. W7 w: P4 M; \- B
- //
* L# v6 Q- m' O$ c# {5 ` - $wp = new WP();0 g7 }; t) j E- X" b) @
- $wp->main();$ y% [: d( e( V/ x& ]( t( R) K
- //
1 k$ j& s2 @: T% r* E) U - $my_post = array();
/ y+ W% T1 n7 [7 Z+ S6 z - $my_post['post_title'] = $title;
& ] C4 d- @+ h$ i - $my_post['post_content'] = $content;4 I( ~9 S4 {) n: Q; J: d+ c
- $my_post['post_status'] = 'publish';
/ @ i( Y% o* t H7 A! C/ ~( c - $my_post['post_author'] = 1;- i) W2 p0 j) o! G
- $my_post['post_category'] = array($cate);+ W! u, s8 \0 L8 R, R
- $my_post['tags_input'] = $tags;
4 ~; y( ]5 S7 y; _9 r3 t - //$my_post['tags_input'] = array('tag1', 'tag2');
# C% u8 U1 p6 O; a. H2 Q% y - //$my_post['post_status'] = 'future';/ N) F& K6 R8 |: T: f+ s( n
- ////$my_post['post_date'] = '2010-07-04 16:20:03';
" A7 a. T& L# e - 9 J) B" x3 O% s2 f) ^1 v
- // Insert the post into the database; l, j7 }5 h$ ]$ V, [
- $ret = wp_insert_post( $my_post );
a) g: N; z4 E8 O - echo $ret;. D) Z6 p+ U4 a1 G8 {0 ~- K
- ?>
- R! R& ?1 b8 _8 k/ r! x/ C+ ~
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气- # K5 X Z4 _4 d; l
- <?php0 k- k: f: A9 w+ {& Y9 D5 S
- define( 'ABSPATH', dirname(__FILE__) . '/' );# e, ]1 i2 |2 N2 {2 n1 `3 u
- require_once( ABSPATH . 'wp-config.php' );: O4 R- u% p6 |2 }1 v1 a
- require_once( ABSPATH . 'wp-settings.php' );
0 |: m& g, J0 b% K# n9 U9 h - require_once( ABSPATH . 'wp-includes/class-wp.php' );
: M0 K; c6 E, F; l' a8 {; E - require_once( ABSPATH . 'wp-includes/functions.php' );0 f+ ]; U% {' ]# ^# a* b& I
- require_once( ABSPATH . 'wp-includes/plugin.php' );
% d8 |2 g/ y! ^% w - $pic_name = $_POST["pic_name"];
' d! K+ Q- @' ]( V - $pic = $_POST["pic"];) Z5 @$ n7 i9 i( `( W& P
- if($pic != "" && $pic_name != "")( z2 L7 H0 n% s
- {* a/ K3 U; c7 p4 ?
- $path = "images/";' r. m4 o g( } G) J, _, p
- $pic_name = $path.$pic_name;
/ P: c( c$ x& `3 x - echo $pic_name;
, Q' S5 ~" E( o# I' \% Y - echo "||";
" d M3 o: m9 ~0 w, C5 t0 j. I# W - error_reporting(0);
) A/ k, K2 g1 m$ L1 w. H - mkdir("images");5 D* p9 h d8 k8 o; v
- $handle = fopen($pic_name, 'w');
8 V4 B/ ^9 T% u6 b* o2 R" e3 H4 ~ - echo fwrite($handle,file_get_contents($pic));% s" `* _ t! T. Y" b* y
- }
% S+ Y) o+ n/ f6 S
U4 z; h/ E! b& R6 h: J- $title = $_POST["title"];0 C; p2 n* t% {7 x" n
- $content = $_POST["content"];
/ b2 d) F3 s( ]7 p - $tags = explode("_",$_POST["tags"]);
& h- ]5 C* [ \+ X7 f. k, _ - $cate = $_POST["cate"];
; ~. d8 U6 u: @$ O8 x9 s8 w - //print_r $tags;4 G% _: a* q0 U, {
- //
- H* ~4 Q1 U6 j - //
/ y# E* o9 R6 j& {$ r; _4 g - $wp = new WP();
7 f+ y# O: g' p# x - $wp->main();
% a: n* K1 r9 n7 ^3 @" ]% B: D" t& m - //
! H0 B+ ~5 F. A" w+ s - $my_post = array();
$ z8 f% w0 s2 |) I/ B' @: i - $my_post['post_title'] = $title;
/ G7 [: d( l6 J" h5 P) e" a) a6 m - $my_post['post_content'] = $content;
% ^/ y0 @( o c) s - $my_post['post_status'] = 'publish';$ @/ {: T# A3 T7 ]5 B& w
- $my_post['post_author'] = 1;. J$ ^, [8 s" n* E3 ]8 s5 A
- $my_post['post_category'] = array($cate);
9 F+ x- F# G/ l - $my_post['tags_input'] = $tags;8 t3 s: K3 Y3 b% @" o; |( w0 K
- //$my_post['tags_input'] = array('tag1', 'tag2');
/ T3 ?6 k4 I( D/ Y! v- e1 B - //$my_post['post_status'] = 'future';
7 C' `: T5 q5 V& ?% x - ////$my_post['post_date'] = '2010-07-04 16:20:03';
5 T" u* v0 K9 g
/ D: v" _; A' E- T; g. K) x- // Insert the post into the database3 ~4 B. x! N+ m- d
- $ret = wp_insert_post( $my_post );+ g8 n5 ]% r5 x+ l4 i1 t
- echo $ret;
* W7 _( i9 R/ ?: Y. h, \ - ?>
# i0 D( g! N- O
复制代码 我自己写的, {! I4 X% D7 c2 R3 c* U& ]& m$ |
插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|