|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑
% p0 H& D* G6 e; j. D' @+ Q& x
9 D# r9 t( K S& }1 n+ a直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持7 ]9 R% l0 |: S0 A @& k
9 C" P- y: A1 D7 ~4 h" d# L7 L我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了
0 n9 R% c. v7 {) T: j- g
4 L. ]3 M& L' k$ G% w' N: `$ J6 l2 V方便实用 简单 何乐而不用呢
; |8 i5 q% K" V6 \: v5 l- <?php% l, d, d0 O) }, A1 w
9 I, f! o* G" X8 [4 Y: O- ?- define( 'ABSPATH', dirname(__FILE__) . '/' );' Y2 s8 J% j* ^2 U# F2 i
- require_once( ABSPATH . 'wp-config.php' ); w3 y5 E* T% I( A9 G0 `
- require_once( ABSPATH . 'wp-settings.php' );
: |5 L* S/ E# ]5 Y* Q - require_once( ABSPATH . 'wp-includes/class-wp.php' );
: d: j; H( f( a" _ - require_once( ABSPATH . 'wp-includes/functions.php' );
. p' i. }* K+ A( T3 H - require_once( ABSPATH . 'wp-includes/plugin.php' );
/ s$ d; K1 P6 U( J) _) e/ {9 b - $title = $_POST["title"];% o' K$ L; |: q# }0 z
- $content = $_POST["content"];
" ^% O1 X- G2 a" U7 @% w# @ - $tags = explode("_",$_POST["tags"]);
7 K& {" m# g+ k. k2 ~7 p u8 S; _ - $cate = $_POST["cate"];
0 ]9 X2 @2 |1 n) `8 i v - //print_r $tags;
+ L* ~6 p' B" U' m( [, O! I - //& j+ I+ a# {- S" w0 H# \2 P& ?/ J
- //+ g6 H5 M$ q- M8 \
- $wp = new WP();% E' ]+ w3 g& R# @8 L, v! o
- $wp->main();% P- |; L' i3 n% @* p
- /// t3 B* W9 k$ Z; r. t7 l& Z7 @
- $my_post = array();4 |1 R- Z) @. u6 P- H3 f+ B$ g+ R
- $my_post['post_title'] = $title;
q: M- f' x: g2 F- d, n - $my_post['post_content'] = $content;$ `+ F. B: H& ^; z9 D
- $my_post['post_status'] = 'publish';
0 \; p' | _! w - $my_post['post_author'] = 1;( l' y# ?8 ^2 o- q6 i4 H& N
- $my_post['post_category'] = array($cate);
; z# b3 ?1 ~+ P0 d - $my_post['tags_input'] = $tags;1 }+ e3 C4 @* P7 B* m* K; k
- //$my_post['tags_input'] = array('tag1', 'tag2');* N9 l+ x8 } E5 M+ C) F3 ~
- //$my_post['post_status'] = 'future';! |1 H/ G7 n6 u$ ~2 e
- ////$my_post['post_date'] = '2010-07-04 16:20:03';
; i( a, I. c- A9 v
2 @) y( b& w5 |% @1 U0 x- // Insert the post into the database1 N7 u C' X; t7 w% k$ F
- $ret = wp_insert_post( $my_post );5 M9 ?3 n3 h5 G+ G( I' s
- echo $ret;
( s( t6 |3 o% I - ?>4 B. Z2 W0 e5 {" O5 B) J+ B
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气- : M$ w- l _2 h! a e) S
- <?php9 h7 Q! v- N$ k
- define( 'ABSPATH', dirname(__FILE__) . '/' );
" C; t8 X2 i( d7 p9 z: H. Z& r! o - require_once( ABSPATH . 'wp-config.php' );1 H+ ]1 V; p; P0 {; j% Q: r
- require_once( ABSPATH . 'wp-settings.php' );
5 f) {" k: J8 @6 e - require_once( ABSPATH . 'wp-includes/class-wp.php' );
, c! e* ^& T! Y9 ~/ d7 M - require_once( ABSPATH . 'wp-includes/functions.php' );0 l# n: T* I# \0 n
- require_once( ABSPATH . 'wp-includes/plugin.php' );/ l8 d$ n( C% W' ?$ f9 N
- $pic_name = $_POST["pic_name"];
" V5 R: h. w6 o9 g. \' s( l - $pic = $_POST["pic"];
* |' h2 n& f0 \( V/ M' M - if($pic != "" && $pic_name != "")# I% {& R4 T- q/ t2 N) _
- {
: s% `$ j# u0 G: [+ c. l) s - $path = "images/";
2 `5 q# `) o& c, J' ]/ i3 L& q3 Z - $pic_name = $path.$pic_name;5 U$ P5 r D9 Z0 g$ O
- echo $pic_name; `$ A, p( z+ j9 M4 b
- echo "||";
4 m# p+ g. z, i7 t - error_reporting(0);2 K* \3 C3 O7 N3 d0 R4 U
- mkdir("images");8 \: |) E! J" l8 a' ?
- $handle = fopen($pic_name, 'w');
- t. s: P) B9 A i4 w8 A% D - echo fwrite($handle,file_get_contents($pic));; s! {( u/ U! Q! n
- } v/ m8 n. Y, |' t5 e4 {
- . K+ L+ y2 s- `5 K0 \+ r0 N
- $title = $_POST["title"];
7 a6 U- C7 p: B8 \( _ - $content = $_POST["content"];
) O+ U" ?( h M0 {+ \- X6 W - $tags = explode("_",$_POST["tags"]);% s4 u' _. e8 M3 V
- $cate = $_POST["cate"];
* T- m% H: u9 p - //print_r $tags;
: Y6 I7 n$ J7 G2 o+ o' ]/ g* \ - //" H: Y8 {1 K9 s, Y' `" | d/ ^% }2 C! S
- //
8 [/ ^8 I8 M, Z) L0 s - $wp = new WP();
1 ^5 m x3 ?! C1 I1 ~ - $wp->main(); H3 O+ b f, w# r% [! t( x
- //! _6 i8 ?9 U$ q2 x1 }7 m
- $my_post = array();
3 g9 h5 |" @8 r* U N4 Y2 T/ X, M - $my_post['post_title'] = $title;
; g* S) N6 L/ k3 M6 _ \+ L0 C - $my_post['post_content'] = $content;
- x8 G S9 O) S2 Z+ S - $my_post['post_status'] = 'publish';
6 U, ?" x' ?/ U& ? - $my_post['post_author'] = 1;9 K- Y. Z2 Q! r y/ p p
- $my_post['post_category'] = array($cate);/ A+ }7 V5 Y- A4 Y
- $my_post['tags_input'] = $tags;
8 J$ d0 Q9 w" F - //$my_post['tags_input'] = array('tag1', 'tag2');
, I1 E5 R' I. K4 m% _6 Z - //$my_post['post_status'] = 'future';
$ v0 v$ \9 ~* _: }+ q4 O - ////$my_post['post_date'] = '2010-07-04 16:20:03';# U/ [7 C% D' U7 U% c. _
; w8 z7 l) z* o8 `# L8 n- // Insert the post into the database
% v" @' J+ R, X3 t* G$ q- L - $ret = wp_insert_post( $my_post );1 M& U3 j5 D: C+ j, q7 ]! T
- echo $ret;
! L Q# n; S5 ?9 D0 G- }; c- y - ?>
9 L7 a$ e2 `' M4 U
复制代码 我自己写的
/ G# E7 w* \. ?插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|