|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑 6 m6 n S1 K. r3 Y2 C$ S, m
! ^1 K3 K$ J$ |, ]! D& {* i* G4 _
直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持. w- R' r' z4 t+ Q
4 ]* ]1 i- _% t" K6 S我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了0 ]& H; `$ _- X
8 Q( c. A' e* ~+ c- m方便实用 简单 何乐而不用呢- & d! i1 F: X9 a& O. C1 |
- <?php
8 J. M4 M* w. \* u9 v' F) f% Z - 8 r3 S1 G7 Z( o" H5 J% A) ]
- define( 'ABSPATH', dirname(__FILE__) . '/' );: a, _) K7 p+ P; v! W% v
- require_once( ABSPATH . 'wp-config.php' );
* @9 X! }7 D. X* R. q - require_once( ABSPATH . 'wp-settings.php' );
& U* Z3 B1 Z0 c, Y+ `+ n; }0 h - require_once( ABSPATH . 'wp-includes/class-wp.php' );
$ p3 h6 Y& c# P6 M/ P - require_once( ABSPATH . 'wp-includes/functions.php' );
. x3 j6 F3 f- Q+ b$ P+ L6 z. s, Y& ^ - require_once( ABSPATH . 'wp-includes/plugin.php' );
( L8 z, o0 R: n8 v - $title = $_POST["title"];" D' \: p; X7 x% V7 y. X7 O
- $content = $_POST["content"];/ i# ?8 k/ I; x# j0 A
- $tags = explode("_",$_POST["tags"]);" R) C* p+ ~# ?, d- V. r. T
- $cate = $_POST["cate"];
! U/ o8 @5 z3 Y! d1 l& B7 G - //print_r $tags;( N, p( q5 s) @; z& a
- //
2 Z8 C3 G O# `; t4 \, ~) [" ~ - //
. O1 i9 _$ E) n6 { - $wp = new WP();
, `, A& I' X+ V% ?4 `# j - $wp->main();
( e' a% [0 h) q% {: } - //6 @# N5 {5 q9 g
- $my_post = array();
4 J8 Z2 a! H* V( u1 b6 d" G0 } - $my_post['post_title'] = $title;
& ^8 b# \6 h/ `" w/ r1 N - $my_post['post_content'] = $content;
@% ~/ o, C9 Z - $my_post['post_status'] = 'publish';
4 @# H5 b9 W* C8 b2 H - $my_post['post_author'] = 1;1 @6 n+ W2 |, {2 r7 k' W K% s
- $my_post['post_category'] = array($cate);
: r# A; S* z- v: p- X. T' @, x - $my_post['tags_input'] = $tags;
& f r$ L6 O% b- U: B4 G - //$my_post['tags_input'] = array('tag1', 'tag2');4 S. l, S4 f' U
- //$my_post['post_status'] = 'future';0 T' ? A( d8 t6 u
- ////$my_post['post_date'] = '2010-07-04 16:20:03';6 x9 ]5 u" c P3 x
- 4 ~* T( f: R! ^! \' o) F7 a; ?
- // Insert the post into the database# l, f' x' y0 @
- $ret = wp_insert_post( $my_post );6 }+ M/ o7 j% G6 n2 t+ b5 O) l
- echo $ret;7 Z4 a, S7 Q1 v( Y
- ?>
- U, s p/ i# G8 X
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气
" \) q" Z) s+ H+ O- <?php
8 @3 m% `6 c1 }$ g( O - define( 'ABSPATH', dirname(__FILE__) . '/' );
. g+ M/ H* j3 S - require_once( ABSPATH . 'wp-config.php' );/ C( v- _+ @/ G/ r: y
- require_once( ABSPATH . 'wp-settings.php' );
0 Z. e+ m" C/ H' K0 _ - require_once( ABSPATH . 'wp-includes/class-wp.php' );
. z8 B6 f y* a9 O Z3 Q - require_once( ABSPATH . 'wp-includes/functions.php' );
# U8 q' k3 Y7 G# Z% s0 W% A) q' f - require_once( ABSPATH . 'wp-includes/plugin.php' );
1 L( P% {2 `" w( q - $pic_name = $_POST["pic_name"];
2 h- n. G" N2 {5 e8 T0 v - $pic = $_POST["pic"];
$ t; l0 K( W6 A) n8 c" d8 b4 X - if($pic != "" && $pic_name != "")
1 I* m! Z8 {0 q) X - {
' M- U8 `2 u5 b- Z0 c - $path = "images/";
. Q$ u! W/ }- d) q- D' ~2 } - $pic_name = $path.$pic_name;% S6 H+ A) R7 C1 i6 ~
- echo $pic_name;, E( [; w# a$ N7 {- |! m( ?9 h
- echo "||";7 n: R' u9 }* j
- error_reporting(0);# ]; t3 K$ W" B7 E. P) {
- mkdir("images");% y$ H! C' {6 T: s: K: E# p
- $handle = fopen($pic_name, 'w');5 \( p/ i. S( ]2 a- W* c/ r+ E8 |
- echo fwrite($handle,file_get_contents($pic));4 d' N+ O. K" O$ b( K0 D! f/ d
- }2 r, Q8 Z% C4 N3 o
- / W& g; j5 I( P' H# Z* I
- $title = $_POST["title"];
) i. s5 B% K! O2 I8 L - $content = $_POST["content"];3 Z' V9 B8 ]! J$ h( P( o* ~, Z/ m
- $tags = explode("_",$_POST["tags"]); }( ^( w1 E' K6 ?# q- a
- $cate = $_POST["cate"];
1 G+ Q; b) B: h. N4 ]0 N - //print_r $tags;
- k7 @/ t) I$ J2 g - //' F$ ^8 C! \1 w$ B
- //
: x6 o2 {$ u2 s$ ^% a" u - $wp = new WP();' i3 m9 u6 Y6 K: `( ~5 o3 U" L2 [4 i# G
- $wp->main();; z+ H L. a( u
- //
3 y- ?/ \% J7 m0 _ - $my_post = array();
& w# b: v% V3 B2 s0 F' L9 ]! Y - $my_post['post_title'] = $title;7 \+ H' W% m) m) }, Q$ B
- $my_post['post_content'] = $content;, T8 V$ x# k& H: K- } g7 g
- $my_post['post_status'] = 'publish';# @ ~& @9 I' Q4 g
- $my_post['post_author'] = 1;
' k) a6 n3 [+ R: R o+ D. E x2 c - $my_post['post_category'] = array($cate);1 c0 g9 ~1 z c
- $my_post['tags_input'] = $tags;3 g4 X3 z6 X! j2 ?4 M9 H B; ^7 a
- //$my_post['tags_input'] = array('tag1', 'tag2');3 Y+ ^4 E6 u* Y) a. c1 c
- //$my_post['post_status'] = 'future';
( e) O. V1 S3 k! s$ X- c G - ////$my_post['post_date'] = '2010-07-04 16:20:03';
% N0 o9 {4 ?% O3 B) @ - ) L) ?- I* v* g
- // Insert the post into the database
: q/ @& H+ J3 d6 Y1 I- l - $ret = wp_insert_post( $my_post );* S g$ Y5 Z7 c
- echo $ret;
4 @# C, D; ^4 h+ L - ?>
" p8 r9 K' F! W( Z# U! S
复制代码 我自己写的
/ _5 k; q% Y! U) x3 V: P插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|