|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑
( ^6 Z8 Z2 W. l, P4 H& L
0 L: Y l: |: B% W5 C! F( K2 Z直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持
2 J7 z2 S! @" w6 ]( ^2 Q1 T- B$ ~7 S: G, z8 c) v
我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了
: o3 `/ j8 e6 @5 V. Y, D) I& o y' O
方便实用 简单 何乐而不用呢
% t, j. ^4 A% m) j1 |/ D- <?php* g6 e4 J* J" B
7 R. ~0 B1 {# L" i: T) ?- define( 'ABSPATH', dirname(__FILE__) . '/' );
% e; f6 j( \0 H( a - require_once( ABSPATH . 'wp-config.php' );. m: k9 M, I" C' |0 e4 F. _8 _5 ~4 M* p
- require_once( ABSPATH . 'wp-settings.php' );+ T C& q: f- u# x b1 y
- require_once( ABSPATH . 'wp-includes/class-wp.php' );4 l6 `* L2 W+ Q$ Z& A1 v' k
- require_once( ABSPATH . 'wp-includes/functions.php' );, n- e, I& R3 d8 ^' d9 @+ y H
- require_once( ABSPATH . 'wp-includes/plugin.php' );) L0 ^* c- d- G( S3 X) ?/ T
- $title = $_POST["title"];4 P$ P0 s2 V( S6 D9 q d9 t) Q3 G
- $content = $_POST["content"];
2 T/ J* R: o+ Z; a5 \, D% Z2 L - $tags = explode("_",$_POST["tags"]);7 M( U* Y, O6 O$ D5 a% W
- $cate = $_POST["cate"];
# J, N- v9 {6 S$ \! k7 C - //print_r $tags;0 a' d; Q4 o* u5 F8 K( S. W
- //
`( V: ]/ B' `+ k - //
0 Y. X" Z1 R: c; R4 y - $wp = new WP();) i& b# c& N9 x, h" ?4 V/ g9 F7 V
- $wp->main();' Q- I$ ?1 Q7 H; z. U9 b
- //% }- `1 x; K# b+ y) H
- $my_post = array();
# \0 U7 U: ~ l/ F6 _8 G: h - $my_post['post_title'] = $title;9 z4 d2 a- q* V
- $my_post['post_content'] = $content;: u+ a% p; S, G+ O; d
- $my_post['post_status'] = 'publish';
6 W) D' X0 [6 _4 s* E# e2 _1 a - $my_post['post_author'] = 1;
8 o' b9 p" L, A2 R9 B7 @( | k - $my_post['post_category'] = array($cate);$ p$ {/ ?" w3 E$ j% R' ]; f
- $my_post['tags_input'] = $tags;
) m& Q/ I& z8 A; p5 ?- m - //$my_post['tags_input'] = array('tag1', 'tag2');
$ h8 d0 x* `$ w; [# g8 F4 w - //$my_post['post_status'] = 'future';" G7 t3 G6 C" |
- ////$my_post['post_date'] = '2010-07-04 16:20:03';4 O7 h5 s6 X: R1 k/ T1 q: {
- 9 [2 F' f/ Q; g% @* b" E
- // Insert the post into the database
8 g0 p+ f4 s' E0 h6 m9 p2 z: b% T - $ret = wp_insert_post( $my_post );
( L" Q2 _/ Y8 x+ T; m( K: B1 S5 ?* o - echo $ret;
( X+ G8 b5 C, W3 g - ?>& }7 s4 m2 o5 c9 {0 ?# o+ f! h
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气- ( ?3 f/ s; Q( R
- <?php
$ W- U& y1 ~; J, v0 y% C8 H# g9 r - define( 'ABSPATH', dirname(__FILE__) . '/' );* p- f- ]! ~" R$ L% y5 |2 }5 |
- require_once( ABSPATH . 'wp-config.php' );
& E; ]7 H1 a' |) a' K1 \ - require_once( ABSPATH . 'wp-settings.php' );, T) \) h1 | K1 f5 X4 i) O7 M1 Y
- require_once( ABSPATH . 'wp-includes/class-wp.php' ); ?- `7 U+ Z6 x" h( z" a' C; K: `
- require_once( ABSPATH . 'wp-includes/functions.php' );
' R4 B. P# b: I5 { - require_once( ABSPATH . 'wp-includes/plugin.php' );* n0 p0 @. ^! L2 B
- $pic_name = $_POST["pic_name"];
2 x+ I+ A" y7 W- z$ X) z2 ^ - $pic = $_POST["pic"];' K/ w+ _' f6 p, d& t' f6 B
- if($pic != "" && $pic_name != "")
6 D0 \& \2 w4 x. e& v - {
+ T3 G: _9 {. G5 @* w% Y8 E( {/ T - $path = "images/";
- {3 L. ~9 a) A# w! c7 b3 y2 B - $pic_name = $path.$pic_name;1 _) R1 e5 z: a- I. T
- echo $pic_name;& r4 G; c. g7 j
- echo "||";6 Z; Y& B) P; F1 X
- error_reporting(0);# @) ~/ E2 k) e2 }
- mkdir("images");
- w# [* @/ S' g# K7 G - $handle = fopen($pic_name, 'w'); Z) s5 D; f. X' w( a' v
- echo fwrite($handle,file_get_contents($pic));- _- `: Q7 U1 o9 O! Y0 k3 x# W
- }3 R m. W$ ~6 M0 c
- ; P, N3 `* i# f
- $title = $_POST["title"];% a+ b9 W1 }/ Z3 |
- $content = $_POST["content"];
+ M) x7 B0 [ [$ |0 m - $tags = explode("_",$_POST["tags"]);
- R+ Z. ~( J, [" r6 H! W O' j4 u - $cate = $_POST["cate"];
/ c& I" [$ \7 M E: u; t4 o - //print_r $tags;
5 S: {% `: Y" d, } - //
1 A: |% w6 O# N5 ^/ Z% E, f% ? - //
! ?+ W9 \7 E5 a5 A8 X5 H y - $wp = new WP();
( D$ E" O i7 d- j4 e; t - $wp->main();
4 j {0 @3 y8 S# C - //
% Q. G* x) }4 O7 Z& y - $my_post = array();: _! I' g& I% r2 _. ~/ p9 s( u, h1 a) _' }
- $my_post['post_title'] = $title; I4 s5 Y# {: H" J0 U R& Z; l
- $my_post['post_content'] = $content;
5 L# f4 w5 [, J3 @0 {7 R - $my_post['post_status'] = 'publish';+ Y5 n, J$ k. P% g/ |+ L
- $my_post['post_author'] = 1;) m/ _( f8 @* P1 `* J( a1 u9 f$ l
- $my_post['post_category'] = array($cate);
( H# a1 _) Z$ B5 c, V. Q5 }, A6 R3 ` - $my_post['tags_input'] = $tags;6 N3 F; J, z% o6 X- k2 w$ @% G
- //$my_post['tags_input'] = array('tag1', 'tag2');
8 E& l' J7 s: I& `- W - //$my_post['post_status'] = 'future';
+ l* W/ H8 ]/ s. S5 \ - ////$my_post['post_date'] = '2010-07-04 16:20:03';* E. x( O7 E" Q4 y$ G5 D; C
- 8 y+ y5 K& C3 }5 }& P) q) C
- // Insert the post into the database9 v- l4 b1 [* a0 \' y6 p1 j- i
- $ret = wp_insert_post( $my_post );8 C# d4 g2 b9 c, x1 d* U
- echo $ret;9 {% i/ F! l4 \% j6 m
- ?>
: [# _& y# p; w# K J4 ?1 t
复制代码 我自己写的( x5 C8 n) Y+ @3 z. l# b! z
插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|