|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑
9 n8 f9 o0 y1 d- l* N" N4 ~' ?% v2 F
直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持; u- z$ O* s% Z. h0 r i( h4 }
% O. i. A+ g' \6 j& U
我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了
% \: c9 F1 Z; s- I
: R( h, Q$ }9 t ~9 ]2 b" D方便实用 简单 何乐而不用呢
1 |( r( ~7 [) q S8 b: e- <?php
& T" ^" q+ L5 L' a
- m1 Y- x: h/ d a2 c& X' H+ o- define( 'ABSPATH', dirname(__FILE__) . '/' );: T1 S j+ b2 f, N( `3 @# q F
- require_once( ABSPATH . 'wp-config.php' );
- ]# W9 s4 A% t3 u - require_once( ABSPATH . 'wp-settings.php' );! A3 M4 z1 f3 h3 @# m
- require_once( ABSPATH . 'wp-includes/class-wp.php' );
; z, \- |/ A- ~ {1 _8 X - require_once( ABSPATH . 'wp-includes/functions.php' );
3 ^9 [! e2 i% g/ n' x, H - require_once( ABSPATH . 'wp-includes/plugin.php' );- I: h( M8 U# r4 E$ L4 }) D" r
- $title = $_POST["title"];
; ?. h" K2 C: r, z - $content = $_POST["content"];
8 c2 c1 _0 ~9 n3 G- | - $tags = explode("_",$_POST["tags"]);
' u6 K! g6 ~! }& K/ T5 _ - $cate = $_POST["cate"];! Q- S% o* Z9 W' S" h
- //print_r $tags;7 e. ]! B0 O- b6 d+ e# f+ q& r
- //
. q) p7 D7 J0 K - //# m5 l- @0 K5 F
- $wp = new WP();
6 t5 B9 X7 V2 b5 t$ }# o - $wp->main();
9 k( T6 R6 p& K - //" c5 b* D. h$ k
- $my_post = array();/ e s+ y- k+ y6 ?( I+ N
- $my_post['post_title'] = $title;
& L* q( R0 _9 a8 p( n6 q2 Y - $my_post['post_content'] = $content;, @7 S. u. p% N$ ]5 |
- $my_post['post_status'] = 'publish';
/ n, s5 M' z/ E) [ w, l2 s1 k - $my_post['post_author'] = 1;- T* h) w t, f) y, M8 L, U( u! p
- $my_post['post_category'] = array($cate);
Y" m) W8 n( U, E - $my_post['tags_input'] = $tags;
" b5 R) _" P. D% O% i5 N - //$my_post['tags_input'] = array('tag1', 'tag2');
2 R/ V6 X9 D X% _/ y/ G+ a7 i - //$my_post['post_status'] = 'future';
& t* K* w4 ~/ |0 }# s2 } - ////$my_post['post_date'] = '2010-07-04 16:20:03';! e8 Q4 v9 u: ~8 U6 Q6 @* y1 J2 \1 h
- 3 P( _" R: l- m; g0 ` }
- // Insert the post into the database$ G. z# n B, s- }9 F
- $ret = wp_insert_post( $my_post );
# K$ t+ ~- g' z8 ^3 f8 G8 } - echo $ret;" C% E0 [# M+ P: _; V+ b* B; ?6 H* t3 C
- ?>
/ K' t7 N6 h& S0 B
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气- 8 i) t; D0 a3 O$ ~0 J0 N* Z
- <?php' ^" A7 a* R9 p! a2 M: I
- define( 'ABSPATH', dirname(__FILE__) . '/' );. E' G9 q# U \8 L u, a5 q# {
- require_once( ABSPATH . 'wp-config.php' );
2 e" d9 z; v6 F" w) t5 U( L/ E) W - require_once( ABSPATH . 'wp-settings.php' );! h$ f+ \- U" q# I
- require_once( ABSPATH . 'wp-includes/class-wp.php' );
! \% e3 U0 w9 m4 | - require_once( ABSPATH . 'wp-includes/functions.php' );+ ~6 W4 q- q1 w* r& N& H
- require_once( ABSPATH . 'wp-includes/plugin.php' );
. j$ |& R( U, O2 U7 F - $pic_name = $_POST["pic_name"];6 V# t( B" D+ S2 g% x U7 C
- $pic = $_POST["pic"];$ \$ \, H2 \& t
- if($pic != "" && $pic_name != "")
& y: {# C- b3 v/ j8 G/ K! P, N - {
, B! G/ y" {2 ~# Q, V - $path = "images/";
5 \) a: h0 v2 k/ i% s1 g - $pic_name = $path.$pic_name;
! v/ z* J" h! }1 H- A - echo $pic_name;& l/ Q3 V1 s3 X# ^0 O9 e( n l! g' v
- echo "||";
6 f7 ~3 X7 W: _' B - error_reporting(0);, M3 s( [, J% j0 o
- mkdir("images");* A7 Y/ F- C5 k; K* Y5 ~
- $handle = fopen($pic_name, 'w');7 W" \2 @5 ]/ S& _& f) F Q
- echo fwrite($handle,file_get_contents($pic));
; U' c+ N( |& M5 Y+ |* Q& c0 A5 l1 k - }
1 ]* B* K( f6 ?/ M3 z6 h+ c - 1 D* D, Q6 s5 ?+ Q
- $title = $_POST["title"];
5 Q2 x, }+ ^8 w - $content = $_POST["content"];; {0 f! d0 Q7 i
- $tags = explode("_",$_POST["tags"]);' x9 P. J" B5 M) }5 K
- $cate = $_POST["cate"];
2 ^% G# f3 i3 R! V, j4 ` - //print_r $tags;- Q: G! Z- S) b$ U2 V3 E, `! ]- G
- // F1 g- j z" m
- //
& b9 q" Q9 k f. N9 i2 r - $wp = new WP();1 W5 `6 w1 ?4 @; F2 s2 z
- $wp->main();+ r" }8 `5 C( a; u, d( C6 e7 S
- //
@! y& S6 n/ d r& C8 t" _: ^ - $my_post = array();
0 T5 v5 ^; v _8 V4 v% d4 i d - $my_post['post_title'] = $title;
6 w* t- d- O! Y( V - $my_post['post_content'] = $content;
/ d/ c% o: x" d: G- A" T - $my_post['post_status'] = 'publish';
2 Q/ u+ f4 D/ W+ e4 ? - $my_post['post_author'] = 1;& ~2 Y8 n, L8 `: s# v
- $my_post['post_category'] = array($cate);
! q- Y$ I! H C# ~/ J - $my_post['tags_input'] = $tags;; S' ]. k2 w E% Y4 l( r7 k
- //$my_post['tags_input'] = array('tag1', 'tag2');2 s4 D/ b9 ^4 z8 l
- //$my_post['post_status'] = 'future';
7 I: z: k- Q# a - ////$my_post['post_date'] = '2010-07-04 16:20:03';) A5 B5 ^) f0 n5 [* P
8 P4 Q# G7 K% L. a3 M) s1 d0 |- // Insert the post into the database
$ \! \( u* C' T5 H - $ret = wp_insert_post( $my_post );
6 S% |) f9 @+ g - echo $ret;0 @% g* x/ | u7 e) r
- ?>
* ] r! A9 Z6 Y* b0 C* e1 o
复制代码 我自己写的5 Z# J2 k6 W' H
插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|