|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑
+ H% a* y! \' q8 H& M% y( f2 X- x0 d2 _( a8 E5 X2 \$ j
直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持% P& u& @* I7 \9 d0 X7 j9 p5 R
& O$ V6 ?0 q( f3 C5 l% W/ `9 e
我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了
" A9 F9 l0 m% d! n" [ W( U! d! c$ I8 ` _! U6 E4 v
方便实用 简单 何乐而不用呢
2 G; [9 W7 o% K- <?php1 i# j. `; w2 k$ r3 d. ~6 B
- * P! a7 s* P. o
- define( 'ABSPATH', dirname(__FILE__) . '/' );
4 }" s2 P8 h: t5 ?* `( m& E1 ]+ W - require_once( ABSPATH . 'wp-config.php' );# y( n8 n1 Y+ v1 e+ t
- require_once( ABSPATH . 'wp-settings.php' );5 m& O" r) W b, s! {) Z
- require_once( ABSPATH . 'wp-includes/class-wp.php' ); {+ t9 P0 f! Z+ Z# A5 r( a
- require_once( ABSPATH . 'wp-includes/functions.php' );) N! N6 `+ K* M) m* R. R6 @
- require_once( ABSPATH . 'wp-includes/plugin.php' );
( w% t* @+ f: ` W h - $title = $_POST["title"];
# L' A( m' @4 J. a8 ^' U2 Q5 ] - $content = $_POST["content"];& W% A8 x( B) W) n0 G
- $tags = explode("_",$_POST["tags"]);, t$ i1 b" Z0 {& }6 |8 j6 H- j" N
- $cate = $_POST["cate"];: \; C H$ P2 \3 ^
- //print_r $tags;
1 h3 h* ]/ V8 P! s1 E+ _ - //
) V1 s1 y- f/ p9 Y1 ~9 ]" K# j - //
% n4 R- }8 ~5 j) C1 }: `) L - $wp = new WP();* r! w1 U# M. o
- $wp->main();
, o! N8 y; X( l0 z( a6 Q# p - //
8 X5 n1 T0 Y* d3 Q" |; r2 \: T& E" S - $my_post = array();
2 F- o$ n1 e* c0 ~# D* z* }9 X# L - $my_post['post_title'] = $title;0 u: M8 k; j) |0 h- W
- $my_post['post_content'] = $content;
. B1 P i1 o6 ]% n7 E# q - $my_post['post_status'] = 'publish';
, d0 m0 j8 u; f1 S) d/ j* { n - $my_post['post_author'] = 1;& ?' M2 k, I3 U9 G5 L
- $my_post['post_category'] = array($cate);
7 C: z- V1 a+ | - $my_post['tags_input'] = $tags;
F ]0 o4 f4 m - //$my_post['tags_input'] = array('tag1', 'tag2');
" C9 z( F3 _0 I. ?! V: ^ - //$my_post['post_status'] = 'future';
4 X1 m8 x( z- ~& ^ - ////$my_post['post_date'] = '2010-07-04 16:20:03';4 m7 r3 D: r, o$ T% X( s
& C! ^4 ]9 z: b0 y- ]- // Insert the post into the database/ Y, k W3 w0 I
- $ret = wp_insert_post( $my_post );' q7 G V/ v# b" z/ y
- echo $ret;
8 a+ \1 i7 h$ R' V - ?>
5 S4 J* S. h7 ]" B2 P
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气
1 \. F' Z0 h, e& E5 E- <?php
! r9 C/ f: i6 l+ x! Q6 j' t: ^ - define( 'ABSPATH', dirname(__FILE__) . '/' );
+ j% l6 r. U' [8 c$ z - require_once( ABSPATH . 'wp-config.php' );7 Q% k; H$ S/ S; G
- require_once( ABSPATH . 'wp-settings.php' );
& O( G* z& {- W6 W - require_once( ABSPATH . 'wp-includes/class-wp.php' );- P& t6 B1 A; t$ K
- require_once( ABSPATH . 'wp-includes/functions.php' );
' g( v( |: l2 N' r - require_once( ABSPATH . 'wp-includes/plugin.php' );' ~$ G+ f. e- R0 p: {' J
- $pic_name = $_POST["pic_name"];
3 A7 x* K4 q. x- v - $pic = $_POST["pic"];5 i, C$ @9 r u( F
- if($pic != "" && $pic_name != "")' J* L- X7 @) i) [& i; L& [
- {
3 n, G; c( i9 g0 m' t4 A1 S - $path = "images/";4 b' A% f! G" E* L0 G* F
- $pic_name = $path.$pic_name;6 |. O" J6 x- P2 Z* O: ?, M
- echo $pic_name;4 ~2 q: T- I' V
- echo "||";, I' g: p! i+ u$ Q2 o8 v$ g
- error_reporting(0);5 q1 E6 }8 U# L* i
- mkdir("images");
9 @; r7 Q- E% q" Y3 Z - $handle = fopen($pic_name, 'w');
! U$ m% w7 y3 Q8 k. t - echo fwrite($handle,file_get_contents($pic));. Y( A) [ z) N: f) {
- }
" ~$ w. z% O9 | Y2 y5 t2 F - 6 h6 V" l/ N5 j# G* j/ {
- $title = $_POST["title"];3 h$ B! s% W0 v, q: E
- $content = $_POST["content"];; A4 T5 J7 p, y& Q" _# R5 K5 O9 g( F
- $tags = explode("_",$_POST["tags"]);
2 W. P* N A1 A# T3 [& ~1 K0 O - $cate = $_POST["cate"];! u6 v! d% _' \- w
- //print_r $tags;
+ P% h' ^6 C! L$ t6 A* H - //
# b; s1 D9 r& I& R2 r, F/ h/ p0 J/ f3 ^ - //4 c+ Z' M6 r" I4 @3 {% C
- $wp = new WP();4 M# R7 g7 c/ O* h0 Y+ N
- $wp->main();- ^) h# W+ v) Y8 n; ^' y E
- //
# D0 e5 t: R& K2 Q3 Q8 L( O0 { - $my_post = array();
* V8 M, j& \& {, R9 j7 o - $my_post['post_title'] = $title;( v8 S& ^7 g7 W {0 f, U
- $my_post['post_content'] = $content;! @3 f( X6 H- [
- $my_post['post_status'] = 'publish';
2 N9 d. ^+ |4 Y( y - $my_post['post_author'] = 1;
0 l l9 p, B3 ]7 s8 P9 n - $my_post['post_category'] = array($cate);
8 [- k' P7 g$ B" g7 `9 m - $my_post['tags_input'] = $tags;
6 N% ^5 z4 Q0 u& {* c - //$my_post['tags_input'] = array('tag1', 'tag2');( j, T3 f/ Q, {( a6 s# V; [
- //$my_post['post_status'] = 'future';% k' y8 |! _1 i' |6 H
- ////$my_post['post_date'] = '2010-07-04 16:20:03';
# @0 W( ]; t; S+ T
/ _+ H4 G$ s' ~2 K7 X. ]; _* _- // Insert the post into the database
3 U! D) ?$ c- a: Q4 v! Y - $ret = wp_insert_post( $my_post );( |) ~! N/ {% p7 X/ G
- echo $ret;
, {+ k1 |3 G% \5 ~. i7 ?! T - ?>
8 v2 e/ o. ?' `. I$ d1 N$ i: f
复制代码 我自己写的. F9 ?7 _1 T* k: F- N( {- L
插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|