|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑 3 J1 ^2 q& b# I& {) `
( A3 R# ^8 N0 ?' e
直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持
( m4 u/ O1 O' N8 m! n! @% R8 ]7 j5 J) w; e; z- q
我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了
( r% J7 f" \% r V0 g3 S- L, u4 l/ G$ L$ B6 |7 ]9 Z( `
方便实用 简单 何乐而不用呢
3 {' N$ h4 e/ ~# K# a, N4 Q; Y- <?php, d: B2 l& D2 ~1 \
6 q' |# g( X# W( q& g# W- define( 'ABSPATH', dirname(__FILE__) . '/' );5 c8 q9 y+ c& Z* O' }
- require_once( ABSPATH . 'wp-config.php' );" j' d- |# U- x5 c2 \
- require_once( ABSPATH . 'wp-settings.php' );' W& V( [9 S2 `
- require_once( ABSPATH . 'wp-includes/class-wp.php' );
0 M2 I9 u$ }0 L - require_once( ABSPATH . 'wp-includes/functions.php' );" U# E$ O3 l9 V: b2 r- @
- require_once( ABSPATH . 'wp-includes/plugin.php' );$ E$ P5 X0 t( S+ k) H5 T
- $title = $_POST["title"];
. {1 V1 g9 N$ q7 m# ` - $content = $_POST["content"];0 W5 x$ {8 b5 W' _8 Z* B
- $tags = explode("_",$_POST["tags"]);+ n: e$ v0 H9 V: Q
- $cate = $_POST["cate"];
$ Y7 O; U, R9 _4 j1 V+ l: z0 B - //print_r $tags;
9 T: ]! [' t M! \ - //6 j5 e$ n. U X8 n# d/ Y. {
- //6 U$ ~$ R- i( S. L7 |/ s
- $wp = new WP();5 K9 B7 N) ~4 X/ U3 s* d0 b
- $wp->main();
& k) X6 Y$ W/ o - //
6 x6 r4 A& f0 A6 K8 q+ R/ U; } - $my_post = array();6 Z7 k' e; W# |0 W g2 |
- $my_post['post_title'] = $title;% k- j5 }* g) X5 l) B
- $my_post['post_content'] = $content;
' o6 K# A' @1 w% [; M7 A" } - $my_post['post_status'] = 'publish';( a# W$ K0 p0 Z
- $my_post['post_author'] = 1;
9 v {8 K& D0 S" ]: Z6 D. G% d - $my_post['post_category'] = array($cate);
+ J( }+ L1 b, b) q, M* z/ L - $my_post['tags_input'] = $tags;
, B- W( \8 I# f; b: ~( J& \8 g - //$my_post['tags_input'] = array('tag1', 'tag2');- t5 W- s' o7 w$ h8 }9 [/ F
- //$my_post['post_status'] = 'future';0 `- d Q+ n+ L. }( X
- ////$my_post['post_date'] = '2010-07-04 16:20:03';6 M+ i/ y4 @+ Q
. P$ F+ s1 M' ~# y2 R! d- // Insert the post into the database- @' p' w1 N& U: P, j) n" Q3 v
- $ret = wp_insert_post( $my_post );
3 Z, u- E) i4 C3 ]! g3 O3 Y - echo $ret;! l) m: ^0 E8 X3 A. V- A/ [. N
- ?>9 y9 L$ C# K s% {: }5 K
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气- + Y, _8 M* N8 o- Z6 o
- <?php
2 Z, k, D+ S9 W - define( 'ABSPATH', dirname(__FILE__) . '/' );
% W f/ @: W. k* Q) B) ] - require_once( ABSPATH . 'wp-config.php' );
7 x# e* ~# j! F$ D - require_once( ABSPATH . 'wp-settings.php' );0 O8 }! W6 _% o: q$ D5 I
- require_once( ABSPATH . 'wp-includes/class-wp.php' );
& ]- e! `# T `- ~9 W% e - require_once( ABSPATH . 'wp-includes/functions.php' );7 F3 Y2 Z5 G* i" ~4 g% z
- require_once( ABSPATH . 'wp-includes/plugin.php' );6 i3 A7 i) |: A- |
- $pic_name = $_POST["pic_name"];
( |2 U" L g& d& L: p& _$ C - $pic = $_POST["pic"]; z1 N; X0 w [7 k: D- a, I
- if($pic != "" && $pic_name != "")7 f9 K2 F) h/ o+ U$ L& G
- {
( U5 u3 L/ r% J( X - $path = "images/";
1 k' f9 V2 X; W8 l9 f" N% _ - $pic_name = $path.$pic_name;: L( J6 Q n% V! g3 Q
- echo $pic_name;7 h3 ]! E. z- b# G! I+ u+ P7 z* a
- echo "||";& z9 A7 a& t# \) D" h( C
- error_reporting(0);
& S$ V+ C$ `3 ?! i7 F - mkdir("images");4 u }* _! n! W& |" v* R5 X
- $handle = fopen($pic_name, 'w');
" w7 V" i. W2 E3 F, @: |/ @ Y - echo fwrite($handle,file_get_contents($pic));0 l+ G9 N5 Q. e- x- ^0 q9 i
- }+ a5 x- B2 I0 T$ V
) c4 j+ c7 a. [5 T; H& y- $title = $_POST["title"];
5 B* F# B" I4 h A: L) W - $content = $_POST["content"];
8 ~/ C; R, k7 b! T - $tags = explode("_",$_POST["tags"]);( q4 r9 o% T( e/ K) j9 e
- $cate = $_POST["cate"];- n. {+ T3 }5 o! Q& W+ }( K3 c: T8 u
- //print_r $tags;3 W# N T. M. E& ~0 A! T
- //6 z, U3 M* G+ A- d/ p
- //5 Z/ b' L# z& m" e6 s5 J$ S" ^3 y
- $wp = new WP();7 W; R& m! x+ j
- $wp->main();. Y2 `" D/ \- P
- //
; J& Z! I/ t: K( s& J# A! j - $my_post = array();
5 Q1 ?* @( c# j e" F- ~4 M( K. P5 N - $my_post['post_title'] = $title;+ G' K7 C& E- H( ^) ^4 q: ]8 [8 f c" b
- $my_post['post_content'] = $content;
2 }; c+ z6 M. C E - $my_post['post_status'] = 'publish';% Q' d' @) |, g
- $my_post['post_author'] = 1;6 E6 S! d- I6 \! a
- $my_post['post_category'] = array($cate);
1 K8 S6 Z: o) P0 G# a" i - $my_post['tags_input'] = $tags;
# ^1 v( ]& H* @# Y2 M - //$my_post['tags_input'] = array('tag1', 'tag2');' M, |7 p. q, B' Q& A2 U
- //$my_post['post_status'] = 'future';
' d8 V. m$ l3 `4 q - ////$my_post['post_date'] = '2010-07-04 16:20:03';
f+ f; m& G% H$ j+ T4 l# w1 J
* O+ e$ T V8 z- x U- // Insert the post into the database# f! S4 m0 s% Z7 s
- $ret = wp_insert_post( $my_post );5 [5 f- D0 H' D. f9 K
- echo $ret;
; w3 M$ w; |/ }7 J- T& [! n4 u - ?>
4 @2 q: |( S# `/ X
复制代码 我自己写的. b# X" \* c5 j8 K9 a
插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|