|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑
( J: D1 U& q$ Z+ M
& L; O J$ z7 I$ }0 C直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持! g7 @4 x1 T5 d2 _9 [& g9 g* v
: }. Z# V5 o# D- v$ ]我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了8 w S2 N! g5 T; Z
) U* P+ |1 {6 ]3 \1 J方便实用 简单 何乐而不用呢
+ Q( y; H( T8 f4 W( y9 q# b( h- <?php
6 P9 G% a" u& D; p, T4 D
) q( Q& c- d! S0 m# C. n- define( 'ABSPATH', dirname(__FILE__) . '/' );4 P+ L" T$ R, v' _9 u& u
- require_once( ABSPATH . 'wp-config.php' );
. ?& {# }2 ^" d" r4 h - require_once( ABSPATH . 'wp-settings.php' );6 |" j& c5 m3 g( G$ u/ }% k3 N
- require_once( ABSPATH . 'wp-includes/class-wp.php' );' w0 l. T* q2 h7 l3 s- I% E
- require_once( ABSPATH . 'wp-includes/functions.php' );
* {% r& O" q! i" ~ - require_once( ABSPATH . 'wp-includes/plugin.php' );
& {- c2 ]9 x# ~5 \ - $title = $_POST["title"];! L3 ~) \# `6 Y6 b
- $content = $_POST["content"];
* _4 b" I3 ?% J' v/ ~9 T3 i - $tags = explode("_",$_POST["tags"]);) ~8 C H& H+ o: ]' w
- $cate = $_POST["cate"];
1 o7 L/ [ r. I7 j/ r' B8 \ - //print_r $tags;
9 y# _2 [9 ]* j - //' y* f: o7 x( r
- //
5 i& V3 g2 w$ d% s$ a - $wp = new WP();
8 m, b3 V8 `& {' q - $wp->main();2 t: x+ |* I: _ m
- //
6 X& w. L! O* r+ }- p5 C; p - $my_post = array();
9 k: K7 _8 G5 y b0 z - $my_post['post_title'] = $title;- _) a2 w+ p1 q4 ^6 s
- $my_post['post_content'] = $content;! P1 ~2 l* ]' O( H5 Y/ n
- $my_post['post_status'] = 'publish';7 A' \! h( E3 S& _
- $my_post['post_author'] = 1;$ ]! e& A3 o. N
- $my_post['post_category'] = array($cate);
& x4 p9 b5 Y( e2 a6 N F! L' x - $my_post['tags_input'] = $tags;. s6 X- W* w9 y6 u- c% X, e
- //$my_post['tags_input'] = array('tag1', 'tag2');: r7 p, ~) t6 i5 I
- //$my_post['post_status'] = 'future';! F1 H$ s( Q( W; D. {
- ////$my_post['post_date'] = '2010-07-04 16:20:03';
+ A9 c+ p9 Z8 E# r. |. c }
d* D5 h$ o4 h) U# z$ {/ e- // Insert the post into the database) y1 g! x: k3 J5 ?( z! }
- $ret = wp_insert_post( $my_post );
# H4 e8 T: j3 I - echo $ret;
0 i S6 n( N* b' L6 _ - ?>7 J+ o ]/ D& S0 l9 F
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气- * A0 m) ^/ q) d1 r9 m
- <?php
% e. L# t1 k! C9 Y2 Z! K9 | - define( 'ABSPATH', dirname(__FILE__) . '/' );, j' q3 h+ S& h$ P- D
- require_once( ABSPATH . 'wp-config.php' );
" t ^; f/ Q# P7 ]) N - require_once( ABSPATH . 'wp-settings.php' );
, L! j" C- k+ T2 i6 d- ]6 W - require_once( ABSPATH . 'wp-includes/class-wp.php' );: T* J' c9 [( y6 x: ` u
- require_once( ABSPATH . 'wp-includes/functions.php' );! Q+ S1 u8 Q9 e* i6 d# z7 d2 e' D
- require_once( ABSPATH . 'wp-includes/plugin.php' );
/ U; ?3 W2 h) ~6 l - $pic_name = $_POST["pic_name"];
/ n/ D" z$ I7 @0 A9 e" N, L3 | - $pic = $_POST["pic"];3 U1 C: @/ i7 V# x$ ]) D
- if($pic != "" && $pic_name != ""); Q' W3 C: Z9 H- ?" j% \1 c
- {) F& |# q# F& I1 S
- $path = "images/";! w) r$ F5 d9 Q7 [! o. Y4 ~0 e8 I
- $pic_name = $path.$pic_name;, L! Q( [) _8 e$ x2 G
- echo $pic_name;3 l1 { G7 f0 _: i
- echo "||";! L0 b4 i. {$ @% T/ S
- error_reporting(0);$ E! @0 U. A' L) B& M# t
- mkdir("images");* C6 i8 t F4 K( K. {
- $handle = fopen($pic_name, 'w');
h( \) `" O+ V9 n( X3 m, g - echo fwrite($handle,file_get_contents($pic));: d- ]- [! I' A
- }
7 O4 f9 x1 E5 J+ ?8 n2 C+ l0 G
5 z6 Z" U, [% O2 k4 j! c- $title = $_POST["title"];. T7 U2 F1 w7 v8 T4 E
- $content = $_POST["content"];
! f8 S. }: y7 m8 g/ u9 m - $tags = explode("_",$_POST["tags"]);1 ]0 m8 t4 m/ ?0 O. m) h
- $cate = $_POST["cate"];
+ Q* _) W) V+ f0 R, L& y T - //print_r $tags;
6 Y. A" D% @% u' C - //9 _1 ^3 s) k1 C# p7 `
- //
- [2 @% U4 Q4 _$ b* O7 w - $wp = new WP();( m# K( y- q( k( y2 I% @
- $wp->main();9 o1 y W9 V2 U
- //) e7 y; L5 |, I4 V. s& n
- $my_post = array();
1 \& x( m% P6 }+ r, k$ Z - $my_post['post_title'] = $title;4 B9 D6 N* F! O: Z- t# F3 `
- $my_post['post_content'] = $content;: R* v0 A2 [, H% Y0 x+ M
- $my_post['post_status'] = 'publish';
! K. K. {5 X: u5 a6 F - $my_post['post_author'] = 1;6 D- w; }+ [) W$ Z6 B6 W
- $my_post['post_category'] = array($cate);
' H. y9 w6 K1 L0 k# L - $my_post['tags_input'] = $tags;% ~9 [% }' n" J- U/ _
- //$my_post['tags_input'] = array('tag1', 'tag2');! \, O3 f S9 i" [( W
- //$my_post['post_status'] = 'future';
/ e% p# ? {5 }# Q - ////$my_post['post_date'] = '2010-07-04 16:20:03';
% A( C2 j' }5 u. \+ ~
4 B: L: t5 l, p% X& ] G- // Insert the post into the database9 Z# _% A }7 ^6 I( G* K* k
- $ret = wp_insert_post( $my_post );8 x# {* L* u$ q- `- V, S) `' [3 v
- echo $ret;
& n) E: r2 ] c4 K - ?>7 x% W2 I+ o1 S/ B
复制代码 我自己写的2 C& q) d0 R" H. s! F& f4 w
插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|