|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑 6 l6 n: k6 s1 J k
9 F9 n# h! H; F$ ~. h: Y3 k2 \( R
直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持
6 t( Y: f/ F/ d j# D* j2 G: G0 E. K4 u% o) A" R% _# A. t5 n" S
我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了5 `( C8 J X, c" J+ U6 v3 I. Z
# e1 P9 [4 [5 v" w8 d" Q$ Q
方便实用 简单 何乐而不用呢- " c- r$ [; J0 q5 u
- <?php' ~4 `4 c$ T# ]+ p. Y A# G
- " D; s! q( t/ x8 n) Z( l5 C/ k
- define( 'ABSPATH', dirname(__FILE__) . '/' );, c2 S# O* S; t) o# [' a/ D
- require_once( ABSPATH . 'wp-config.php' );, V$ K$ g4 i+ w* i2 Y
- require_once( ABSPATH . 'wp-settings.php' );5 s4 b& T0 q6 m% l8 V I5 i
- require_once( ABSPATH . 'wp-includes/class-wp.php' );
# P% V3 {! F. o" |2 n q - require_once( ABSPATH . 'wp-includes/functions.php' );- S- W3 }) j1 y4 j9 i
- require_once( ABSPATH . 'wp-includes/plugin.php' );2 H7 }% y. ^; z5 p2 g
- $title = $_POST["title"];7 a+ A: g+ N9 w$ x# @# @* b
- $content = $_POST["content"];* t [$ v/ B# W2 f0 n7 N. {
- $tags = explode("_",$_POST["tags"]);
% Q* w; a5 c2 \( b w) ] - $cate = $_POST["cate"];
' ?6 @! d! ~/ J3 |: q- V) N2 w - //print_r $tags;
5 S& M E3 f; R - //: N' M' d: T, F" J1 c& {: x' {* ^
- //# P2 C ^9 @ r2 X
- $wp = new WP();
+ ~' v3 @, m$ M4 I! \6 A - $wp->main();* P5 z5 z% w: `: `
- //# P& l% {# u/ M+ B% m2 _3 U
- $my_post = array();
* J) l, {( S2 Q9 I% S - $my_post['post_title'] = $title;
8 |: b/ ~: q" m# k; g - $my_post['post_content'] = $content;( Y, U" m" z& l
- $my_post['post_status'] = 'publish';/ b% B+ I( r( e. N2 U/ z7 Z
- $my_post['post_author'] = 1;
9 F, F) \3 t( |7 @5 G: D - $my_post['post_category'] = array($cate);+ m, f" g7 B, P
- $my_post['tags_input'] = $tags;( }0 }- q+ l6 c. E0 L0 a: W
- //$my_post['tags_input'] = array('tag1', 'tag2');
; `" g+ h: L( _9 ?) |5 \ N - //$my_post['post_status'] = 'future';" w6 N* m+ l4 L
- ////$my_post['post_date'] = '2010-07-04 16:20:03';
& v1 r N' ?6 w
& K* C: H4 m) }% o/ X- // Insert the post into the database
/ {- m0 g- }2 O9 _0 C3 ~ - $ret = wp_insert_post( $my_post );
) n( B4 G; y0 d5 T - echo $ret;
% ^& B2 T; A: s9 z - ?>
) k" E# T. N/ M! e! e: {% D8 ~ P
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气
. E* B, n. K3 N- <?php$ |, N( A4 Y% B2 L
- define( 'ABSPATH', dirname(__FILE__) . '/' );8 L( C0 t( ]4 D+ k% b3 p0 F- f
- require_once( ABSPATH . 'wp-config.php' );, [& T0 r6 G- M% t- q7 @' C
- require_once( ABSPATH . 'wp-settings.php' );: e! i+ K3 E D. ~
- require_once( ABSPATH . 'wp-includes/class-wp.php' );# n- X: n7 x, F% T/ U' I
- require_once( ABSPATH . 'wp-includes/functions.php' );
/ N6 m% f) C' K+ I4 o - require_once( ABSPATH . 'wp-includes/plugin.php' );
" M1 S( `0 o% f4 w! [) F* C - $pic_name = $_POST["pic_name"];
7 X* ?& G* e% E - $pic = $_POST["pic"];
+ b, W4 f% O& V6 V+ E4 n - if($pic != "" && $pic_name != "")2 x) n& L, ?& O0 ~. e; \
- {- j, t& V' C' C/ Z/ R( k
- $path = "images/";0 T8 f; N" b7 B: W) H& T$ j
- $pic_name = $path.$pic_name;; q% ?/ Z7 I! T* ?) f
- echo $pic_name;
/ f E( q4 u9 |) @ - echo "||";
+ d' ^. {3 M+ }2 M( ? - error_reporting(0);
* Y8 S0 X: ~3 P9 K- N( n: h - mkdir("images");- G4 G* j) a `) e& K
- $handle = fopen($pic_name, 'w');% g- _7 R1 ` L8 q+ D; ?4 @, O
- echo fwrite($handle,file_get_contents($pic));2 l( M- \0 \7 a
- }
& e' [; k* m1 \: T. ]8 L* | - : K7 Q/ A7 p% p
- $title = $_POST["title"];/ L' f: C" U! _; N4 w; j
- $content = $_POST["content"];; ]8 M8 h3 t/ I9 q
- $tags = explode("_",$_POST["tags"]);8 `" c: f4 K# P! w
- $cate = $_POST["cate"]; P k% O( t4 L' b( x1 U
- //print_r $tags;: W, t" E- V& w# {- F& M# A
- //
/ I/ `4 N! f, U9 |! c; F2 I: D4 n: v. ` - //8 j6 r2 Z4 Z0 x* Y% @
- $wp = new WP();
; i2 R0 I3 g% B. m0 m" J - $wp->main();
6 K% ^* ^6 U3 H* c& h4 C5 e - //- e/ X( e0 K; U* z( O o
- $my_post = array();
) Q7 F9 W1 Y& U - $my_post['post_title'] = $title;
* o, q& D6 F n) L( c0 y4 r - $my_post['post_content'] = $content;& |- Q: Z- }' _+ }2 P' b& u
- $my_post['post_status'] = 'publish';
% x* C) x$ d; @7 B - $my_post['post_author'] = 1; k- e7 ], O2 S8 G: A
- $my_post['post_category'] = array($cate);& k, @0 }* i- i. R. ^
- $my_post['tags_input'] = $tags;- ]% y2 ?6 p! D- G
- //$my_post['tags_input'] = array('tag1', 'tag2');
+ }9 w. I1 t5 m5 C7 ] - //$my_post['post_status'] = 'future';
& h! ~! S, u5 E( W& P4 R, a, O' R - ////$my_post['post_date'] = '2010-07-04 16:20:03';
5 ]( S0 t& F) `2 n. }6 |& H; F
$ A# ?( y4 e4 z' f* O3 f- // Insert the post into the database
$ G V# h {& u - $ret = wp_insert_post( $my_post );% Z3 ^- O( p& |, _( i2 j5 o
- echo $ret;
( v7 A7 ?" t7 {$ F5 z* G( v - ?>
2 o6 x- x. n6 o* R0 r. s1 U
复制代码 我自己写的3 N$ c) q, S m7 g# \$ D1 i8 X* \
插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|