|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑 & F' o$ x$ N$ U6 x8 W* g
% C0 y7 |9 Y8 Z; _5 M直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持; O: |: R ]/ N' F- e; F
! X- l! O1 h- }- X& n我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了
# N+ Q8 t, k. }4 ^' F
- T2 V9 o% B* Z5 P m g1 A, q方便实用 简单 何乐而不用呢
" \3 z4 w! b0 C7 g/ N- <?php
1 e# H# v l+ Q - + r2 B" S( s- s' Y1 C& p, G( U+ D! m
- define( 'ABSPATH', dirname(__FILE__) . '/' );) @- |+ K4 n6 ?8 U8 K' E! ~
- require_once( ABSPATH . 'wp-config.php' );
3 `/ ]* B4 s& x/ O) u0 Y8 L - require_once( ABSPATH . 'wp-settings.php' );
+ t. \: l. G3 D - require_once( ABSPATH . 'wp-includes/class-wp.php' );3 J4 g+ B% i3 w0 d4 N3 O+ n
- require_once( ABSPATH . 'wp-includes/functions.php' );
$ R' |4 `- \1 u: {- ~ - require_once( ABSPATH . 'wp-includes/plugin.php' );- s& h1 |) U( u& ]
- $title = $_POST["title"];
4 a0 u6 a& A% G! U - $content = $_POST["content"];
& T: N* w& G$ Y' B: C - $tags = explode("_",$_POST["tags"]);% O# c% @9 @" x0 h+ U
- $cate = $_POST["cate"];
, p' N* l0 a) I' w( Z - //print_r $tags;3 r' e, F( @+ H" L
- //
8 K. j/ W) L/ X) X" |4 ]. o/ A - //
* F" X/ t6 } m' ~( `6 y# i2 T) J - $wp = new WP();
0 c$ `( r& M6 K, i) P - $wp->main();
) q! Y) Z: K" t. K3 h7 [) U - //
+ v1 o8 p7 O' T$ R! H9 [. g6 S; m - $my_post = array();
( V4 j, ~/ p. m2 D8 k - $my_post['post_title'] = $title;3 c, u# V8 u6 @- C5 Y4 k/ i; _
- $my_post['post_content'] = $content;. f0 K% V ]! G# u( O- e+ L3 @. ~
- $my_post['post_status'] = 'publish';' W2 b4 ?, L6 t1 N
- $my_post['post_author'] = 1;
/ h( l [* l* f5 B) a6 } a0 { - $my_post['post_category'] = array($cate);
. C9 O$ T8 @; k. r - $my_post['tags_input'] = $tags;
/ x4 F0 A2 i& U3 A8 n3 ?- G - //$my_post['tags_input'] = array('tag1', 'tag2');+ e4 f0 H% O( g( \
- //$my_post['post_status'] = 'future';
& Z: z# v8 L5 e6 H. L L - ////$my_post['post_date'] = '2010-07-04 16:20:03';/ E% f# x& W% }* x% o, @' Q1 R" |
- 2 c7 L c: W, V8 G
- // Insert the post into the database
0 J( n' G: O$ y' t( e- K, c - $ret = wp_insert_post( $my_post );7 w( x ^+ [7 A- K" V" }
- echo $ret;
2 W g. q3 D8 p% v! p# O6 b. J" g2 T - ?>% ~; o: a0 L# ~, c8 D1 p
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气
* ]; z8 F$ u$ c: U% V- <?php
/ y7 ^1 _: B9 N7 Y$ ~2 v( K) W - define( 'ABSPATH', dirname(__FILE__) . '/' );
8 i& ?1 ]$ e6 V3 I* c - require_once( ABSPATH . 'wp-config.php' );, D# Q* n3 l' l2 E+ G" {
- require_once( ABSPATH . 'wp-settings.php' );. m7 d& G4 a$ N5 r2 S, L
- require_once( ABSPATH . 'wp-includes/class-wp.php' );% N {5 M& g( M6 z9 D
- require_once( ABSPATH . 'wp-includes/functions.php' );. w- Q( V- c. l' P
- require_once( ABSPATH . 'wp-includes/plugin.php' );
1 Y0 O3 T0 ^: N0 e - $pic_name = $_POST["pic_name"];
$ D- | c7 Y0 K7 _6 D0 H0 v - $pic = $_POST["pic"];- K8 P9 w/ J% e- l2 D
- if($pic != "" && $pic_name != "")
& F" @, b1 \/ a1 V( ]0 y - {2 [* A B! r+ ?; B9 Z
- $path = "images/";' ~8 F7 O5 E1 P+ R
- $pic_name = $path.$pic_name;5 ~' {8 E2 J9 H5 j" y0 a! V
- echo $pic_name;
4 k& y$ s2 S# h - echo "||";
- _. s4 S7 Z. t9 _1 L - error_reporting(0);% v% z. p3 `- u
- mkdir("images");! P' a- P' ~: s+ E* N
- $handle = fopen($pic_name, 'w');
2 m" D' O% O0 }; B4 y% K& y7 g - echo fwrite($handle,file_get_contents($pic));! o4 x$ h& r: D, p* ~/ O6 t
- }8 T. W: u0 _# e) c6 U0 t2 D
- 6 B ^3 _( Y7 z) G
- $title = $_POST["title"];# Q2 M/ s3 o. e+ P- F
- $content = $_POST["content"];
) r% V% `5 d4 w; j - $tags = explode("_",$_POST["tags"]);! P9 j3 H" b: K; D U% ]! c; F
- $cate = $_POST["cate"];
) q8 t' _6 o% _) s: C% e5 Z - //print_r $tags;
7 w( W: d$ T9 o% O - /// q0 G4 w9 T3 [
- //
+ d X' c( O( P9 } - $wp = new WP();& q3 s# ]9 U7 N2 o/ f+ N
- $wp->main();9 X4 _7 f# }' I4 Y& Z. B8 ^
- //+ A. Z& _. e# ^; R1 B
- $my_post = array();
4 h& J/ F9 d0 E- _ - $my_post['post_title'] = $title;
- r. m' T! b. r- h( z! `7 E1 T - $my_post['post_content'] = $content;
1 |3 X+ l3 ~+ b% C - $my_post['post_status'] = 'publish';1 Q# w1 H- B _8 n: ~9 J; u. l! i
- $my_post['post_author'] = 1;9 F0 h9 P' o- h! z9 B
- $my_post['post_category'] = array($cate);
, u% e3 L( n5 y2 k$ T - $my_post['tags_input'] = $tags;1 V0 t8 `$ D J' H4 v B
- //$my_post['tags_input'] = array('tag1', 'tag2');. c# ^6 i) N7 \3 Z* u
- //$my_post['post_status'] = 'future';
. ]7 T9 V; e H - ////$my_post['post_date'] = '2010-07-04 16:20:03';$ p9 i; T+ j2 s
- ) Q5 w8 w* `. j6 M+ S
- // Insert the post into the database
8 H4 _% l, e! l' m - $ret = wp_insert_post( $my_post );4 S( O3 q+ d3 @5 \5 e! `
- echo $ret;0 \" C+ L O8 |0 \! e( v
- ?>
& q* u" }0 _0 x1 E& O) H5 E
复制代码 我自己写的' Q w, x1 G$ b' d2 d
插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|