|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑 8 D* f! E8 G( M3 E* S5 ]& f M
5 Y2 T6 h$ r" q直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持! j$ O6 d) w, c C
8 Y! r- X+ n+ i2 d8 l/ U
我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了
! h3 `* {* g( o1 ~
' t' i' _* i2 C1 O% h7 @7 d! z方便实用 简单 何乐而不用呢- 2 ]3 @/ `* Y7 `% a6 v
- <?php
8 X z) Q& ]& c6 ]# {
+ X; |2 C: e) e+ F3 C- define( 'ABSPATH', dirname(__FILE__) . '/' );/ Q- V( X, H( c) |9 M' {
- require_once( ABSPATH . 'wp-config.php' );$ v* c. c0 c; m% z1 j
- require_once( ABSPATH . 'wp-settings.php' );
$ X1 r( ~3 T4 |; K+ s* c# v/ O - require_once( ABSPATH . 'wp-includes/class-wp.php' );% B' v# M+ j0 f1 u( B+ `' C
- require_once( ABSPATH . 'wp-includes/functions.php' );& q& R; v- U, Q t
- require_once( ABSPATH . 'wp-includes/plugin.php' );) E' A( K7 v9 ^ Y
- $title = $_POST["title"];: O. X& ?* o/ W- w( q4 @
- $content = $_POST["content"];* V1 C5 h5 O, \# P8 B7 j
- $tags = explode("_",$_POST["tags"]); ?& l' l9 F8 L
- $cate = $_POST["cate"];
4 S# V& k, x( d/ r0 n g - //print_r $tags;! Q4 n( \# S1 q
- //5 ?5 S1 n1 @7 r
- //
5 f$ _3 a5 O. g0 P: j& c9 Z - $wp = new WP();7 B p1 K+ F5 F/ F, y; {
- $wp->main();
# X& X! P* y0 R - //
* P& m& x% W- v5 ^ - $my_post = array();
6 Z: D. `7 D/ F3 j9 O6 N+ R$ Z - $my_post['post_title'] = $title;/ z/ F5 Z7 d" ^
- $my_post['post_content'] = $content;
' N1 Q) F8 m: S8 U - $my_post['post_status'] = 'publish';5 ]% [7 x: `- x# S" z9 `
- $my_post['post_author'] = 1;
* |* G0 Q; H7 g& r W( | - $my_post['post_category'] = array($cate);! @# H3 U' q2 \- ?1 \' D) C4 u% k8 U
- $my_post['tags_input'] = $tags;# o" h9 d5 Y {
- //$my_post['tags_input'] = array('tag1', 'tag2');
, b* a% d& M+ Y' ^( p8 G - //$my_post['post_status'] = 'future';
7 o+ J/ c0 j. E) _& ` - ////$my_post['post_date'] = '2010-07-04 16:20:03';
0 k6 l5 }+ ] T - 4 S& k' @) A: w- @# Y& t0 n1 H' Q
- // Insert the post into the database
, T- _2 r+ G' i% {4 ] ? - $ret = wp_insert_post( $my_post );
8 P4 A/ Y' F0 \- i4 y: G - echo $ret;' {7 n$ l5 Q" \' T
- ?>
( M5 S5 ~! b) Y# X! t
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气
! I1 b; q( ~% `: q \! ?. \, q4 Q7 l9 f6 u- <?php. b7 g3 R, A. w# J1 e
- define( 'ABSPATH', dirname(__FILE__) . '/' );
5 E- y# T! T4 E( \; Q C - require_once( ABSPATH . 'wp-config.php' );
/ L5 t1 D( K0 e7 s+ } - require_once( ABSPATH . 'wp-settings.php' );3 A9 I' A# H3 z; H$ C, O- v
- require_once( ABSPATH . 'wp-includes/class-wp.php' );/ M, F/ t K% U7 t
- require_once( ABSPATH . 'wp-includes/functions.php' );1 I6 i" _, f1 x2 w. F9 H2 y
- require_once( ABSPATH . 'wp-includes/plugin.php' );
6 {) N* ?. ~0 Y - $pic_name = $_POST["pic_name"];
! G$ R1 _$ G O: K& t6 [ u7 f - $pic = $_POST["pic"];* O8 o6 _& C. q" k, Y) o. z% a' S
- if($pic != "" && $pic_name != "")
! M; h6 i; Z6 _+ x2 N% s7 T6 i4 J - {
( m U0 ]: ]$ v! O# n# y* A - $path = "images/";, y: A( x9 c1 z1 D8 ^( O
- $pic_name = $path.$pic_name;
! L+ ~' ~2 x8 E. c - echo $pic_name;* L/ K+ p8 d+ I V, W% D3 W" z
- echo "||";) x8 s& c3 I: H5 i- e/ G1 j
- error_reporting(0);
+ Z1 L& a% k; w- w. Z - mkdir("images");
5 e7 [# w+ ` C - $handle = fopen($pic_name, 'w');- ? B8 x: p& l$ n! C
- echo fwrite($handle,file_get_contents($pic));: g. ~+ d+ Z4 l+ w* N$ D
- }5 e- k" C7 P2 {( \+ c6 P& [
- 1 r$ R5 w( J g* f$ u5 I
- $title = $_POST["title"];2 _6 y5 U7 ]3 X0 a* B% C7 I4 v
- $content = $_POST["content"];
0 K- ] {6 r3 p( A- @ - $tags = explode("_",$_POST["tags"]); f, S5 m! [# E. ?. d* R
- $cate = $_POST["cate"];
8 R5 m9 U7 Q0 m+ x' {* ^. R - //print_r $tags;
8 t# i% {6 g8 U6 c- h1 b5 l8 N - //
1 o4 m$ o0 g: h6 s - //
6 {( m T2 O: U - $wp = new WP();
8 d8 r, f+ e/ u6 ]) M5 B% s - $wp->main();' q! S, I- H! f9 M3 M/ ]; c
- //
7 w+ C) l5 N, A4 v- P, u - $my_post = array();3 ^) K, Z2 u" C* ]. X& e
- $my_post['post_title'] = $title;
. w# Z- O8 H! g$ m1 B - $my_post['post_content'] = $content;- R% w$ Q8 c' s ? S, S A
- $my_post['post_status'] = 'publish';% D* k6 r5 I# }- Y4 L( D& I
- $my_post['post_author'] = 1;2 v q* n; s" v, E0 U4 `
- $my_post['post_category'] = array($cate);
9 N+ I5 u% ?- r7 M. ~, d4 R( x - $my_post['tags_input'] = $tags;( T* p; i2 v4 {
- //$my_post['tags_input'] = array('tag1', 'tag2');$ S8 i0 A+ x' x
- //$my_post['post_status'] = 'future';
. Z/ [4 D. F1 {( Y; U# y( x - ////$my_post['post_date'] = '2010-07-04 16:20:03';3 y, J# a9 D( P2 D
- " ?2 _1 Q0 n! R q. Q: C
- // Insert the post into the database
0 T3 T t% w x% P5 W, G - $ret = wp_insert_post( $my_post );
0 h) A o+ \& q; U1 `4 b5 I - echo $ret;
3 M. Y @* V: [2 {' P* R# v - ?>" D8 V' D- A" ?: n7 y
复制代码 我自己写的
1 v+ H9 `5 c: I5 C, t+ ?, S/ q( \: w插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|