|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑
) v% D8 y. }4 I: i' T% X" T2 T$ C# i* Y
3 G' i, q" J- Y, d0 a直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持1 c$ }- n, ~6 e; k
* N+ u) }' y( ^5 \1 q
我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了
& J0 V. J: l' L r
6 R$ q/ T. H0 \+ x2 P+ h方便实用 简单 何乐而不用呢
% r5 Q; P9 f4 e$ H( K! ~3 ?, ^- <?php
) {: j, D$ b3 g - i0 G* X( ?" ~
- define( 'ABSPATH', dirname(__FILE__) . '/' );, M# t+ U. A M" E: h" A* i
- require_once( ABSPATH . 'wp-config.php' );
0 y; S$ J% }! h6 u - require_once( ABSPATH . 'wp-settings.php' );
. y2 h4 F! T) i6 u* H. j& N+ e) f - require_once( ABSPATH . 'wp-includes/class-wp.php' );
6 a8 M# J F1 k$ _8 [ - require_once( ABSPATH . 'wp-includes/functions.php' );
4 \% C4 E A( o6 M - require_once( ABSPATH . 'wp-includes/plugin.php' );
5 ?" l! U) }9 L* i - $title = $_POST["title"]; e- f3 q$ ?+ E' ]0 z, X
- $content = $_POST["content"];
( m* p) ]6 D5 W* C - $tags = explode("_",$_POST["tags"]);+ U, I0 q" Z" J! P5 A, X
- $cate = $_POST["cate"];
' H' j, N4 e5 X5 W - //print_r $tags;
& C9 S5 t, g; e* n2 y - //8 X+ T1 ^: j/ p) S0 r! b
- //
. k' j) ?, x5 M+ R: d3 N7 w1 _2 s- {& z - $wp = new WP();
- X' W7 V& @% N1 g& A6 I - $wp->main();; S5 } z, S& y: @" o
- //
- z' |/ ]# H4 ^. b - $my_post = array();4 e0 t& U- k* L4 ^
- $my_post['post_title'] = $title;7 K7 w4 Q% |! `( P+ [1 L
- $my_post['post_content'] = $content;1 f. }& d' R& J0 }! F+ X" t8 a7 N
- $my_post['post_status'] = 'publish';
' f7 H2 N: Y' c1 F - $my_post['post_author'] = 1;
& ?9 ~1 S% l5 ^& ^ - $my_post['post_category'] = array($cate);% D) k9 p* d+ B, u9 L1 L2 u$ V7 Z! S
- $my_post['tags_input'] = $tags;
: k$ i5 b) }$ R8 H: D - //$my_post['tags_input'] = array('tag1', 'tag2');# s( I0 c& T% o3 E! @, F
- //$my_post['post_status'] = 'future';8 m* c* B6 h; `0 f) k& d# O: h
- ////$my_post['post_date'] = '2010-07-04 16:20:03';* ~5 B1 z2 q9 v9 \
- ' s1 P a; e5 s3 E$ a3 f9 X( I
- // Insert the post into the database4 I1 @% j# @( S M* |
- $ret = wp_insert_post( $my_post );1 k1 V& Q) A+ S: F" |0 r" G1 ^
- echo $ret;
; t& @+ O& @: A& P1 f - ?>
- a) q2 _) j+ P& m: d4 }
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气
1 K$ ?% `' c8 y* Y8 g- <?php6 l. L% R7 b; s5 c. ]0 A. h5 j% @
- define( 'ABSPATH', dirname(__FILE__) . '/' );
3 }' D" ?: y9 U4 U: b7 E' m - require_once( ABSPATH . 'wp-config.php' );; B- }- |1 G8 Y( a5 Y
- require_once( ABSPATH . 'wp-settings.php' );
, M- s& Z# E$ f+ V0 _* g0 g) E# D - require_once( ABSPATH . 'wp-includes/class-wp.php' );
5 t+ |2 g* J% j" P - require_once( ABSPATH . 'wp-includes/functions.php' );
$ B# L4 J |, m3 N. [6 B. r - require_once( ABSPATH . 'wp-includes/plugin.php' );4 v/ E( O+ h6 v
- $pic_name = $_POST["pic_name"];: Q% m# V; B9 w3 T1 Q
- $pic = $_POST["pic"];
, c+ u8 S6 I& T& _' z. C - if($pic != "" && $pic_name != "")
) }8 b/ j, h# Z- V9 U - {
: G2 t) ~2 \- z5 V( \ - $path = "images/";
0 A2 w, V& K/ c2 J$ f - $pic_name = $path.$pic_name;) s8 O- B1 }% L* k
- echo $pic_name; ]( o. [) N% u$ g
- echo "||";
/ R3 A U# ~ I: e' f# }3 ]1 V - error_reporting(0);
! ]1 ~1 ^% L9 N1 k - mkdir("images");
3 z, ]* @, t* K2 U+ l( U - $handle = fopen($pic_name, 'w'); z* S7 I4 w F9 V. K
- echo fwrite($handle,file_get_contents($pic));" m5 C/ V, B0 R# T; A" e
- }# d6 g" l& j, {) T4 {
! {3 I! l8 t/ j+ c- $title = $_POST["title"];- b8 }+ z- a3 _0 ]3 V
- $content = $_POST["content"];
: J9 [0 `2 v) X. d1 _3 a - $tags = explode("_",$_POST["tags"]);, j" g9 s9 Z: L1 y1 j( E m9 @
- $cate = $_POST["cate"];
: u/ G" C! D2 u% ?' j - //print_r $tags;' }# J2 C' h( {' c7 P, t8 _8 u
- //' t' c5 f# ^% W' E% |" a/ D
- //
. u- d1 J/ ?% Z! \ - $wp = new WP();
$ I' d. r- X/ R d; ^) w - $wp->main();6 Y' P- K( K$ b8 ^; A
- //. a2 b8 C" P+ U
- $my_post = array();
, j8 ?/ C9 {! a9 d) s+ n4 D - $my_post['post_title'] = $title;
- M$ C6 w; Z1 d5 l - $my_post['post_content'] = $content;7 K$ L* j: q; G1 j! g
- $my_post['post_status'] = 'publish';
+ C0 @ X5 B7 C {, U' X( n6 ~ - $my_post['post_author'] = 1;. p% v3 ^7 H Y- _1 I, ]& \- o& Z8 _+ R
- $my_post['post_category'] = array($cate);
9 G4 p% X* i: F9 e - $my_post['tags_input'] = $tags;
. l" c$ M4 k4 U- u% U - //$my_post['tags_input'] = array('tag1', 'tag2'); g* o4 {( s6 Q) K) h
- //$my_post['post_status'] = 'future';8 e) n, k+ g4 {/ e
- ////$my_post['post_date'] = '2010-07-04 16:20:03';, d8 t1 E2 n9 d/ T7 {
- ) q' \/ l3 L0 t4 ]! L7 V# w- {! p
- // Insert the post into the database
+ }" K. V: [$ A1 H - $ret = wp_insert_post( $my_post );
) }* B$ r N8 ^$ U - echo $ret;
5 |$ b( e( f& N% a - ?>4 O5 m/ l! \& k: E3 c+ z7 N& a4 N$ e
复制代码 我自己写的9 V+ n! x7 ], P6 O8 e
插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|