|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑
9 f9 K4 f _3 K0 D
9 u" e h) \/ Y" H! M直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持( H9 X8 f Y4 N$ Z
7 _$ A- z3 v* ^& u j' F. j我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了
: ]+ x* J9 M, P3 j3 q2 H) `. t3 N- w" |1 C1 F
方便实用 简单 何乐而不用呢
& O% W; k, _# Q# y- <?php
' h: k6 M4 Y8 R" ~ m - % M8 G1 `# j% p( g1 C
- define( 'ABSPATH', dirname(__FILE__) . '/' );
% R7 c0 ?7 P# q, R8 b - require_once( ABSPATH . 'wp-config.php' );3 D+ F& Q! R2 G: x* v$ p
- require_once( ABSPATH . 'wp-settings.php' );
" [0 q* D2 O9 e" X6 E( [; ] - require_once( ABSPATH . 'wp-includes/class-wp.php' );
& A$ K& ~" L+ v8 N3 x - require_once( ABSPATH . 'wp-includes/functions.php' );8 N& W4 c% x) t: l
- require_once( ABSPATH . 'wp-includes/plugin.php' ); ?+ |. h- X1 r, u$ I( r
- $title = $_POST["title"];
- v) i" h! d% @9 p3 g* Z1 \" w+ x - $content = $_POST["content"];' _. ?: i- A% p6 C
- $tags = explode("_",$_POST["tags"]);( J" k1 A% ^- ~
- $cate = $_POST["cate"];
# }4 u, r! o. n' h' v- Z% M6 X' m1 M - //print_r $tags;
' Y% F. R6 D* A - //
2 } ?+ H1 \% P0 U: U# @ - //
1 i4 S+ j0 o& N x/ U - $wp = new WP();) m, \, q2 g: u2 F7 }
- $wp->main();
# O1 A. V! r2 b) c7 E" B - //
! |; t7 V2 S6 Q' b/ a - $my_post = array();
7 i6 M0 O$ g- P. g( |! L - $my_post['post_title'] = $title;' j/ [* N' ^' y) X V% [% x
- $my_post['post_content'] = $content;
$ j: f/ N) K% l$ V. R4 I- L% P9 ?0 y - $my_post['post_status'] = 'publish';
: b( D# \% {# v* w N$ \9 @ - $my_post['post_author'] = 1;
R: y, [; ?3 c7 V- D - $my_post['post_category'] = array($cate);8 L: o* u( ^4 @1 _
- $my_post['tags_input'] = $tags;
0 \* \& s1 p7 S6 |. t9 b5 n9 u4 x - //$my_post['tags_input'] = array('tag1', 'tag2');7 P$ S% G0 y6 H
- //$my_post['post_status'] = 'future';
+ T& Y* w" E' H' Z - ////$my_post['post_date'] = '2010-07-04 16:20:03';: x" R1 M! T, x# J: y9 A6 v" ?
- + i5 L' x' i5 V( J& H( Y. m
- // Insert the post into the database( B& q$ u5 w! ^+ _
- $ret = wp_insert_post( $my_post );; d8 x3 z$ i [
- echo $ret;9 P* N2 a9 o. T' M; c
- ?>
, b8 l1 o( e) I0 k. `
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气- " J. ]# r# | y1 f& ~
- <?php2 a3 x1 T. D3 \: x0 G3 h
- define( 'ABSPATH', dirname(__FILE__) . '/' );* P9 K3 e* c ]! j" M8 T
- require_once( ABSPATH . 'wp-config.php' );
+ X. @' I4 D: o1 _9 [ - require_once( ABSPATH . 'wp-settings.php' );9 K, O* J9 S* w3 q# I
- require_once( ABSPATH . 'wp-includes/class-wp.php' );: j1 ^/ @. _0 K1 T6 M) p
- require_once( ABSPATH . 'wp-includes/functions.php' );
w! ?6 t! L1 n; {; Q7 ? - require_once( ABSPATH . 'wp-includes/plugin.php' );- A. V5 q Z( S$ b4 ]6 V4 s
- $pic_name = $_POST["pic_name"];( {: K4 i) O2 L
- $pic = $_POST["pic"];
: V4 D$ ~" r; P9 V! c - if($pic != "" && $pic_name != "")
* Q# c7 Q. {8 a; _5 T* a- x - {; K/ ?' j6 @; B2 E
- $path = "images/";) a4 @" ?) z3 y
- $pic_name = $path.$pic_name;
" B3 B* s2 E9 ~( J' p0 @ - echo $pic_name;6 `& b v, y8 g8 d$ c: \
- echo "||";
/ x! [$ D& ~. y2 d4 N( c - error_reporting(0);- p. E; C, y0 W% _/ H# L
- mkdir("images");
! t0 J* i- O$ }- A6 a- s- n- M - $handle = fopen($pic_name, 'w');
' n) @3 n0 i7 C8 W - echo fwrite($handle,file_get_contents($pic));; P( f7 t8 T) v5 p
- }" [- L, f+ S6 x% E! d
- . e* w9 t: m( H8 E6 H5 O
- $title = $_POST["title"];
" @5 p! c: e7 |/ W/ ]" g; F - $content = $_POST["content"];, {. y( f, R5 ^3 \) o
- $tags = explode("_",$_POST["tags"]);. ~8 p0 G* r0 n; B1 Z4 d; l' B
- $cate = $_POST["cate"];
4 g0 [* b3 ~& J2 N - //print_r $tags;6 h1 Z. p) W' J+ x
- //0 U" Y! s E! M
- //
! t' U2 ~$ L) I/ ?2 c( H - $wp = new WP();
3 F8 J2 U% J3 B) u - $wp->main();
( q. y U" q, d) H$ r# i S - //
$ v- A" k, X7 H% u$ l# I& s - $my_post = array();
0 ~6 o K/ r# {8 p7 c. e9 t - $my_post['post_title'] = $title;' @. u/ H: y! C1 h/ c7 D- N, _
- $my_post['post_content'] = $content;
, e& K1 ]3 q- G# D - $my_post['post_status'] = 'publish';, t. [3 l0 z, @/ A4 t `8 v* Z
- $my_post['post_author'] = 1;
. ~% n% Q! s5 U' i) H' T4 X - $my_post['post_category'] = array($cate);
7 q, g" v# B1 @' s6 I - $my_post['tags_input'] = $tags;' B9 w" H8 X- |4 \ ^! @, x
- //$my_post['tags_input'] = array('tag1', 'tag2');
5 I+ { k5 ?' w3 q7 J - //$my_post['post_status'] = 'future'; c/ f2 K6 S& ?7 L
- ////$my_post['post_date'] = '2010-07-04 16:20:03';$ L% N& Y# ]' _2 C
# s! W; L3 \0 |6 P) H6 t1 Q- // Insert the post into the database; p( [+ ^ X- ]" U1 R
- $ret = wp_insert_post( $my_post );' G% W' r; s3 y& w5 L/ L/ g' X
- echo $ret;$ R" R! P! D2 ^. ^( ~: H3 y$ B
- ?>
; Z2 O+ ?# V c) G0 ?; A, k3 ~
复制代码 我自己写的
+ ]# H1 ?3 l4 A) q7 C插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|