|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑 / J& H7 f3 F8 V
. F. {5 s1 T% U* X
直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持7 n9 s% E3 U! D" M) j5 S+ Y M8 y
' s: Q8 i8 X1 D) c我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了
, b6 [5 C: a+ p+ N- ^3 B1 |6 s1 s. n ]
方便实用 简单 何乐而不用呢- . @: T! G' c% A5 f
- <?php
. c8 @& b0 d( L& T( h6 A: m - 4 N$ _( E& G4 \
- define( 'ABSPATH', dirname(__FILE__) . '/' );
/ x" Q) ^* u3 F0 P3 o - require_once( ABSPATH . 'wp-config.php' );$ o( ^. X' R& t3 X+ D: G0 I: l
- require_once( ABSPATH . 'wp-settings.php' );
5 f5 H3 O( s* S$ T7 u - require_once( ABSPATH . 'wp-includes/class-wp.php' );8 j9 E0 H2 C" o1 I
- require_once( ABSPATH . 'wp-includes/functions.php' );
+ @ L- F9 N6 f$ ?- ^& f b - require_once( ABSPATH . 'wp-includes/plugin.php' );
/ B6 q$ c0 Q/ m* n$ e) v0 ~ - $title = $_POST["title"];
6 F- `/ q4 H i7 G - $content = $_POST["content"];
! p E+ e2 u' J m% m# Z& D4 R4 r! c - $tags = explode("_",$_POST["tags"]);5 W) i7 h% {$ n9 e+ z9 S
- $cate = $_POST["cate"]; S0 l4 z4 W- l. d& i5 [
- //print_r $tags;! z! Y; i3 Q+ Y- m, B4 f
- //- ?- l- X$ ^( u! ]7 C( `
- // K& o/ J& N" ?/ t3 K4 K
- $wp = new WP();
' e( f9 O9 y) b3 Z4 |: `$ p - $wp->main();' b7 _9 Q/ S2 ~# ]2 T$ ~
- //4 `) ]; v9 J- ]$ i6 @
- $my_post = array();
. K3 ]0 o( \& q) v) E2 E - $my_post['post_title'] = $title;
) W$ [8 U% j! G' s4 D- e/ t - $my_post['post_content'] = $content;
: ?' `7 R) w, r# Q9 A - $my_post['post_status'] = 'publish';: U' \% @1 n4 w; r |5 L2 E! x
- $my_post['post_author'] = 1;
6 U6 T& [) w* B1 \3 ] - $my_post['post_category'] = array($cate);3 V8 y/ X2 E6 e2 P" k
- $my_post['tags_input'] = $tags; c7 M( A6 D/ |
- //$my_post['tags_input'] = array('tag1', 'tag2');& y# u" n# I" O2 @7 Z' V
- //$my_post['post_status'] = 'future';
! m- F# j5 _4 B& \- f - ////$my_post['post_date'] = '2010-07-04 16:20:03';: R, M$ g' [3 {
6 v! c+ W( n8 ^- // Insert the post into the database
+ ?3 \2 Q" T- S, B0 F - $ret = wp_insert_post( $my_post );
- m9 _ J3 I0 L - echo $ret;' Z# [- Z; g8 a% _ G) h, G
- ?>, |- F% B& k* N" y
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气
8 N! ~! ?2 T6 X5 c' W/ w- V- <?php5 X- }5 Q1 S" F! M) M% R+ G" Q( B- w) a
- define( 'ABSPATH', dirname(__FILE__) . '/' );$ g& h* A7 n+ y7 V' f% X
- require_once( ABSPATH . 'wp-config.php' );8 a7 J" v/ s7 _& v& @: l1 ]$ b3 n
- require_once( ABSPATH . 'wp-settings.php' );3 N* G# v+ U* @
- require_once( ABSPATH . 'wp-includes/class-wp.php' );
- d( v. _- q2 _3 G& E( @ - require_once( ABSPATH . 'wp-includes/functions.php' );
5 j% U, S# E" c+ t; } - require_once( ABSPATH . 'wp-includes/plugin.php' );
% N6 d. e' c% e( H4 D' D) ^ - $pic_name = $_POST["pic_name"];% P+ M" n- \' c8 H& f; e! L
- $pic = $_POST["pic"];! M+ X# M* t% C( @) p+ ~
- if($pic != "" && $pic_name != "")
6 f$ P8 O. t# V% F1 g7 @ - {
; R% M. ^ v( h9 t+ d - $path = "images/";
" i4 _; U' c% \5 f - $pic_name = $path.$pic_name;- F3 l8 y1 C1 s* m8 A5 K' t
- echo $pic_name;- `: z' D' |/ }8 Q, M2 D9 }6 a
- echo "||";
M/ a' U, L7 z6 y) x - error_reporting(0);4 q1 ]' J/ B( f# |
- mkdir("images");
* ^! {& t3 b# Z - $handle = fopen($pic_name, 'w');' E+ t' S) E+ N; a7 g
- echo fwrite($handle,file_get_contents($pic));9 t2 B2 h0 m) l2 ]. T5 E8 o4 h
- }
* ^1 s% O1 ]# M8 Y \9 i - 7 ]( b1 j9 V" V" J% ]
- $title = $_POST["title"];% v: p% u \ `/ F- t6 I! [
- $content = $_POST["content"];" K A3 y, v% n/ N
- $tags = explode("_",$_POST["tags"]);6 i1 D3 J3 |8 H1 j9 A) @9 t" E
- $cate = $_POST["cate"];
, ]6 Q4 p+ Y( Z% c" C - //print_r $tags;
$ t, Y" Y+ U' }+ c: L) R* Y+ S# Z - //
' ^( d# o7 p6 F - // r% S9 Z+ q! ~
- $wp = new WP();
- [1 d* p6 W3 T. E - $wp->main();
$ f |0 z% A, h* ]2 B, P - //+ {- [, W' p& Q ^# T# T
- $my_post = array();
5 d% \7 R$ B$ N - $my_post['post_title'] = $title;
+ ]8 t j9 B- V/ z* ? e# B" e - $my_post['post_content'] = $content;4 ?2 R: h) A0 X
- $my_post['post_status'] = 'publish';
* n. ^# h0 ` P! F& x1 c - $my_post['post_author'] = 1;; {: b% u6 `( [
- $my_post['post_category'] = array($cate);
& d6 D+ c5 b4 o7 b4 O6 a - $my_post['tags_input'] = $tags;3 A8 o( i% q) |% |) I9 A0 e
- //$my_post['tags_input'] = array('tag1', 'tag2');: H" f7 `! R( g- T$ J% v' q* ^! @3 p0 H
- //$my_post['post_status'] = 'future';7 A0 c' p: p; a3 n7 W
- ////$my_post['post_date'] = '2010-07-04 16:20:03';8 o( \4 ~4 K7 S x
/ r" H3 D* K- w& I& J" E/ o- // Insert the post into the database
: {; \8 e+ q6 {: ` - $ret = wp_insert_post( $my_post );; }' U' N# z7 r0 J7 ]% O% z
- echo $ret; f9 a- h0 ~- ~0 O# ]6 P
- ?>
5 E8 d* x5 M' U
复制代码 我自己写的
, ^* b' K7 O) ], B9 A插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|