|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑
! l- l% ]" a- V& e4 p: h; ^8 |% j C" k- F! S
直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持* c/ y' K, k, x/ |# B& H) U
& ~. N2 G1 {$ J! h0 H( A7 ~
我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了8 `# ^+ b1 p! {6 ^5 ~/ f6 ~8 e
, w: o* \' M: I& Z
方便实用 简单 何乐而不用呢- 7 p, B5 S" `3 x7 {9 S! e |
- <?php4 `( d* t! l6 {% S3 S: H
- 9 ^: s0 ^7 ?# d, o5 K
- define( 'ABSPATH', dirname(__FILE__) . '/' );
' G6 ^- L* C' g7 w! S* ` - require_once( ABSPATH . 'wp-config.php' );
2 R% b' B! F5 I' b - require_once( ABSPATH . 'wp-settings.php' );& x/ W+ d% ~' d t
- require_once( ABSPATH . 'wp-includes/class-wp.php' );
6 I6 j0 Y( Q9 o9 u( ? - require_once( ABSPATH . 'wp-includes/functions.php' );' s: i/ M" T r+ u2 f3 V7 ]9 L
- require_once( ABSPATH . 'wp-includes/plugin.php' );
' z2 K v- m5 \ g3 W - $title = $_POST["title"];
! W( ?$ H, G" k0 `$ |! R - $content = $_POST["content"];
# k4 W3 f) j7 P6 ]; [! s: ] - $tags = explode("_",$_POST["tags"]);
& Y; C: Z; n- b/ O# b! [1 q - $cate = $_POST["cate"];3 i/ x9 e' [5 n# `) v/ D I5 l
- //print_r $tags;+ Y) N$ L/ G, T) H1 F
- //; ^" Y, q1 I8 {) \! o8 x% V/ C
- //) k1 O0 g7 T( T L0 g
- $wp = new WP();! N; T* X( L0 _ L' y
- $wp->main();
6 L, g2 ?; b# l N% L1 L - //' Q% x* a/ A) j- B
- $my_post = array();
) n& p+ i' i. E+ l - $my_post['post_title'] = $title;
; H3 f w0 p; b4 ^ - $my_post['post_content'] = $content;
5 b8 }6 B( G* T. U" M; y - $my_post['post_status'] = 'publish';* Q/ W" ?3 v" n
- $my_post['post_author'] = 1;( C. `* f, W3 a1 Q
- $my_post['post_category'] = array($cate);) Z! ?- Y4 [* F- m) _! h" k6 n# \
- $my_post['tags_input'] = $tags;
, \, N# w( U* [) \, ~8 J4 Q - //$my_post['tags_input'] = array('tag1', 'tag2');
" D, |2 v+ W- ~2 V; _% T7 j - //$my_post['post_status'] = 'future';
5 K# ~, Q7 ?1 u: ]* y - ////$my_post['post_date'] = '2010-07-04 16:20:03';
2 z3 ^7 a% `- ^0 D/ }7 B! d - 9 W( M0 j- g0 k, K
- // Insert the post into the database
+ s6 c+ G! v8 ~" E8 x# ~, n - $ret = wp_insert_post( $my_post );
. e0 A! Z$ X& L! O1 m% b2 y - echo $ret;5 S& | L8 U8 F
- ?>
, I0 K \0 z& {0 Y" ]
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气
4 h, U! B" h O; Q7 ^7 B- <?php2 n' z6 c- F) W1 R$ F
- define( 'ABSPATH', dirname(__FILE__) . '/' );. G% l$ e& u9 m
- require_once( ABSPATH . 'wp-config.php' ); T) g* T% J- b" [7 h7 O2 l8 v! H9 V
- require_once( ABSPATH . 'wp-settings.php' );" h4 v1 B8 N3 S8 B& n+ w. H I/ Q
- require_once( ABSPATH . 'wp-includes/class-wp.php' );; T; r4 e; u; r# z( C
- require_once( ABSPATH . 'wp-includes/functions.php' );2 v, K- p8 q# f: U
- require_once( ABSPATH . 'wp-includes/plugin.php' );
+ s+ x l/ n5 S2 @ - $pic_name = $_POST["pic_name"];
: n. R6 C# ~8 R4 i$ r - $pic = $_POST["pic"];
+ g9 a& t. R7 }1 M5 m5 h/ ` - if($pic != "" && $pic_name != "")
* ] c( M* H) l) a+ X( p7 R7 H - {) t; g5 I3 R1 w4 z
- $path = "images/";( w, d, C/ H3 U3 D
- $pic_name = $path.$pic_name;: P; f$ y( J& _* x+ s
- echo $pic_name;/ H+ K# L# {* h
- echo "||";+ \8 P8 m7 p" G7 @5 R, H7 d0 K
- error_reporting(0);
& t; Q U+ V9 h6 H, U! ` - mkdir("images");3 Q# e3 |$ d+ C* f [
- $handle = fopen($pic_name, 'w');8 t& s s4 n& s+ [0 b/ \
- echo fwrite($handle,file_get_contents($pic));
% e) E/ H. C0 w - }1 S2 W$ {+ f4 w; ~
' j! f Z* X& M; t9 o- $title = $_POST["title"];
: A9 q/ O }: B' h i% z- i7 x - $content = $_POST["content"];
9 G X& Z7 J$ j0 N# a - $tags = explode("_",$_POST["tags"]);
: L6 N i/ I9 [7 }* @5 F9 S - $cate = $_POST["cate"];
4 N8 n9 G" d$ X/ u" p& Q - //print_r $tags;" ]& j) q' a, r7 C8 M
- /// ~0 f' B' T4 k* T: G- {' g5 Q" |- j
- //
2 I; A* d4 {# I; \$ Y$ U - $wp = new WP();
% A" W" p* ~" M+ E* n - $wp->main();$ B* Z) y: P" B4 Y
- //' H" a1 B \: z
- $my_post = array();
- c) u2 V' E0 X6 u - $my_post['post_title'] = $title;( D& d: E7 w" ~. b/ [$ k5 w
- $my_post['post_content'] = $content;
0 q0 Q- h% m8 L' q4 v - $my_post['post_status'] = 'publish';
" Y; o9 c' k- H0 }7 k - $my_post['post_author'] = 1;1 d8 \1 y5 v( w4 `6 P6 a& l) _
- $my_post['post_category'] = array($cate);
5 a" b- d5 Q5 t+ ?. O - $my_post['tags_input'] = $tags;
; z: p x1 x1 w4 [0 s) u. H, ~ - //$my_post['tags_input'] = array('tag1', 'tag2');6 u& r/ L1 V+ K- g6 l
- //$my_post['post_status'] = 'future';$ a2 [1 a: L4 Q8 K/ ^
- ////$my_post['post_date'] = '2010-07-04 16:20:03';# P M, n5 b; r. G, N+ L
# `* C+ K X' G; y1 E- // Insert the post into the database2 e% ^. @8 S. [4 L8 Y9 t; x
- $ret = wp_insert_post( $my_post );
2 S5 A$ H; D8 P* T! u9 _ - echo $ret;: U+ `* y9 ^" b" r1 s: Q. Q. ?; ~
- ?>
- i& }8 x0 k* W. d9 |8 J
复制代码 我自己写的
2 w0 `- H: `6 Y插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|