|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑 5 H0 s e1 R k3 u8 I R- { v. u
# J( j$ Z% w' [! L4 l$ `直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持9 x7 J* I# O9 M8 C+ K
6 B7 p% e& j2 U, j+ F! d我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了
! `; a2 L! p& E+ k B; J7 s5 u3 C2 n, i' y- @
方便实用 简单 何乐而不用呢- 5 ^+ F B. t) C1 m2 b! D& \ y
- <?php
+ n+ s# V; x+ |" I- F' T. A3 s - 1 ~( L" v7 O6 L& L. P
- define( 'ABSPATH', dirname(__FILE__) . '/' );
7 Q, {/ }$ Y' e) s/ @ - require_once( ABSPATH . 'wp-config.php' );2 D: S! y8 C2 ~/ b6 u1 s/ Q
- require_once( ABSPATH . 'wp-settings.php' );4 a1 f H: V, ?9 F# Y
- require_once( ABSPATH . 'wp-includes/class-wp.php' );
, z( B& c# `* W9 i5 g - require_once( ABSPATH . 'wp-includes/functions.php' );+ s* ^5 P; ~6 I- _9 K3 H8 z
- require_once( ABSPATH . 'wp-includes/plugin.php' );5 {% Q1 u$ v7 T6 n6 ~
- $title = $_POST["title"];
+ x$ z( q+ V3 v# [2 j7 t - $content = $_POST["content"];9 J; P+ p( n3 X4 e) K
- $tags = explode("_",$_POST["tags"]);# K+ G9 U* l# s$ B9 u& r
- $cate = $_POST["cate"];6 D- B6 h0 h% v+ u) g
- //print_r $tags;
. t( D, f% c/ v" O0 ~( Y& V - //
: a; _9 D$ G% s H - //, q5 v/ U* F2 S& j/ y" j( S
- $wp = new WP();4 x+ _" T% n5 U' O& N6 O
- $wp->main();
/ o; K e ?" I& E" X - //
/ @7 G- Y6 Z7 j8 p {9 c7 y - $my_post = array();
5 Y% ?7 @2 n, Q* l8 k' z" h - $my_post['post_title'] = $title;% a7 C. v# J- ]! w% ~
- $my_post['post_content'] = $content;9 `1 K. [# [) w# S
- $my_post['post_status'] = 'publish';
7 S/ e7 @: d" i2 x; v+ C: I* I - $my_post['post_author'] = 1;. r A- J8 v, Y1 _2 \
- $my_post['post_category'] = array($cate);
0 r0 y" ^ E6 n, b - $my_post['tags_input'] = $tags;
: S. f) b5 F7 ]4 B2 d! c5 M" [6 Q9 b, C/ W - //$my_post['tags_input'] = array('tag1', 'tag2');
, R% p0 O7 ]" A; P, m5 N- o - //$my_post['post_status'] = 'future';* [+ ^7 Y! ]6 e
- ////$my_post['post_date'] = '2010-07-04 16:20:03';, x; ~ U k8 e1 R5 y$ `9 }* W
- $ z" P# Y. @7 E% l6 |
- // Insert the post into the database/ J! u+ D# \$ B: d9 ]9 \) \' N
- $ret = wp_insert_post( $my_post );
! l9 K; R" R' r" }! g0 Z# Z n - echo $ret;$ n' Z7 W2 V2 ]& M/ Y
- ?>
6 a0 N% O5 a# B1 V* h( w
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气- + C. e# N$ x5 }8 P
- <?php
! G( l+ S7 u, @4 w& a e5 v - define( 'ABSPATH', dirname(__FILE__) . '/' );% S: z7 E8 B9 K. W7 e: ^
- require_once( ABSPATH . 'wp-config.php' );
; p: T% g3 V; U2 M+ G- k - require_once( ABSPATH . 'wp-settings.php' );# J8 L, k: l0 y; L: p( k* }
- require_once( ABSPATH . 'wp-includes/class-wp.php' );. d5 P& ?4 O/ n0 ~8 m' M! W
- require_once( ABSPATH . 'wp-includes/functions.php' );2 d0 y$ ~7 I6 H) P+ ]# T
- require_once( ABSPATH . 'wp-includes/plugin.php' );
, f9 A7 G/ f- F6 g( V' c, H - $pic_name = $_POST["pic_name"];
" u, b7 L0 c5 r% N" \# c- f - $pic = $_POST["pic"];1 }1 s/ y0 y d E- D# P3 Q* E
- if($pic != "" && $pic_name != "")
$ `3 h4 ^; G( A4 N8 g - {
: _+ K* z' _5 {) M. A P - $path = "images/";
: {; J' G: D0 H# U. g0 [ - $pic_name = $path.$pic_name;
2 S, R, H3 @4 O. _ - echo $pic_name;
i5 X. _ e% p4 _( P# P - echo "||";
$ S f2 O. y; I m _4 K - error_reporting(0);, r8 q. L! V: b, c' s- [
- mkdir("images");3 I( ~# D- e% o9 ?" D
- $handle = fopen($pic_name, 'w');, O7 ^1 u: }7 A! i1 S! z% d
- echo fwrite($handle,file_get_contents($pic)); e& L0 b3 x1 e3 A
- }" f+ f- H& s. x% }
4 p: A& J3 b9 `# J* s& y/ ]- $title = $_POST["title"];3 k! {9 Q }$ ?3 \. a1 _4 I& j
- $content = $_POST["content"];
; Y- |+ Z) x0 {- K4 p - $tags = explode("_",$_POST["tags"]);
) u* S9 F7 B4 }5 w* i - $cate = $_POST["cate"];: ?0 A" H0 [# o' k3 O; u
- //print_r $tags;
) }+ W: R" D) s1 m/ {3 X - // D( L; x) }7 S9 l' i
- //) v+ X$ B- Y9 N# L. {! s
- $wp = new WP();
3 Q: j: ?7 p3 T2 y9 a8 |7 W; a - $wp->main(); w9 u) J9 T: @5 z& |3 s
- //% Y( K# y5 \7 j' ]& J$ x1 ^
- $my_post = array();
% b# x& c+ |3 E# h5 u/ {) F( L - $my_post['post_title'] = $title;& K6 b, F% S4 _. ~( r) x
- $my_post['post_content'] = $content;
+ u. _% m S R8 {9 n - $my_post['post_status'] = 'publish';3 D. T7 p3 l9 {' `& X L. P
- $my_post['post_author'] = 1;
7 s5 f! M. M/ G) e# C/ l - $my_post['post_category'] = array($cate);
- K$ K: z" P i' r - $my_post['tags_input'] = $tags;
7 ~; X: A/ p8 c9 V6 h. v1 X% \. N2 f, X2 z - //$my_post['tags_input'] = array('tag1', 'tag2');
/ V0 W! N4 c7 Z7 a - //$my_post['post_status'] = 'future';& L e% ?8 w/ g9 e2 B
- ////$my_post['post_date'] = '2010-07-04 16:20:03';( V/ w/ P% N4 ?9 K" ?
5 L$ i6 }# `: q$ o9 ^0 Z! O' b- // Insert the post into the database- G! u! R* W: e' D
- $ret = wp_insert_post( $my_post );
! k/ W/ Y8 i) f/ T7 t - echo $ret;
?1 x8 K2 c: e# O. d+ B3 W - ?>3 K4 x# L" l6 k) k8 v k
复制代码 我自己写的" s. r' h3 t5 }1 s9 Q
插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|