|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑
7 p: G* N) t0 o* O. t. R( }1 g) N
+ p! X$ j$ K/ E8 s直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持; C& d6 Z S" s9 L9 x
( O# X1 Z2 d& |7 g
我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了% m5 [/ M; p" q) k, f* k) m* c
8 t# Q/ h; ^0 E方便实用 简单 何乐而不用呢
" ?4 ^, ?* ~6 W6 l- <?php' B$ c7 T* O; E+ h: \
0 G7 @2 K3 W* H' N" e2 B2 J- define( 'ABSPATH', dirname(__FILE__) . '/' );: O* c2 G1 p" U3 \5 ]: K6 I# y' F
- require_once( ABSPATH . 'wp-config.php' );! L1 Z j9 R6 m+ }+ c- G4 e
- require_once( ABSPATH . 'wp-settings.php' );2 t0 `% c6 N: k5 y+ X$ s" @0 z: f
- require_once( ABSPATH . 'wp-includes/class-wp.php' );" q' H5 n8 ]7 p; `5 ?% y% i
- require_once( ABSPATH . 'wp-includes/functions.php' );
( o: F/ W6 Z/ k' k5 |/ n$ W - require_once( ABSPATH . 'wp-includes/plugin.php' );2 M. d/ x- @7 ^
- $title = $_POST["title"];2 X% g+ s; Z: B, o$ \; k+ Y
- $content = $_POST["content"];% L, A# u% x" I) Z. b" a' t b
- $tags = explode("_",$_POST["tags"]);
5 M, O# z" K" }, R; X - $cate = $_POST["cate"];! W6 L9 k# d) [; K
- //print_r $tags;
- ?9 k8 z$ c( w$ U. A3 o; I - //
0 @* E& a: \& e, @6 N& J - //
1 |+ v3 u* v% _6 [1 J8 X- y5 Q - $wp = new WP();. j+ K6 K" o$ Z
- $wp->main();
0 y0 k7 n3 h5 R. Z9 O& v' Q6 d - //; O, t9 {" J# ]( o
- $my_post = array();1 _* Z- @8 C" x8 c9 O/ g( x
- $my_post['post_title'] = $title;; k6 x$ p9 D) O* v4 m5 @1 n
- $my_post['post_content'] = $content;6 R! |; a/ U+ Z
- $my_post['post_status'] = 'publish';
( D! i5 b0 C; u; s9 E# Z) Y! z; { - $my_post['post_author'] = 1;
6 X2 y% X: Q' A+ O8 J0 ] - $my_post['post_category'] = array($cate);& I. O- q4 V5 y J f( v
- $my_post['tags_input'] = $tags;
7 H& T6 Q1 Z3 F, W - //$my_post['tags_input'] = array('tag1', 'tag2');
- _2 [0 y6 l$ Z- c2 T- K4 o7 W* ? - //$my_post['post_status'] = 'future';# O' ~ d7 W7 |" ^
- ////$my_post['post_date'] = '2010-07-04 16:20:03';( ~! v3 \+ K* E$ n
7 N- W" w* T" Z0 J8 N6 L" P- // Insert the post into the database
' X ^ }7 B0 ^# @0 d - $ret = wp_insert_post( $my_post );4 q5 E) \: q9 D" i% h. h' s& G$ h# p
- echo $ret;; E; b* y+ B5 d- o
- ?>: v+ h/ B& d) U! p L7 ?
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气- 0 z, ^& _2 H: ~6 }0 u! `
- <?php% C, h/ A& L; m& w1 I4 v% r, \) i
- define( 'ABSPATH', dirname(__FILE__) . '/' );
" T! G/ m( }0 r* N1 ^, A d& M - require_once( ABSPATH . 'wp-config.php' );" z4 H6 `; W5 }7 K' T
- require_once( ABSPATH . 'wp-settings.php' );
4 M/ }/ B( v" k. n. T% f. { - require_once( ABSPATH . 'wp-includes/class-wp.php' );8 Y/ s; S1 ^' }% L1 w2 `$ h1 c5 F
- require_once( ABSPATH . 'wp-includes/functions.php' );) C! y: A Q! S/ z, h( b1 J
- require_once( ABSPATH . 'wp-includes/plugin.php' );7 r D1 P1 @2 B# c* a7 C% x- {9 }
- $pic_name = $_POST["pic_name"];, {' u. k# M/ g0 v) p, t" |5 d
- $pic = $_POST["pic"];
$ x7 s+ s1 D& ^& ]; D/ F7 t9 x - if($pic != "" && $pic_name != "")5 j. j& e" r2 ^5 i
- {6 @& K* g# ?6 t4 C! |4 H
- $path = "images/";
6 [: G9 u7 Z f' ?# g' H - $pic_name = $path.$pic_name;
' j- k* }; L1 x& h v6 [ - echo $pic_name;( R5 h# \* @( _, ]9 O8 s
- echo "||";
3 G/ {% h' E8 w2 E+ d2 M$ L; q - error_reporting(0);
, L9 k p7 |* E7 H- n - mkdir("images");- K' }/ j$ V# s9 F
- $handle = fopen($pic_name, 'w');
7 x, N/ g2 b. I( b, s3 _ - echo fwrite($handle,file_get_contents($pic));
! r+ i( ~# \5 v- J: h; A( A, z2 W2 ] - }# `, @1 r% ], `7 p W3 Q0 j
8 K& q, \7 Z( \* \4 [- $title = $_POST["title"];1 d( B4 n( a+ y/ n' |5 _9 |
- $content = $_POST["content"];, I: S f9 U0 E7 t: _- y
- $tags = explode("_",$_POST["tags"]);& R9 S; U: ?# G. f, D0 G
- $cate = $_POST["cate"];: G$ \/ d5 X! c4 j: J' J
- //print_r $tags;$ v; s! l' g- D6 d. V
- //
) V% I4 C3 m; R# s - //
) ^ W/ s" M W+ |1 G - $wp = new WP();
0 \7 h$ j6 X# Z# r( Z" t8 O# m; B4 R - $wp->main();
* l. O& ]/ W0 Y% s: {4 `- c - // P/ @0 q0 U' I5 a- H+ X4 Y: h
- $my_post = array();: w# e- ], R9 C
- $my_post['post_title'] = $title;
) `. a9 {2 e3 v! M2 {: V' J - $my_post['post_content'] = $content;
! H. z7 K. @5 p ? - $my_post['post_status'] = 'publish';
6 @6 H2 [. m% P/ Q+ x a9 r - $my_post['post_author'] = 1;
1 g1 D: T- f# X) R8 P, s. C( V# I - $my_post['post_category'] = array($cate);0 P* ~% Q3 z6 M" Z, t9 o T
- $my_post['tags_input'] = $tags;
8 {8 D0 W f8 \ - //$my_post['tags_input'] = array('tag1', 'tag2');
, M8 d8 K5 X: ]9 @3 [ - //$my_post['post_status'] = 'future';
% A T* @% L/ L" E k# R( { C3 [ - ////$my_post['post_date'] = '2010-07-04 16:20:03';
9 T. t: e6 t M7 P8 [
. C }3 Q. H# A9 j F- // Insert the post into the database: N% d9 \/ N' Z
- $ret = wp_insert_post( $my_post );! V! r4 T. ^3 g: P) K- A
- echo $ret;
! \4 j8 \2 N) p6 d8 o0 S i8 v - ?>
! J1 a2 m& J2 Z' n
复制代码 我自己写的7 [# y# N" b5 Y
插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|