本帖最后由 finder 于 2014-12-28 15:13 编辑
0 r. E' e' v. ^5 [4 F2 ?
, t7 h5 F `0 T4 L$ {1 q+ y 直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持. I, d& B5 [/ h* u$ w% |/ a
% a" ~8 e* V9 v 我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了( |7 H3 s7 Z1 R
' `/ P% A8 p: ~' I- X' `, m
方便实用 简单 何乐而不用呢
6 X& e+ |, z, {/ N <?php
( }$ N# m+ ~ d+ d! j 5 T. ^: ^* \& _, Q0 P
define( 'ABSPATH', dirname(__FILE__) . '/' );) [4 F- F' h" [3 I# B( J
require_once( ABSPATH . 'wp-config.php' );" E6 w" ~8 k8 ]% L& n, n R
require_once( ABSPATH . 'wp-settings.php' );& t1 d0 r z" {
require_once( ABSPATH . 'wp-includes/class-wp.php' );
3 Q) {& W/ T2 y6 I% N" M require_once( ABSPATH . 'wp-includes/functions.php' );9 @& K$ |% Q9 y% A
require_once( ABSPATH . 'wp-includes/plugin.php' );
6 L' w5 ?7 K1 x1 T( m $title = $_POST["title"];. Z# @* K& }' w4 U
$content = $_POST["content"];4 \' T- S* j" f3 ? J% X$ _
$tags = explode("_",$_POST["tags"]);7 p Y: L* u* S: I2 P2 P
$cate = $_POST["cate"];, r% L& B! [: q( H1 w! g
//print_r $tags;! b; [- M% {2 g* F( Z0 q) W% Z
//" K- y& `- \+ _. c6 A
//
7 j5 z( o# D2 ?) d2 y0 P; ^/ H# a $wp = new WP();
$ l* A/ g7 O5 R7 ?9 z: X6 \2 b $wp->main(); H7 }6 [2 m% j* h4 g3 E' {& r/ k
//8 P1 _+ }, l/ k: ]; T% w
$my_post = array();
) e# c* N. R5 {6 a5 N $my_post['post_title'] = $title;: |" Q; C% v' X; O- e1 D; K' q
$my_post['post_content'] = $content;* ?# d7 B3 J3 N6 R$ j+ S8 N
$my_post['post_status'] = 'publish';
: ]6 [, j; | ?/ E8 E0 i* q $my_post['post_author'] = 1;: S/ {" ^/ A8 w0 @/ v \5 z
$my_post['post_category'] = array($cate);
% b8 s/ i3 K* Q7 h5 ` $my_post['tags_input'] = $tags;
, \7 c- L6 o6 ] //$my_post['tags_input'] = array('tag1', 'tag2');# i; i0 _% k! U- t
//$my_post['post_status'] = 'future';
/ n- q- @/ e. W* [1 g ////$my_post['post_date'] = '2010-07-04 16:20:03';
( H) E) K( s4 i9 l ! Y2 o& p: s' l6 {' o& d
// Insert the post into the database
1 w$ L% s5 P7 q8 I- R# d* |, } $ret = wp_insert_post( $my_post );
: l( W/ F9 B5 i: j2 N echo $ret;
* Q! _( Z- i1 M6 H ?>+ B3 L+ O# L! n: Q9 u
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气% d2 \& M" D) k
<?php- ~7 }; Z% w! D* a: _. a8 `' }
define( 'ABSPATH', dirname(__FILE__) . '/' );3 c- w7 \! k6 G" ]/ Z
require_once( ABSPATH . 'wp-config.php' );8 f6 @" c# n% \5 ]" g8 L8 ^0 L! Y
require_once( ABSPATH . 'wp-settings.php' );4 z z% V G7 \6 J, j0 @5 Q
require_once( ABSPATH . 'wp-includes/class-wp.php' );: e4 q: H) L. n% z4 J( ?8 X% X' q
require_once( ABSPATH . 'wp-includes/functions.php' );
( Y$ U$ a) r9 ~) y* L$ v' y require_once( ABSPATH . 'wp-includes/plugin.php' );
9 A( ]! g5 {0 y! D $pic_name = $_POST["pic_name"];* T5 m( L" s# J0 t! h g
$pic = $_POST["pic"];
! ~2 N" ^4 J6 H/ |* J( K( t; m if($pic != "" && $pic_name != "")
: U# b3 D( h ]( ~5 X {
3 N/ F* f1 O: i $path = "images/";
% O+ a% ~1 o: m: ^2 G2 L& N2 D! w $pic_name = $path.$pic_name;
/ {7 P0 R: r8 J# B4 o; s echo $pic_name;. m- P* v; N, _ D2 ~& p8 ~
echo "||";
& I# {! ?' t* R! s) X error_reporting(0);- V0 O- Y# ?, k/ d P$ ?8 N0 ]
mkdir("images");5 E g& S* y7 o# z" z2 M6 L
$handle = fopen($pic_name, 'w');
3 Z: f g1 t! E. I! D8 Y echo fwrite($handle,file_get_contents($pic));, q# L1 |% T, q& Z! @. e4 K5 }; W: t
}* V2 _% _" i- n# @- o
* V2 _- Z' S# E) ` $title = $_POST["title"];9 h9 m; r' K- Z6 ^! O. n
$content = $_POST["content"];
7 l) C* @# B) T $tags = explode("_",$_POST["tags"]);
! N2 g+ `$ t' l6 @+ } $cate = $_POST["cate"];( I- H8 {; M9 C
//print_r $tags;
* x p. y' q! M1 d- g$ c, I w: N1 ? //
2 U: {: ^( w3 N. ~- S4 C% @! {( p //
$ Q" c% F3 U# A8 b: | $wp = new WP();0 q I- ]7 m6 j3 z* [5 R) T7 J
$wp->main();) k5 n: d! e& k( Q6 _$ X8 |
//6 \" j4 C# x9 H- `, X4 H% R
$my_post = array();. @# m+ L4 `/ [+ C$ j
$my_post['post_title'] = $title;6 B# b$ x8 L. \3 y
$my_post['post_content'] = $content; L( g% s5 d( y) u2 s; J
$my_post['post_status'] = 'publish';9 D' v# b5 d) t1 j' l+ u+ M. M9 f3 ]
$my_post['post_author'] = 1;: l# r1 g! \/ z# B# F- D, a
$my_post['post_category'] = array($cate);
. k) L/ _+ e6 E $my_post['tags_input'] = $tags;5 U' ?4 B: Q+ F: a; {/ u4 H% p7 o
//$my_post['tags_input'] = array('tag1', 'tag2');
5 `. e# o5 V% h: E3 L //$my_post['post_status'] = 'future'; _$ L8 X9 O; o: D5 b; R# X
////$my_post['post_date'] = '2010-07-04 16:20:03';
# n7 p1 H6 q, L. S, G* g( m% N e: Z; _ % E8 X0 m+ C4 w1 F
// Insert the post into the database
, W& w) i1 e* Y5 H6 \1 ` $ret = wp_insert_post( $my_post );. q4 z. h8 d6 S
echo $ret;
6 o( ~2 T! e+ y% f- L \) U ?>* ~& O, m1 Y5 ^
复制代码 我自己写的7 F6 o" }0 A8 i/ l7 H/ x) B
插入效果看图 不看广告看效果 已经测试过有需要的 拿去
评分
查看全部评分
相关帖子