|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑 - X. b% F6 C# x; y; O; [! H
* V7 G& e1 H" e, }
直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持4 N v+ s7 F9 c+ V( D6 l+ z
3 R a# @/ \( b* ~4 Z& R我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了
8 v) B/ }8 t# S2 Q$ d3 f7 i2 j8 ?; S; D; o: E
方便实用 简单 何乐而不用呢
9 Y! Z; R5 I8 }" ]7 q- <?php
. T* P6 k" L5 m3 s( S
q& C2 k* [! _6 c1 j2 |- define( 'ABSPATH', dirname(__FILE__) . '/' );
" _ n) C- e8 |, N8 J# a6 A/ N' m/ O - require_once( ABSPATH . 'wp-config.php' );8 {6 _* F2 o; C* m( t4 }+ H [
- require_once( ABSPATH . 'wp-settings.php' );
. s: P! h& V# W5 @/ u - require_once( ABSPATH . 'wp-includes/class-wp.php' );
7 G2 U3 U9 X- F/ L - require_once( ABSPATH . 'wp-includes/functions.php' );* B, r9 K( g/ c& F
- require_once( ABSPATH . 'wp-includes/plugin.php' );$ \4 U& D4 X1 s; i
- $title = $_POST["title"];% [' o4 q5 V. m3 ^' ]
- $content = $_POST["content"];( H/ Z2 d E5 g1 `
- $tags = explode("_",$_POST["tags"]);
' _4 S. k* E# s, Q - $cate = $_POST["cate"];2 r3 ]. D9 \9 h b' j* Z( r, `3 P
- //print_r $tags;
6 Q4 m2 k8 `; ], u - //$ f5 t( _' a- O; {
- //4 Q$ k4 N' O6 d% n$ T
- $wp = new WP();
/ W8 s: c# @6 K; D6 j( D. ] - $wp->main();
; X* ~! r' f1 X - //
' G# |2 q0 B E4 b& k - $my_post = array();" h3 s v* `9 @
- $my_post['post_title'] = $title;; ~0 K6 b! K+ d8 K6 i
- $my_post['post_content'] = $content;
A: M% f) z Y" O( u - $my_post['post_status'] = 'publish';
% q" w, l: g4 |0 h5 b0 o. ^ - $my_post['post_author'] = 1;4 Z6 z/ K `$ W
- $my_post['post_category'] = array($cate);
4 T# e( k4 v6 [- w6 J - $my_post['tags_input'] = $tags;
7 k$ e, M3 ^7 C1 b; v0 D' S - //$my_post['tags_input'] = array('tag1', 'tag2');) N/ u) W3 [! ^ Q9 t" {
- //$my_post['post_status'] = 'future';/ T5 h, n/ I- m# |( W2 Z) c
- ////$my_post['post_date'] = '2010-07-04 16:20:03';% T1 `' |+ ]8 T( g
- + ^. z& B: @: S4 `( i
- // Insert the post into the database c4 [4 A4 e' y4 S. X7 _
- $ret = wp_insert_post( $my_post );- s6 B% B2 G4 {( a5 M
- echo $ret;
7 s+ e$ q, @# f0 _0 m9 b% H7 X' U - ?>
% ?$ ]9 e8 X4 y+ F7 ?- r" |
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气- - x+ O; y3 \5 ?/ G0 l( }) n6 ]
- <?php/ d$ m1 `- _3 Q2 E2 x
- define( 'ABSPATH', dirname(__FILE__) . '/' );
2 \- V! y9 X3 N& c4 ~4 ] - require_once( ABSPATH . 'wp-config.php' );7 E/ V$ R; o* D& Q) M+ R. ?
- require_once( ABSPATH . 'wp-settings.php' );" z+ S0 u8 r q: U3 ~/ s
- require_once( ABSPATH . 'wp-includes/class-wp.php' );
F# L' f/ L! @( D - require_once( ABSPATH . 'wp-includes/functions.php' );( w4 i. j9 G G8 G
- require_once( ABSPATH . 'wp-includes/plugin.php' );" _" f/ _6 y% \2 L* X3 y
- $pic_name = $_POST["pic_name"];& P' h7 g/ G! s. D* V
- $pic = $_POST["pic"];
7 G& R }" n4 G - if($pic != "" && $pic_name != "")
7 w/ K# p7 ]0 b) o& g, E - {
( K7 u, x1 M4 l# l1 O. D/ B - $path = "images/";. b2 `) R- J# a9 U
- $pic_name = $path.$pic_name;
* M) o2 {1 _' V5 ?# B: ]1 } - echo $pic_name;" R' Q u2 E0 A$ h
- echo "||";; L2 V( e' L, E9 k" k0 u; `
- error_reporting(0);% k- w" T E( M5 X: W- i
- mkdir("images");- Q: Q# O8 x4 }5 s7 H
- $handle = fopen($pic_name, 'w');$ G E6 ?5 d" y3 L8 o5 V1 [
- echo fwrite($handle,file_get_contents($pic));( r4 _# V3 O+ ]/ r c3 s2 ^ p
- }
9 B' h j" A; m( J6 t2 Y4 T. U' L/ g
: H3 c9 b+ K$ {& ?* j' ~6 u7 Z- $title = $_POST["title"];# A; P6 C- \* P- Z
- $content = $_POST["content"];
) O, h: _1 |9 B - $tags = explode("_",$_POST["tags"]);
% A1 j/ |) ]& h/ T, Y% f, O* i - $cate = $_POST["cate"];
+ I, T! F' t. J" a - //print_r $tags;- I9 y. r. ?3 @7 c0 s+ O# p3 U, Z
- //$ N, A. [- x( t* f; u4 E
- //
( m1 L+ C3 b( H& _) C9 a - $wp = new WP();+ g9 p6 T1 b& M! {
- $wp->main();
& B g: N; o6 Q3 X - //: f6 j6 E, `$ X2 f
- $my_post = array();
2 C& o0 {6 G9 I7 j - $my_post['post_title'] = $title;
7 v- d. L1 J. t) c5 y3 [ - $my_post['post_content'] = $content;
/ u& f# [- @9 F7 |: Y - $my_post['post_status'] = 'publish';9 c5 n4 d: E) G8 [* p: E
- $my_post['post_author'] = 1;& E8 W+ P& ~+ F" _3 o1 d
- $my_post['post_category'] = array($cate);
- d5 H8 I( [* s; ]: v, m: ^ - $my_post['tags_input'] = $tags;/ k! {* j4 r1 C7 h7 ~7 N: d
- //$my_post['tags_input'] = array('tag1', 'tag2');0 I" m0 O" J) L( s
- //$my_post['post_status'] = 'future';
" |. t4 a2 O! @2 {/ T" H; X5 K - ////$my_post['post_date'] = '2010-07-04 16:20:03';
/ C( N9 r5 w+ f) \
# Y9 t5 R! u! [8 O9 l4 \* b- // Insert the post into the database
/ v2 z# h0 q( }* B* X4 d# E# { - $ret = wp_insert_post( $my_post );
6 _2 s- {" \. J) C$ }4 w. o - echo $ret;! n/ O; B+ ?* e! ^
- ?>4 |# S8 a2 P' V2 w
复制代码 我自己写的
; \! G$ h! Y. x7 r% N9 S2 Z插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|