|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑 ; \6 Q- Q% k+ V( m5 R' K# C
4 l& w2 ~1 T+ c+ n直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持4 U/ H( o9 B$ g8 ^# j
4 }0 K; y9 G9 d
我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了
: p3 L; [( Z9 H, m
. I+ I4 X) I# N7 U3 m5 U& o6 Y方便实用 简单 何乐而不用呢
3 R! X- \ |$ e' Z7 h7 F( x% c- <?php6 h R n7 U& T* `- V2 Z
) K F; b& P- k% a& F6 g- define( 'ABSPATH', dirname(__FILE__) . '/' );
! F) J) E; K1 I- J/ Y5 I# }- h - require_once( ABSPATH . 'wp-config.php' );
8 b! c" }9 F7 L+ | - require_once( ABSPATH . 'wp-settings.php' );( k! S D+ a4 M5 \" r k' n, e# f
- require_once( ABSPATH . 'wp-includes/class-wp.php' );! B8 E- v/ l2 w
- require_once( ABSPATH . 'wp-includes/functions.php' );5 C+ |0 ~" T1 `
- require_once( ABSPATH . 'wp-includes/plugin.php' );' d9 F7 v5 }1 z; J! E7 I: L" q
- $title = $_POST["title"];* g( {4 G3 R' O- A" `+ J" i
- $content = $_POST["content"];3 Z* _7 A3 N; K8 V7 G+ k) D5 y& c: ^
- $tags = explode("_",$_POST["tags"]);
- }" S2 z* c$ d) @. i - $cate = $_POST["cate"];
8 q4 B6 I# N8 o L: K - //print_r $tags;
$ r: t3 s4 d8 K. X' {% @ - //( ^, D* }, O- Z( D3 I( _3 A* ~
- //
8 t8 Y8 Y! Z4 K/ M. J7 O) w - $wp = new WP();
& a6 M: Q4 T6 C7 x - $wp->main();
% m8 V9 u1 X6 P7 N* W - //
, @3 f( n2 ]8 ]" N+ X" [ - $my_post = array();
1 o2 U- B: W8 J4 h) S+ w/ T* X' A - $my_post['post_title'] = $title;: ?3 }+ ?0 N. a5 f
- $my_post['post_content'] = $content;
. f& A+ I/ z1 M - $my_post['post_status'] = 'publish';5 n- X) Q0 V8 ^3 Y
- $my_post['post_author'] = 1;
+ H! I8 q7 t8 I8 e3 s$ i4 \ - $my_post['post_category'] = array($cate);+ ^/ v/ e) J! G$ `2 @ @7 }
- $my_post['tags_input'] = $tags;9 E$ _, A9 M- X: B) Q
- //$my_post['tags_input'] = array('tag1', 'tag2');
8 M9 l. z7 i L, ? - //$my_post['post_status'] = 'future';
8 U+ Y8 M7 Z9 ?8 ?/ E" X5 W5 ^ - ////$my_post['post_date'] = '2010-07-04 16:20:03';
5 ^/ R/ z, `4 |
+ x' f: `* t4 v" K, z# y- // Insert the post into the database. H% L' c/ E" C' g
- $ret = wp_insert_post( $my_post );
}7 k0 u+ q7 d - echo $ret;. D1 c4 S. e# d
- ?>/ d! k, |) Q3 F& h9 A
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气
2 f9 e3 I- D5 l: Y6 h/ J# X- <?php6 X, t5 G& n; t' z
- define( 'ABSPATH', dirname(__FILE__) . '/' );
" A, x' k; |7 l8 T- f - require_once( ABSPATH . 'wp-config.php' );- ?) ]. T' C0 n; [" h. W8 c
- require_once( ABSPATH . 'wp-settings.php' );: K8 I5 K5 F/ P2 p2 Z7 h, E
- require_once( ABSPATH . 'wp-includes/class-wp.php' );* O6 u$ s5 ], Y$ z8 l8 p/ G
- require_once( ABSPATH . 'wp-includes/functions.php' );! P4 S* a f+ J& \
- require_once( ABSPATH . 'wp-includes/plugin.php' );5 _: ^! k- _) J9 q
- $pic_name = $_POST["pic_name"];' T' b! X5 Y4 i, j; t6 M
- $pic = $_POST["pic"];# N4 Q( |- N( M. F
- if($pic != "" && $pic_name != "")
0 y) n! |% i# \$ j4 I - {& @3 x* t( I' e& g
- $path = "images/";+ Z+ a$ Z% i* j+ Q3 q) P
- $pic_name = $path.$pic_name;
$ P- f' {( i# ^( I& O - echo $pic_name;
1 U$ I# t7 I7 o6 s6 s - echo "||";1 u5 f8 E/ j3 l6 l/ M9 \4 {- j* Z
- error_reporting(0);
" X. C1 |' w9 M9 i( [9 x2 A+ S5 o. D - mkdir("images");
0 Y! i5 [" f" } - $handle = fopen($pic_name, 'w');
8 N3 e5 H9 E9 o& \7 c+ z - echo fwrite($handle,file_get_contents($pic));
+ S; n' B) Q+ Z- C- w - }. `. c: K( }% V2 E
- : _4 x" i: s& m9 C( O1 p
- $title = $_POST["title"];
4 B+ G; Q2 f/ \4 F: h$ D, ?1 K - $content = $_POST["content"];
' Y0 {6 _! n& @$ X; u - $tags = explode("_",$_POST["tags"]);
( P" m8 l1 Y' @8 ~2 W: U - $cate = $_POST["cate"];
2 f% o" E" C* G4 \ - //print_r $tags;
9 J1 u9 e3 d" `9 i4 y* v - //( u: e* k" L2 v# B7 ^, [9 W
- //
$ o& s9 D- l; Z$ M8 F$ _ - $wp = new WP(); N1 y. @$ K2 v' N* o: @6 K
- $wp->main();! S, D$ L. `* {" b
- //
3 e# F# [( U- ~5 ]0 f5 ] - $my_post = array();
3 O4 m R- x. Q$ @ _. Z - $my_post['post_title'] = $title;5 G. a4 W# ]7 N# O6 B4 Y+ c$ N8 L4 P
- $my_post['post_content'] = $content;# r6 `6 R' v. B; g/ p! r- O
- $my_post['post_status'] = 'publish';/ a0 ?! ~/ o& N: O& W" n
- $my_post['post_author'] = 1;
9 w0 ] {$ n* b- t) ? - $my_post['post_category'] = array($cate);* d9 r/ x+ [! |
- $my_post['tags_input'] = $tags;) R, S$ Q. v3 u& l1 F5 n8 h
- //$my_post['tags_input'] = array('tag1', 'tag2');) b) i# k' H3 _
- //$my_post['post_status'] = 'future';
6 N3 x5 ]& @. h2 ~; Z4 ~7 O+ I: g - ////$my_post['post_date'] = '2010-07-04 16:20:03';1 m) Y% x2 L! a; V1 a2 Z8 ?+ q
- 4 z! V) n: a- t, t0 e
- // Insert the post into the database$ o, l! s0 k1 {
- $ret = wp_insert_post( $my_post );
1 o0 g+ X* ]/ q* _9 u% | - echo $ret;
/ L2 b* [ b5 W3 N; q' g& I& _ - ?>% Y" W d- q( a) y, \! ]7 |9 Y
复制代码 我自己写的
3 E. g4 |7 g- E; m1 v) W插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|