|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑
. e6 N1 N2 Y% V @" o3 p K, u O) R
直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持
: z) P) T6 j! P. y6 ?! [( M' ?" T
& k9 }/ p5 N" k* x* w* n- ?我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了( `8 }: c* d/ m% W* T \+ K
* ~7 f, C, j( ^9 e5 u Z
方便实用 简单 何乐而不用呢
! X P3 z7 V( T+ e- <?php
q6 b2 v$ v& V6 _' y7 c - , B9 G$ v8 p. y% D. D9 t
- define( 'ABSPATH', dirname(__FILE__) . '/' );
7 ^- Y6 V: |2 @; @ - require_once( ABSPATH . 'wp-config.php' );4 r. P7 h$ N# D6 A, s7 z
- require_once( ABSPATH . 'wp-settings.php' );* C6 M& J* ` M7 d; n+ L
- require_once( ABSPATH . 'wp-includes/class-wp.php' );
" S! T8 |5 p" B3 C* b/ e - require_once( ABSPATH . 'wp-includes/functions.php' );: N; W" x# u# g3 J9 ?2 y
- require_once( ABSPATH . 'wp-includes/plugin.php' );/ x8 C: C+ s8 Y
- $title = $_POST["title"];1 J$ Q* E+ y2 @5 p, N" U# _
- $content = $_POST["content"];
c6 V- ?* Z, K. ?' _) g - $tags = explode("_",$_POST["tags"]);2 |3 v" B, g5 k4 {
- $cate = $_POST["cate"];
2 U. k6 i d7 [( [5 f" Y% j1 b - //print_r $tags;1 k/ b; Y' b& ?# G
- //: P7 d! ^) [+ K# ]9 s- i) w
- //
! Z w) t$ H+ V2 `8 E( D - $wp = new WP();
& C' B \! {* y! ?: L! T4 T2 v - $wp->main();
% t$ v$ T" l& ^8 v8 W' N- { - //
6 S4 q4 l' C l6 v7 V. V: Z" ` - $my_post = array();" F* D: @3 p; Z: V4 X' l
- $my_post['post_title'] = $title;# P) L( L1 s3 Q
- $my_post['post_content'] = $content;0 Z$ G! f* d2 W2 h% b; H
- $my_post['post_status'] = 'publish';
9 d& w+ C" g/ n" x/ p6 g3 { - $my_post['post_author'] = 1;
# G# Q1 t& l g6 [* S" u - $my_post['post_category'] = array($cate);8 ~7 l) [3 u m
- $my_post['tags_input'] = $tags;
, y6 O4 L* g6 f/ u8 [- ?5 o - //$my_post['tags_input'] = array('tag1', 'tag2');
8 G, {! O9 ^0 K5 C - //$my_post['post_status'] = 'future';
8 h8 F* V8 P2 y; j - ////$my_post['post_date'] = '2010-07-04 16:20:03';
2 ^ k5 @) m5 I$ {% w
5 u% }2 P" q0 u' k5 m9 c- // Insert the post into the database
/ ?% O Y9 v4 u) V0 v - $ret = wp_insert_post( $my_post );0 Q+ _1 \3 L: ]4 ~, H
- echo $ret;
+ `" G& \/ [7 o2 ~ - ?>
; E0 ]1 x/ n' \0 p( [
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气- * p. L- {6 A0 o5 M+ f
- <?php
/ a: i6 R- J# M9 } - define( 'ABSPATH', dirname(__FILE__) . '/' );/ E0 h" k; a3 z4 v8 b
- require_once( ABSPATH . 'wp-config.php' );9 i$ l' q2 @+ r6 a
- require_once( ABSPATH . 'wp-settings.php' );" T; N, t' W7 z$ I. F* L
- require_once( ABSPATH . 'wp-includes/class-wp.php' );2 X' l6 }7 @7 m; {6 x
- require_once( ABSPATH . 'wp-includes/functions.php' );6 \3 C; Z: z- A1 I! l
- require_once( ABSPATH . 'wp-includes/plugin.php' );& f h/ I, a( d6 v7 h- l% T
- $pic_name = $_POST["pic_name"];
+ @/ c2 D8 ]& |! z* R2 c8 ~1 ~ - $pic = $_POST["pic"];
; B( l/ D5 |4 u6 c3 D1 g) W - if($pic != "" && $pic_name != "")
! H" M" M5 v$ j7 B9 o$ P+ a8 Y0 t - {4 E2 v- a2 D1 g7 @
- $path = "images/";# I8 M8 n t$ z+ n
- $pic_name = $path.$pic_name;: G. K! N( r1 Z/ O# h& q1 k( @: L
- echo $pic_name;2 c, Y- W6 [2 |( t
- echo "||";
7 ?8 O6 w" R& Q0 x P1 f - error_reporting(0);
8 R! l; t* b) l( l - mkdir("images");
+ G( H- N6 a# C! m2 p - $handle = fopen($pic_name, 'w');* X+ a+ e; \$ f A( o
- echo fwrite($handle,file_get_contents($pic));9 a! Z* ] }, W
- }
t4 D" T4 L# p0 I - ! @& d( ^) ]( S# N! Y: U
- $title = $_POST["title"];) p* n9 {% k3 L( y. z
- $content = $_POST["content"];; N5 u+ e* r% d% y! c# j0 a
- $tags = explode("_",$_POST["tags"]);- I7 E4 J4 A5 i9 t9 N5 H" z5 U6 x
- $cate = $_POST["cate"];
% A2 g7 I2 s5 t: X - //print_r $tags;
( c G7 J$ U- g' r( q - //
+ k, d4 N# G! ?9 a- P6 M( f9 x - //
9 B2 E% b8 p7 q8 p6 ~) u) f' ] - $wp = new WP();
) t; C; m, |1 L" K6 O5 B% Q - $wp->main();
/ X& n6 B0 ~) ?* y6 I2 I$ } - //
x9 H: H/ B+ y- S - $my_post = array();; ]5 R" A" e1 L( K
- $my_post['post_title'] = $title;5 V$ J8 k: j; S
- $my_post['post_content'] = $content;
" P( P7 I# b* ?! T2 u3 P - $my_post['post_status'] = 'publish';
/ O( M# Q7 D4 ?! q. o, o$ B - $my_post['post_author'] = 1;" }6 |, F9 f0 E& D1 p% q% o3 _. {
- $my_post['post_category'] = array($cate);4 K" U9 V" F# Z/ c! v6 b) f
- $my_post['tags_input'] = $tags;; k. z6 U% z) g2 a
- //$my_post['tags_input'] = array('tag1', 'tag2');3 e: y! H* q* Q- }/ c
- //$my_post['post_status'] = 'future';
" q$ l' C: w5 o4 }) Z6 `! x - ////$my_post['post_date'] = '2010-07-04 16:20:03';
! d5 I4 l4 R9 P) ?& r8 g - 8 ?* H" {% _0 m7 I, \$ d
- // Insert the post into the database1 u8 h1 G7 k" J
- $ret = wp_insert_post( $my_post );3 Y, }" A* |- l2 B4 @ `% o& l
- echo $ret;
/ y3 A+ c% i9 J* b - ?>
' b& ^- J- [# Z( [
复制代码 我自己写的
0 Q4 a8 u+ N W u插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|