|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑 ( ]7 }# L' D& j% G$ G8 q: c
6 Y6 ~$ t8 F7 X2 q# X7 M, N7 I直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持& n6 v* Q! l4 {4 } E$ V6 L4 F z
- D* q- n: A* y& f
我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了
# @# K, {, o/ B& o5 ?0 A2 O3 e& L
方便实用 简单 何乐而不用呢- : B# A- S( X7 r( O9 Q
- <?php- b C6 Y) R. X W$ g5 K6 F
+ j& T, q) v2 J2 i" d$ ?- define( 'ABSPATH', dirname(__FILE__) . '/' );
& j( B- m8 D9 p( X; \ - require_once( ABSPATH . 'wp-config.php' );
0 e( o: e- X9 e4 P% Q' O8 } }7 U - require_once( ABSPATH . 'wp-settings.php' );& d* q2 I( ~8 Q8 z1 w
- require_once( ABSPATH . 'wp-includes/class-wp.php' );9 b( ^: j% i7 n) m
- require_once( ABSPATH . 'wp-includes/functions.php' );
3 u- o: ~6 ?# `3 X8 `* C# h - require_once( ABSPATH . 'wp-includes/plugin.php' );
3 Q S3 W- H. R* B - $title = $_POST["title"];
S+ T! Z/ i/ i& h3 ? - $content = $_POST["content"];
) \+ w% J6 [- W( S* X1 i3 a" l - $tags = explode("_",$_POST["tags"]);$ c% A+ e! k( `* w- N
- $cate = $_POST["cate"];
1 g3 s W2 k+ J8 t4 s% j - //print_r $tags;
* |5 P. ?! `" @2 k" ?# Q - //
* h; c# A5 g7 a0 E) X0 k - //% z- C) ]& E( z1 f ?
- $wp = new WP();) d# V- S/ o) g4 h3 E4 Q5 P
- $wp->main();2 D! L0 a. t* E3 M; z
- //) J. v/ M! b3 N9 f& g: w. R& o
- $my_post = array();/ C' M3 P7 P/ _9 n
- $my_post['post_title'] = $title;
$ J( w9 a/ k& \# e- [ - $my_post['post_content'] = $content;4 v0 ]/ I4 E/ }/ O3 t
- $my_post['post_status'] = 'publish';' S. I/ x* [; q% a, t. Q' c5 p7 W4 m
- $my_post['post_author'] = 1;
3 E! A2 f* F0 A' ~# o% A4 D7 s8 V2 d - $my_post['post_category'] = array($cate);
9 E* E# f& k# p, l3 x1 i E$ D - $my_post['tags_input'] = $tags;/ Q) E1 A5 A! y. S& u+ S+ B+ m
- //$my_post['tags_input'] = array('tag1', 'tag2');
6 J0 U; \" K( {0 |) G - //$my_post['post_status'] = 'future';
! R# a/ q) a& S! C2 d2 ?) } - ////$my_post['post_date'] = '2010-07-04 16:20:03';% m* a% L6 g8 o- U2 n
& ^( n# J+ G/ A. {+ {' r- // Insert the post into the database
% h8 v" G$ X6 e& ]1 D- B - $ret = wp_insert_post( $my_post );! C& O" w$ u' g+ r8 F
- echo $ret;
% O' z( e, g. W5 d# B, l - ?>
/ }3 i( A. R l; u+ i
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气- , c0 W# {+ Z w+ L8 }( Y
- <?php
5 c% M. O' S$ z5 R: D - define( 'ABSPATH', dirname(__FILE__) . '/' );
" V& Q6 U3 \- [+ u! q4 N0 x - require_once( ABSPATH . 'wp-config.php' );0 y; y5 F4 M: u% D* f
- require_once( ABSPATH . 'wp-settings.php' );
" J+ U) ^" z! K& w - require_once( ABSPATH . 'wp-includes/class-wp.php' );
( G. k3 v! k! z8 M4 V/ ]$ g - require_once( ABSPATH . 'wp-includes/functions.php' );
/ P9 V6 h5 H) z - require_once( ABSPATH . 'wp-includes/plugin.php' );( H4 m1 r& ]5 N+ L$ \7 M
- $pic_name = $_POST["pic_name"];2 Z" T& j+ @5 G9 y) A% m G& A/ ?
- $pic = $_POST["pic"];3 w$ A7 G" I- ~" k' J0 W0 J/ A
- if($pic != "" && $pic_name != "")
, @/ B' E) d" E6 n7 A8 F - { c9 X M j- v/ o$ I* S) @
- $path = "images/";
4 r& `2 S' m4 A+ G8 n9 v - $pic_name = $path.$pic_name;) Y3 [. A+ f; X+ j
- echo $pic_name;% }9 d( O# ^7 v4 u# X3 u, \
- echo "||"; n) H! @# w# X H5 s
- error_reporting(0);
+ u) c- t+ d+ ^0 e - mkdir("images");
: ~- q! h; E' K j6 b, n4 w - $handle = fopen($pic_name, 'w');8 N! `/ {6 Q/ [: n
- echo fwrite($handle,file_get_contents($pic));
; q( a. L3 x# z! b# C* q - }. X3 {, ~. ]+ A2 z; U
- # V- j _) L+ P$ x8 W
- $title = $_POST["title"];
2 }( y% {6 L3 D" P# w0 _) M7 }# k - $content = $_POST["content"];7 \2 c1 Z% k0 o) x7 ]7 t# Y6 I
- $tags = explode("_",$_POST["tags"]);
4 {; A# \& _. i& b+ D7 N - $cate = $_POST["cate"];
# s. v! H I) ]: s6 `: B4 K. \7 n& F - //print_r $tags;, P4 I: O5 ~ [0 s' L4 F7 ]! z
- //5 {0 F6 x7 T) B6 D
- //" H" X @" Q7 i2 L. ^4 {5 `( N1 Y
- $wp = new WP();9 c. U7 U2 I5 T3 X6 t
- $wp->main();( x! v* k6 \' J. y& {
- //
# `! K/ o1 W9 y- g( a - $my_post = array();) W7 U a8 ^& m1 E7 L7 F
- $my_post['post_title'] = $title;
, U9 J1 }. }1 k, ^* r0 K - $my_post['post_content'] = $content;% u/ r4 c# f6 q; k4 M; g8 f+ N& M: ?; j
- $my_post['post_status'] = 'publish';6 m1 w$ Z1 f" C$ `
- $my_post['post_author'] = 1;
1 [* S8 O1 @7 s6 v% S. @1 g - $my_post['post_category'] = array($cate);- q q9 g( o( W0 `, {2 t! u
- $my_post['tags_input'] = $tags;
, j& p2 {! D9 a: t& i. p - //$my_post['tags_input'] = array('tag1', 'tag2');
+ o' V$ _5 \, I" B7 _ - //$my_post['post_status'] = 'future';
5 m1 j0 d0 D+ Y0 x - ////$my_post['post_date'] = '2010-07-04 16:20:03';
9 }% I" h- \5 {- _8 o1 E& N
0 H6 f$ G4 K/ ]* G/ [2 K r- // Insert the post into the database
- X- f# ]( q7 _ - $ret = wp_insert_post( $my_post );
5 @6 u5 k9 v* i8 A2 s - echo $ret;
2 Y# @. x3 u8 |0 W - ?>9 Y8 v3 n7 T1 e) {# w2 V8 K+ B
复制代码 我自己写的) @ G% G/ s7 p4 p% P: G h
插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|