|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑
# E8 K" x- O8 f5 O; z4 l6 E! }2 d
1 h1 v J! B, c7 t, I- z7 S直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持; U/ l2 K6 B6 d9 V& i
! T7 \ U6 G: T0 m/ H( e
我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了
" C1 ~, T. U1 ?. | R" M: M
7 o6 f* p! Q/ \# N: a方便实用 简单 何乐而不用呢
. m/ n! S. {4 `- <?php
" H4 H% _1 Q- ?8 ?. ~
" j7 ?/ E. L3 h5 c) U* f( ~0 }7 h- define( 'ABSPATH', dirname(__FILE__) . '/' );
. v2 }$ u2 B; ]: n, u* v& u - require_once( ABSPATH . 'wp-config.php' );) E. X! w( g, A) N: U# r
- require_once( ABSPATH . 'wp-settings.php' );
, ]/ a/ g* d2 ~- ]! e& F. |/ z - require_once( ABSPATH . 'wp-includes/class-wp.php' );
4 ]5 z# C! p( M& K' `" Z7 m* y" @ - require_once( ABSPATH . 'wp-includes/functions.php' );
# R/ L' C* H/ X6 Z1 P& K - require_once( ABSPATH . 'wp-includes/plugin.php' );
. m$ v7 F- @$ b - $title = $_POST["title"];5 u4 j# |; l+ n
- $content = $_POST["content"];
. y% b/ [ d9 i - $tags = explode("_",$_POST["tags"]);* p% }, p6 C4 z$ A, ?8 o: U
- $cate = $_POST["cate"];
! O/ e) M6 Y6 v' ~ - //print_r $tags;
9 t! T( F% x P$ ? {/ Q - //
. V) H: c* R/ ` - //
$ i% U" p; k+ d% B% W% p - $wp = new WP();
6 Y2 a( @% Y' h0 `& S3 m0 Q* c - $wp->main();
( A8 _2 x9 ^" z+ X4 [- ? - //
2 U% x5 D. P% n; y ^+ ^" k - $my_post = array();% t* x1 s+ l% ]' W/ \
- $my_post['post_title'] = $title;# _* |& Y) X5 Q7 c2 _2 }
- $my_post['post_content'] = $content;
, k' K* Z% N2 D# c; _3 z# N - $my_post['post_status'] = 'publish';% y( u4 M& W; A" M6 g4 U' T
- $my_post['post_author'] = 1;
3 U `" C; p3 B! i$ ]1 i - $my_post['post_category'] = array($cate);
3 s" V. j" J/ ?3 ~) E - $my_post['tags_input'] = $tags;! p7 t/ W. o0 Z9 E! F) V; y( S
- //$my_post['tags_input'] = array('tag1', 'tag2');8 n( p8 q* c. b, ]' K/ `) w
- //$my_post['post_status'] = 'future';
: f7 |& | h% I - ////$my_post['post_date'] = '2010-07-04 16:20:03';
+ s! E5 L1 m$ `. r# r# z" Q2 j - + W) E0 _' f1 V |+ T
- // Insert the post into the database; p3 n( _& F! c+ u" H
- $ret = wp_insert_post( $my_post );7 d; G H( p! d( R
- echo $ret;3 ^7 a3 i) c( T: F) U3 t2 q, y5 b
- ?>" R I* u! @3 [/ b% r: F
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气- . O% o6 b5 a# q$ V0 e2 u' D0 }9 e
- <?php
S K5 b& p+ R. I4 x - define( 'ABSPATH', dirname(__FILE__) . '/' );; i+ D2 q' E2 `, d. H2 L+ Y
- require_once( ABSPATH . 'wp-config.php' );- ]( ?5 `7 G) r1 F
- require_once( ABSPATH . 'wp-settings.php' );8 C' G; I& |3 n# Y. [; j
- require_once( ABSPATH . 'wp-includes/class-wp.php' );* J8 a0 N9 m- w" G4 }! l: U
- require_once( ABSPATH . 'wp-includes/functions.php' );' ^$ }7 G6 P8 a) z! m
- require_once( ABSPATH . 'wp-includes/plugin.php' );
, D% X& C/ W& H* e- _. C5 n |) R- { - $pic_name = $_POST["pic_name"];
6 @" p t! l: S/ |, Q3 b) ?* [ - $pic = $_POST["pic"];1 Z* H+ R, W0 @' ~" G1 Q. p& m& i. Q
- if($pic != "" && $pic_name != ""). `6 r. _3 J/ I
- { _* T. b! _- A1 C( A8 U4 o8 c3 B
- $path = "images/";
. c& ]. g' ?3 |% J5 d - $pic_name = $path.$pic_name;
4 ]; w: r0 k' t! L8 J! O6 G - echo $pic_name;* t2 O @, \0 y- i; L$ k" k: ?
- echo "||";
8 r. i, j$ \. ]! ]* A# x - error_reporting(0);" ` ^6 R3 u3 g: D# Y8 M
- mkdir("images");
9 C K1 Q: W5 g5 e. C5 J - $handle = fopen($pic_name, 'w');
7 b; l! Y: d! E: o2 c3 D8 n+ S) V; N - echo fwrite($handle,file_get_contents($pic));7 O8 W' n" p+ g2 [5 R" ~0 U' O7 `
- }
, }/ w6 a0 b' l6 b/ ]5 ]; Y. u - ! T1 |8 L- v% u. q& u) T
- $title = $_POST["title"];) ~7 q. g5 ?$ Q
- $content = $_POST["content"];
. Y0 N6 f+ a3 w: a! Y1 R) c - $tags = explode("_",$_POST["tags"]);9 U. a. I% }, |6 B9 w' k4 P2 e& E
- $cate = $_POST["cate"];
" E1 `; x0 ]1 \ h1 f M+ K3 u - //print_r $tags;0 f/ _/ X$ l) b! G2 q1 z7 t3 |
- //1 C- h4 G9 r4 [3 Y7 p
- //
$ g w5 Q {4 q6 w: ~ - $wp = new WP();
. R5 G% C. H! K/ y - $wp->main();& j3 w3 e7 A* x# l* P
- //
0 E5 O" o6 ^0 j% ?% X/ s - $my_post = array();& Y4 A+ M( |0 A( {4 j9 b% d/ k
- $my_post['post_title'] = $title;7 V1 h9 c5 K. x7 w( T% v/ y" r) Z) n
- $my_post['post_content'] = $content;
4 h# T8 Z0 z. u6 z - $my_post['post_status'] = 'publish';* X' ^$ F5 `5 Z2 G' a
- $my_post['post_author'] = 1;8 c& ]4 A- A W4 e x7 E
- $my_post['post_category'] = array($cate);
, R1 E1 d' c6 J% H - $my_post['tags_input'] = $tags;
3 `7 W; p1 V6 \8 ^5 [7 c - //$my_post['tags_input'] = array('tag1', 'tag2');
6 w# F8 U/ }2 P% \" ? - //$my_post['post_status'] = 'future';: }7 @! |' z, Z* b @
- ////$my_post['post_date'] = '2010-07-04 16:20:03';" Y" y9 P V: p, r' _4 ]
- # D; r+ m! p: I3 r- e
- // Insert the post into the database
- X2 R7 n8 ]! U* n - $ret = wp_insert_post( $my_post );+ t% X% W( K2 `& B3 i
- echo $ret;4 \: V. u9 B8 w, n+ J! K
- ?>
. }0 p2 q3 _4 H: i% N8 B6 C0 _; Y0 i( y
复制代码 我自己写的
/ B. V1 m# `! d7 U4 {; b0 u插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|