|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑 7 U% F' R* m. Y4 N- Q, t
. x& j% c6 L' I5 z" I9 Y7 g
直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持" K- k& S8 B4 I9 ?3 ~
: V9 ~+ _0 M- E0 g* D我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了6 r& q! Z, O9 E, ]& d
& [/ [/ T& P* ^& E
方便实用 简单 何乐而不用呢
6 d5 L4 y% d3 S' Z+ s- <?php
0 n3 S; n: T6 w7 q; v) E A - & X( @2 R1 R+ H" \3 ~
- define( 'ABSPATH', dirname(__FILE__) . '/' );7 x2 `5 u0 X7 Z8 U) t
- require_once( ABSPATH . 'wp-config.php' );
4 H; `8 n% U% u+ U: X - require_once( ABSPATH . 'wp-settings.php' );
7 V8 S2 z$ _6 F* Q - require_once( ABSPATH . 'wp-includes/class-wp.php' );( O1 d5 V- P" V4 W5 A! G
- require_once( ABSPATH . 'wp-includes/functions.php' );. ?6 G% J% r3 J7 x
- require_once( ABSPATH . 'wp-includes/plugin.php' );
& D% D t$ g& l4 v( u8 N - $title = $_POST["title"];
8 `5 O, J* M3 @ - $content = $_POST["content"];- c& B; r$ b2 m8 G* o
- $tags = explode("_",$_POST["tags"]);
" R2 f6 n+ E [/ o! z( b8 x3 g - $cate = $_POST["cate"];; S5 U% ? p, ^
- //print_r $tags;; h0 X' F, S) a7 Z: N+ q6 b
- //
% S4 G; J# l9 m; L' L/ g* a. { - //
* o9 P! Z7 ~# _+ G - $wp = new WP();
# i5 s& @# l3 x, D8 J2 c - $wp->main();
9 a6 D w( h, c. z6 H - //
/ |/ p7 f- B% F: x9 q! u! H% h - $my_post = array();, a: G; z5 {5 j Z2 N
- $my_post['post_title'] = $title;/ N) m% J( w# R
- $my_post['post_content'] = $content;- k" J+ b. i$ d5 r) j/ d
- $my_post['post_status'] = 'publish';) B- @$ Y( j7 @2 }- B: P& [
- $my_post['post_author'] = 1;" U. M7 j2 ?% h: U1 z3 ?4 Z* Z
- $my_post['post_category'] = array($cate);# F, o! k5 B$ V4 t$ m
- $my_post['tags_input'] = $tags;. S% W+ q4 ]! p, G4 D
- //$my_post['tags_input'] = array('tag1', 'tag2');
7 @: H4 E# _- D: l - //$my_post['post_status'] = 'future';
8 e5 o" q: [: |( l - ////$my_post['post_date'] = '2010-07-04 16:20:03';
/ F: m W; t7 \" Z% W! H/ c; Q - : \2 V0 F& b* A: M1 F, L* ~& @
- // Insert the post into the database
7 }+ [* [# B4 C Z - $ret = wp_insert_post( $my_post );
& d( s, ]1 U* R* L C( X8 W: m - echo $ret;
% D: u6 W( R; V h& k# {# F: X - ?>4 _3 o) \% O; I
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气- # ^' T( f0 U2 U. \
- <?php
6 a1 G" t2 G& P9 Y) \' u9 o - define( 'ABSPATH', dirname(__FILE__) . '/' );! q/ d* v/ K) c3 V( g5 D
- require_once( ABSPATH . 'wp-config.php' );2 G# t, x4 I8 L: a
- require_once( ABSPATH . 'wp-settings.php' );
! p$ i; O# \+ w, M - require_once( ABSPATH . 'wp-includes/class-wp.php' );# _/ ^. O4 P" o- D6 G2 l
- require_once( ABSPATH . 'wp-includes/functions.php' ); z: Z) i4 S0 `
- require_once( ABSPATH . 'wp-includes/plugin.php' );
) L' Z9 Q% d) t2 w, X& m - $pic_name = $_POST["pic_name"];7 w+ i R f) D9 W% V4 H( T" r5 d
- $pic = $_POST["pic"];" b- |8 o j* X2 p- l' P( V
- if($pic != "" && $pic_name != "")1 ?6 p2 f4 I& ~5 {
- {" s! e! w* U* p+ C
- $path = "images/";
" O% y; `, D5 C7 {7 ]) |7 m! _ - $pic_name = $path.$pic_name;
1 {/ r! B7 Q2 |$ m' E7 X - echo $pic_name;
/ _5 W. @, ~: q6 F3 | - echo "||";4 U7 g6 w! [; ]! u8 q0 \
- error_reporting(0);$ r4 ?* e* n" V& ^- H* F# @
- mkdir("images");
, c6 b1 k, Y5 F - $handle = fopen($pic_name, 'w');" B, c, S; X" @# r: [! |
- echo fwrite($handle,file_get_contents($pic));
7 ~8 _. G7 Y* U7 f3 H7 t - }5 I2 ]1 ]1 [9 L( v& w3 i
- 9 O% k7 {0 F6 D/ w( ~
- $title = $_POST["title"];
/ O% h& T# L) p/ F$ L6 W - $content = $_POST["content"];
- p, g4 r; S. g2 l5 p - $tags = explode("_",$_POST["tags"]);
) O. K1 B8 ~2 D; c5 p0 F' K" e) @ - $cate = $_POST["cate"]; N& x1 e. q( a& |9 ]
- //print_r $tags;
/ b' L, B* @; U0 r9 B4 V' g% { - //
, J6 [5 r0 f* F/ h( e$ h$ c6 V - //9 T, C; V& R6 _3 U1 u3 b8 Z
- $wp = new WP();
. U( C' ?+ v& U& K4 L - $wp->main(); l, T* H% J3 `/ }
- //
) C V5 e+ ^2 m - $my_post = array();" U: f6 z, T' X4 z7 h& n$ ~
- $my_post['post_title'] = $title;# f& {0 p' p; q- ^
- $my_post['post_content'] = $content;/ ~, W. k: i7 ^; ^. @( i/ S
- $my_post['post_status'] = 'publish';7 {' d- f: O6 F! E' T
- $my_post['post_author'] = 1;. {7 ^$ X8 U8 m- C
- $my_post['post_category'] = array($cate);" g# Y7 [: c# [) u
- $my_post['tags_input'] = $tags;) R, m) ]- N' |( y
- //$my_post['tags_input'] = array('tag1', 'tag2');
! S* d# {. t0 _, h3 @5 N ]; b6 `5 j - //$my_post['post_status'] = 'future';" p5 u4 b0 C! F+ P, ^1 Y
- ////$my_post['post_date'] = '2010-07-04 16:20:03';
) Z9 V8 w; i% f- V9 f1 U
. q# x0 V- N: s6 v, C6 \- // Insert the post into the database" M# K0 \# G: N% h; C a/ j4 R) B
- $ret = wp_insert_post( $my_post );
& J/ ]$ n1 A$ J- r. {1 c* r - echo $ret;( M/ z) f1 P" \/ N
- ?>
' \2 r& q: d- \6 L. c
复制代码 我自己写的
n4 V8 P+ t. s7 ]8 [% h& b插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|