|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑 $ \) ?& s+ l& A1 O
" o7 w, I" \' [1 y5 j& V2 V2 L直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持 t7 J( o$ E; X
9 Y. O h2 h- p6 b9 C& k2 F/ [! F
我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了
$ B( n2 r7 f% o9 G1 w' M; i' p
+ G" ~) F/ N/ N方便实用 简单 何乐而不用呢- 3 \- {1 U6 v- j
- <?php
4 V2 v% w; M3 q# S - . t* d+ u o2 T7 W; {. _
- define( 'ABSPATH', dirname(__FILE__) . '/' );
- Q$ L* u' P6 P Z6 C+ M5 { - require_once( ABSPATH . 'wp-config.php' );6 G& G" B R2 F6 U" f
- require_once( ABSPATH . 'wp-settings.php' );. {- H f& c: M
- require_once( ABSPATH . 'wp-includes/class-wp.php' );' U# X. A) y u% |! J3 E
- require_once( ABSPATH . 'wp-includes/functions.php' );
" z2 ?( P \" Y1 _0 |1 J - require_once( ABSPATH . 'wp-includes/plugin.php' );
# P& g- {; O# B E [1 U9 G - $title = $_POST["title"];
! }. ?: {6 q% V2 ^2 O - $content = $_POST["content"];8 L; T U: r% t& J! N: w5 R( T
- $tags = explode("_",$_POST["tags"]);+ Y6 d+ X x( ~( \8 D) u: R4 E9 I
- $cate = $_POST["cate"];
1 Q0 j# `* L- G) Z - //print_r $tags;, l! b: |9 T2 x* Y2 \
- //
: X3 [6 R; S9 T5 }4 T9 T# ] - //
" d! R9 o- L5 C# B - $wp = new WP();6 m( L( c* Q- I0 n% ^: _
- $wp->main();" l% n9 o1 M4 {& o
- //
6 g, o- H) \7 l# r; k4 |; ^ - $my_post = array();
7 ~% f/ \5 j) c5 _" K - $my_post['post_title'] = $title;+ \+ o/ W& y7 ~: y& j
- $my_post['post_content'] = $content;
9 L3 M& E* R! U# C+ \/ l - $my_post['post_status'] = 'publish';& d6 \, F8 j2 T" [: S6 x
- $my_post['post_author'] = 1;
1 [) Z5 [* W: ] - $my_post['post_category'] = array($cate); d" P1 S1 a, s" \" K4 c4 R. i3 g
- $my_post['tags_input'] = $tags;, g4 `1 F& Y2 J$ Y, q$ P
- //$my_post['tags_input'] = array('tag1', 'tag2');1 T0 }6 Z/ h) v+ w% T, D7 N
- //$my_post['post_status'] = 'future';
9 {( L9 V7 L" Q - ////$my_post['post_date'] = '2010-07-04 16:20:03';% X, V* ~4 {2 }# a) m- z; P$ x
, p: ?! u6 R7 j+ y- // Insert the post into the database: l3 r: f- L, d* G K t3 u
- $ret = wp_insert_post( $my_post );
) G0 y* G8 s+ i: z3 U - echo $ret;
" K) ]& _- g. _5 ^* h - ?>
+ e# Y' f- @# p9 ?+ v
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气- 5 T; T0 w* [ O/ h
- <?php5 d( ^& I- p' {- J
- define( 'ABSPATH', dirname(__FILE__) . '/' );
2 E C; g0 ~5 ` - require_once( ABSPATH . 'wp-config.php' );
7 V, J4 W0 c" m - require_once( ABSPATH . 'wp-settings.php' );
8 Z/ ]5 o9 V4 \- }1 k! ^' k$ c - require_once( ABSPATH . 'wp-includes/class-wp.php' );
) T& F+ d; g6 G! P% H - require_once( ABSPATH . 'wp-includes/functions.php' );
+ t# Q( Y! s2 i! u4 f1 w - require_once( ABSPATH . 'wp-includes/plugin.php' );% d1 U( \3 J1 V( n& c) x
- $pic_name = $_POST["pic_name"];5 n3 H4 I" e2 @5 h0 w) [0 p7 c$ T
- $pic = $_POST["pic"];, @" [1 Q* h8 {9 c) \6 `
- if($pic != "" && $pic_name != "")+ |* u* a$ c# E9 k
- {( t I. j- m" n; r# I' p
- $path = "images/";
5 O2 V1 ~% [3 c5 [ - $pic_name = $path.$pic_name;
1 `3 \% U4 b% q3 A+ F - echo $pic_name;- q8 G. I' R) p1 [% i: a
- echo "||";
) t# U* |. a; ^* b, V9 U - error_reporting(0);
+ i0 X2 k' A: a1 u+ c& h; \ - mkdir("images");
. P' e @) q( W4 v. ?/ m - $handle = fopen($pic_name, 'w');
: `& J& H5 f0 ] G$ G: W ^4 J - echo fwrite($handle,file_get_contents($pic));+ t% D c3 H( e$ A2 ~, D
- } b* j" {" ]0 G1 j2 X: X4 G+ W, B
- 6 ^, N H; N9 U6 E/ w/ G& V3 H3 ~
- $title = $_POST["title"];
( b- V3 G v+ J, C4 T6 Z) j - $content = $_POST["content"];
% x1 R% u' a8 f( c; L3 e2 g5 } - $tags = explode("_",$_POST["tags"]);
* l. o/ o! J w - $cate = $_POST["cate"];3 G. e) h i! f2 c
- //print_r $tags;
- w( v8 u3 r5 `7 l# j3 ` - //
' Z6 y: O7 j3 |3 O. P" j7 o1 S' J$ m - //
9 U: X6 m$ N; K; h. h! ? - $wp = new WP();
7 C+ u, |$ h+ [ N - $wp->main();( ~& d5 H8 v0 k
- //7 |/ r" q1 y1 `! A: Q' \
- $my_post = array();1 N8 v3 i+ m, i9 a
- $my_post['post_title'] = $title;
5 E { x. y# U3 ` - $my_post['post_content'] = $content;
) l' l; Q2 T! z' }( M4 d - $my_post['post_status'] = 'publish';
f! n% m9 q/ W, O" V; L - $my_post['post_author'] = 1;
+ z3 \( q3 x- ^7 R% H" | - $my_post['post_category'] = array($cate);1 N; l: g1 {8 F2 U- }- ?( i3 P
- $my_post['tags_input'] = $tags;, w& v, ^' `* x& ~- y
- //$my_post['tags_input'] = array('tag1', 'tag2');7 {. W: m5 ?2 P- b. U& b
- //$my_post['post_status'] = 'future';
( W5 k+ H, `# j% v - ////$my_post['post_date'] = '2010-07-04 16:20:03';
. K* F# z, M& m# l, `- c$ _) ^6 P
) M. k2 W7 n8 U$ P/ G7 `% P- // Insert the post into the database3 h6 x$ Y$ }9 O- k+ }8 x+ h* u$ |
- $ret = wp_insert_post( $my_post );- z( N. C! h8 @8 O5 L% O* p
- echo $ret;8 y$ y, m, w- w, K3 U. n `* N/ P/ w' u
- ?>
! J" m+ @- S- K& I
复制代码 我自己写的
' w1 ~) O. H. k6 I, G% n插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|