|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑
D( Q3 p, R) U& p% K7 J3 R# x1 A6 `8 a4 G! n& A0 b* s8 k
直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持
. @; U* \( R5 W) F
, r2 c. o, `, X8 _ y$ a% T我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了
1 W2 q6 J4 x; ]8 N2 s; ?& f$ P' M0 B& W' a1 b6 |
方便实用 简单 何乐而不用呢- 2 J; ?2 Y" O9 Y9 A. r
- <?php
3 _/ |. B: k' F! X- |) z1 ?, R
8 b" t& J6 t2 O, E3 V- define( 'ABSPATH', dirname(__FILE__) . '/' );
5 |1 z+ M: U% h( ?& I! _ - require_once( ABSPATH . 'wp-config.php' );) N" h; O1 j9 A
- require_once( ABSPATH . 'wp-settings.php' );
2 J0 u! B3 Z0 O" x9 o3 ] - require_once( ABSPATH . 'wp-includes/class-wp.php' );2 E' ?. H& b" U1 n5 E. K, e" M
- require_once( ABSPATH . 'wp-includes/functions.php' );8 d6 |* d. F8 Y) s3 y+ |' g
- require_once( ABSPATH . 'wp-includes/plugin.php' ); h' \9 b) Y) [
- $title = $_POST["title"];6 Z: n! @7 t2 X! F: z% {
- $content = $_POST["content"];+ x7 ?# B0 T0 I/ y* P$ y
- $tags = explode("_",$_POST["tags"]);- `# F) b& v# `4 \# h3 J0 h2 W
- $cate = $_POST["cate"];& t" J2 q- N, V- ?% D
- //print_r $tags;
3 K7 A0 l+ F- }/ `# Q0 U - //
4 g. y7 w W; t0 W7 x - //3 J0 k8 ?' I6 c# Z2 o
- $wp = new WP();" l7 \ t. @7 I* O4 O: K
- $wp->main();
( v `# U$ Y, {! v* M( R" R# O2 j - //
1 x9 P- q0 {+ i& V+ B i* V - $my_post = array();
F. O0 S. ?8 f - $my_post['post_title'] = $title;7 i0 I3 L. b' o- s. E; r9 E5 |& B, s
- $my_post['post_content'] = $content;
0 S* h% ], h( J# i - $my_post['post_status'] = 'publish';
7 o$ f/ p. O e: ` - $my_post['post_author'] = 1;
I! ?+ r% _9 R$ ] - $my_post['post_category'] = array($cate);
1 N* o2 R5 z2 z A - $my_post['tags_input'] = $tags;
# Q- D4 x% ^0 O' Z! k9 Y' W; @5 c7 B - //$my_post['tags_input'] = array('tag1', 'tag2');8 N' `* D6 l0 i0 `7 `1 q9 d5 u
- //$my_post['post_status'] = 'future';; s) l9 b% G5 _4 Q& V9 ]% J: o6 o
- ////$my_post['post_date'] = '2010-07-04 16:20:03';6 k' U' ^% y/ x" W& X/ C* q. t
9 @. b# L* J6 }6 c- // Insert the post into the database
# h- H1 C" o" n - $ret = wp_insert_post( $my_post );) E( ^, ? Y+ u' S7 F" _
- echo $ret;5 F0 A) z l K2 F2 [( M$ Z
- ?># [- _: B4 U0 d, H% a( K( e
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气- : x) }& C3 N% }8 h Z
- <?php( Q3 w& r: E6 P& S; b
- define( 'ABSPATH', dirname(__FILE__) . '/' );6 l" T* Y, n: \, G, `6 b
- require_once( ABSPATH . 'wp-config.php' );2 P' c. S. B: G/ h$ M
- require_once( ABSPATH . 'wp-settings.php' );$ d# y8 F5 `: w" U9 [2 p
- require_once( ABSPATH . 'wp-includes/class-wp.php' );
8 I/ x2 I& v& u8 W - require_once( ABSPATH . 'wp-includes/functions.php' );! ~+ g. ^0 R/ S* a8 p
- require_once( ABSPATH . 'wp-includes/plugin.php' );
$ ^! V0 q5 w E; b: ^7 r - $pic_name = $_POST["pic_name"];0 D- K. r2 ^7 z. N8 ]& a9 ]
- $pic = $_POST["pic"]; s6 E$ g2 m, x' Q3 A7 t
- if($pic != "" && $pic_name != "")6 u5 {7 K# M% ]# {# ~
- {9 _- F3 g' U, w+ L+ h, |% I
- $path = "images/";4 x( P, z% t& e7 e% i) \2 u
- $pic_name = $path.$pic_name;
1 }8 r: W5 _" d$ k) y) G: n' E1 |2 a5 ] - echo $pic_name;
& z9 o+ D4 B7 E: m; }& n - echo "||";* g! b& a5 [) u: W
- error_reporting(0);
) B3 U' x! E; P# T& l! @' B& U - mkdir("images");
9 P8 p$ T9 H( p3 z# } - $handle = fopen($pic_name, 'w');2 C: h7 U& o" B1 h% a
- echo fwrite($handle,file_get_contents($pic));
0 f8 O; s6 G' m) R" D - }
4 Y, o/ Y0 c6 Y1 y - 6 @# L9 W( w0 r# h2 w3 ~
- $title = $_POST["title"];
7 }( \* r: E0 U% N A - $content = $_POST["content"];
, A% L8 K5 G$ ~; R; g2 \( @- S - $tags = explode("_",$_POST["tags"]);
2 W4 U( k! q0 W2 E - $cate = $_POST["cate"];* U) v3 }, `( y u. p. {* i: J
- //print_r $tags;% S0 q" g- l5 q$ Z) I
- //, t1 t, K! s6 b: _/ K, p' M
- //
7 t& L2 P& G/ c6 q+ C& x+ d - $wp = new WP();; H( |" y. z2 ?1 R: P3 ?
- $wp->main();' B3 v2 i" c) H- N- u! \5 ^: |; [7 f
- //
& f4 x! S) @% A - $my_post = array();
/ ?. C5 Y' z: C4 r# B! Z - $my_post['post_title'] = $title;3 b; W% I! b* d# \
- $my_post['post_content'] = $content;
% n+ _5 M: i& [$ {+ F# s - $my_post['post_status'] = 'publish';" u* J2 l/ }4 H( H4 D
- $my_post['post_author'] = 1;
5 y( q" d# i$ m6 N% L2 k. R. r - $my_post['post_category'] = array($cate);
& _' g% N( c) [7 n - $my_post['tags_input'] = $tags;
* ~! H" ?# f. Q: J2 a, F( B - //$my_post['tags_input'] = array('tag1', 'tag2');, x3 r/ k8 r$ d, V3 q8 I& z
- //$my_post['post_status'] = 'future';
" |6 U: a% G2 {( _/ Y - ////$my_post['post_date'] = '2010-07-04 16:20:03';- }: ?- V" O5 t. x5 X
- , k' Z0 e. k0 Z- ~$ r+ B
- // Insert the post into the database
7 ?, j0 X- P$ Q2 x m1 Y - $ret = wp_insert_post( $my_post );* k* }$ ^# }: U. O
- echo $ret;
: N: [) p8 {9 x$ ?6 { - ?>
8 O) r! D- y, H% H! N- r* L& \! c: V
复制代码 我自己写的* [7 Y! u1 e! }5 M; Y
插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|