|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑
7 O/ j9 S. U* v4 _8 R0 E( y9 p2 g
5 v- n0 d& k! @7 t& w直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持
7 b1 t. }8 g; M1 V; N( y" i4 D- D; V( g
我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了
( \9 H7 g C d9 q. ^' j' m, B1 z/ X1 ~: n5 I- M3 i" p
方便实用 简单 何乐而不用呢- 6 [( n! D5 g9 m% T( D
- <?php& X" K. U& C' W$ w/ [1 I" W5 `
- " `! ~1 o# W1 x/ r; P3 S+ {
- define( 'ABSPATH', dirname(__FILE__) . '/' );; _; S# X; o. n
- require_once( ABSPATH . 'wp-config.php' );
3 L- V0 E( L" O$ ]/ S4 b, I - require_once( ABSPATH . 'wp-settings.php' );
) d# Y) H o, N$ y' i - require_once( ABSPATH . 'wp-includes/class-wp.php' );5 ]) u: d/ H9 @2 A" E7 u- C
- require_once( ABSPATH . 'wp-includes/functions.php' );
: n" M# B! H0 {; g - require_once( ABSPATH . 'wp-includes/plugin.php' );9 `& a( [+ C! s. B
- $title = $_POST["title"];
( D" _, u: d e0 ]- [ - $content = $_POST["content"];
! b7 T3 q% K. J" A! g7 B6 g - $tags = explode("_",$_POST["tags"]);9 Z+ l; q' O# R# l6 C/ R4 I
- $cate = $_POST["cate"];
( g3 A- e; X: ~8 b. M0 l2 w0 M4 F - //print_r $tags;; f2 g5 V) _) B( E. E
- //
/ E( T3 ]7 Y4 R8 V Q, H+ X% d2 f - //
) v n/ W5 J `& M1 D- B8 ?$ m - $wp = new WP();( e6 J5 t3 S; m; u {3 F
- $wp->main();
$ F6 ]/ f) T" V: m, ^9 {4 g4 Y, L - //
8 Z8 j4 A! T9 s+ J8 n* N' A - $my_post = array();: o1 e. n/ o" ]0 [: d9 c, t
- $my_post['post_title'] = $title; P1 D" t/ o- n1 T( s* g8 l+ s
- $my_post['post_content'] = $content;
- T8 A' L7 m# U! z - $my_post['post_status'] = 'publish';
/ p1 C9 S* Q) b' k; U) \- M( S - $my_post['post_author'] = 1;
! p- q( h! L' a6 U. U4 Y( J - $my_post['post_category'] = array($cate);
, ?3 N; W9 U% p - $my_post['tags_input'] = $tags;
5 Q$ v# l5 F( U4 U - //$my_post['tags_input'] = array('tag1', 'tag2');0 ?& C0 u7 W) U3 o6 w
- //$my_post['post_status'] = 'future';
, }% ~& [! C. J6 u1 ^ - ////$my_post['post_date'] = '2010-07-04 16:20:03';
" v& r7 s+ [. Z' F$ O% H+ n d - ! f9 d6 v* a; @" `2 Z2 Y2 t- G
- // Insert the post into the database
- o2 f0 a0 L4 e6 _1 H - $ret = wp_insert_post( $my_post );
; T6 {, j$ H3 ~6 C0 g" Z - echo $ret;
( [% N( D/ R, I6 C% B; o$ Q - ?>
* {$ [- v. O+ A: T
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气- 4 ^% ^& r! b) T( D1 ^; c/ E
- <?php( _! B" i4 g+ w: w
- define( 'ABSPATH', dirname(__FILE__) . '/' );' o2 i4 o( n- r8 \0 e2 T, _+ B
- require_once( ABSPATH . 'wp-config.php' );
1 ~) z" Z. l( _9 t* ] b - require_once( ABSPATH . 'wp-settings.php' );$ t9 A0 C' Z, I) t* @$ ^; n
- require_once( ABSPATH . 'wp-includes/class-wp.php' );
# o% f/ z0 V" ~3 ? - require_once( ABSPATH . 'wp-includes/functions.php' );
/ t# y* y8 \2 w9 G' I9 o - require_once( ABSPATH . 'wp-includes/plugin.php' );& R4 i* I( m, U' |# s5 \ n `1 B
- $pic_name = $_POST["pic_name"];
6 m1 n1 e( g8 E# m5 L2 a" M/ t - $pic = $_POST["pic"];& T; E8 B' T% q9 r" I* W% O
- if($pic != "" && $pic_name != "")' P m. j8 C# v$ W. \
- {
3 x# }5 h7 D2 O, F - $path = "images/";8 P0 ~6 m9 ~5 z) M: B; g
- $pic_name = $path.$pic_name;5 E$ T1 q1 w( r D! ~; L) X
- echo $pic_name;
6 Z0 y! W5 d& G" C/ Q/ u9 f - echo "||";0 C& Y/ j( _" C% H6 q# i
- error_reporting(0);
. p4 h- x- J) K# p7 c - mkdir("images");
9 v ^' Q( w# w ?+ o$ a - $handle = fopen($pic_name, 'w');9 } n* T! Z5 e
- echo fwrite($handle,file_get_contents($pic));
% \+ t/ \; W; i9 e* Z/ y1 N' d - }
/ J' R7 N+ O9 f! s9 j
1 Q' Y8 m- R( F( Q- $title = $_POST["title"];/ t8 O: `" U6 B9 i$ f! |' e" G
- $content = $_POST["content"];
$ s D+ t9 d4 ]2 k" g - $tags = explode("_",$_POST["tags"]);
3 I$ i6 u3 Z+ w7 G1 h! O9 ?; ~* X* r - $cate = $_POST["cate"];
& J' A, E: P8 J/ ^7 R - //print_r $tags;" A2 @- |3 B' N% Z
- //
% }1 p4 v; n+ _: r - /// n6 x; {: R4 G A. ]. H
- $wp = new WP();
+ R4 @. A8 O! {9 F - $wp->main();9 `( F4 p, e' V0 ?4 b
- //2 U. A4 Q# ?) M j; e
- $my_post = array();
) `0 {- B" B/ ]& ^5 {& L - $my_post['post_title'] = $title;
1 m% b' W! W* A3 \9 u+ i5 } - $my_post['post_content'] = $content;4 u4 \/ M( U& g7 H4 s9 A' O
- $my_post['post_status'] = 'publish';3 B1 V3 f% X( P8 T3 w
- $my_post['post_author'] = 1;/ b) N* C: s2 } H6 k
- $my_post['post_category'] = array($cate);
; r6 I5 b! r8 l2 e2 y$ I - $my_post['tags_input'] = $tags;
! m, I6 r0 i: s+ g8 ?$ i8 n' ~ - //$my_post['tags_input'] = array('tag1', 'tag2');4 J1 H& F0 U5 n6 A# h N
- //$my_post['post_status'] = 'future';$ e- p+ ~5 a0 ^' D% g5 O' c$ ]
- ////$my_post['post_date'] = '2010-07-04 16:20:03';
" j) y4 A" G7 j- L' `5 y - ) G1 S7 m0 V# W: ^/ F6 k5 D$ C+ p
- // Insert the post into the database+ m0 h# `2 `2 H: t, T# e: O' H
- $ret = wp_insert_post( $my_post );
7 s4 C' ]/ Z8 v Q - echo $ret;5 X+ a! ]' L+ G
- ?>
+ P& W- M& t( `& @, H- a5 }' J
复制代码 我自己写的% H% r- z2 x* {3 n, ~
插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|