|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑 9 x5 y# u; s6 n1 z
U* f9 B1 d" B4 s5 o直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持" Q( q3 t# ~2 y7 c+ |
* e5 |/ T' {1 A, n我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了5 W2 k% N$ {7 `; N) a: s
+ x' C+ f K9 M; l9 K方便实用 简单 何乐而不用呢
. D2 i, d+ L Q# Y- <?php
! N- H; y6 ^+ s; X
6 w, Y G5 v; d9 D7 k- define( 'ABSPATH', dirname(__FILE__) . '/' );
K1 Z" C7 Z1 P2 [+ | - require_once( ABSPATH . 'wp-config.php' );
& ^1 X+ P, o, [' S; N, T" C( j - require_once( ABSPATH . 'wp-settings.php' );/ X% x9 c: H$ j
- require_once( ABSPATH . 'wp-includes/class-wp.php' );
1 k+ I% k( V; L8 Z - require_once( ABSPATH . 'wp-includes/functions.php' );
; X7 F! W, l9 x# A" K - require_once( ABSPATH . 'wp-includes/plugin.php' );& Y. L, E8 Q' Y- e
- $title = $_POST["title"];! m: Q2 L" H. @- i% C
- $content = $_POST["content"];
- n% p5 [, F5 t - $tags = explode("_",$_POST["tags"]);" |! m5 L, m5 V
- $cate = $_POST["cate"];) B7 p. `# B2 X- H$ m7 z9 ]% `
- //print_r $tags;# o) S0 [5 b* U
- //( t# @: y& S& u$ t
- //- l. @6 k8 Z+ U+ v* m, _; O2 n0 b
- $wp = new WP();
# {* W2 J2 J5 ? E - $wp->main();
6 } ~4 I: A4 h6 ~6 p$ m* f - //8 t, \ w v- ]7 \9 K9 M
- $my_post = array();* j. r' z) n" j
- $my_post['post_title'] = $title;8 ? T ~. H: U* S
- $my_post['post_content'] = $content;
_( o/ A. [; C7 J# L ^ - $my_post['post_status'] = 'publish';) K: A) u. |/ c+ `
- $my_post['post_author'] = 1;
) s! r1 N% n, R. q$ m8 g - $my_post['post_category'] = array($cate); Y0 H; d- z8 z4 U. e
- $my_post['tags_input'] = $tags;
& j# y5 j! N% t- H: s7 O - //$my_post['tags_input'] = array('tag1', 'tag2');6 n4 K( ]) E( R8 B$ s9 R r. x6 {
- //$my_post['post_status'] = 'future';
$ M6 c% _# g, L1 c - ////$my_post['post_date'] = '2010-07-04 16:20:03';0 _0 B6 E e' J
" O. r' D# H2 I: [- // Insert the post into the database1 Q) s( M3 X( x+ b+ L) d
- $ret = wp_insert_post( $my_post );0 [) W5 p0 }; k3 l
- echo $ret;0 z5 O8 a) A- e) j; w
- ?>! A# S o1 W% g: e; l$ X3 y6 C
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气
- w, @0 H; Q! S4 A- <?php3 n I7 }. M2 F) R" f3 w N
- define( 'ABSPATH', dirname(__FILE__) . '/' );
! g5 M# e" R. R5 i/ l6 w3 G: w - require_once( ABSPATH . 'wp-config.php' );
8 ?" I! r7 J* Z% D - require_once( ABSPATH . 'wp-settings.php' );
2 F+ F4 F2 E. L' X* O& t5 d0 ~ - require_once( ABSPATH . 'wp-includes/class-wp.php' );9 B2 }% M3 R' w: u( V
- require_once( ABSPATH . 'wp-includes/functions.php' );
/ V) {" Q- i, w: |5 ^$ R - require_once( ABSPATH . 'wp-includes/plugin.php' );
7 H7 [4 b' g7 N9 B k* q' Y - $pic_name = $_POST["pic_name"];
$ J$ a# J+ g" C8 k0 t - $pic = $_POST["pic"];0 d: }8 M- W e0 C( R
- if($pic != "" && $pic_name != "")
, @2 m/ Z# G X& \- e* b7 y - {
8 w. @, V( C5 o* [- H& f, D - $path = "images/"; s8 p4 f' l' g
- $pic_name = $path.$pic_name;- ^- ]/ `! K2 ?* ]- ] E
- echo $pic_name;
; W& m% b2 G& G5 `* v! C* Y - echo "||";0 ]( |: h/ x# X; t( C
- error_reporting(0);6 F- w/ ], }& q
- mkdir("images");
3 _3 y6 d" H' @; p* J; F - $handle = fopen($pic_name, 'w');! {! n+ C' ~# x7 u
- echo fwrite($handle,file_get_contents($pic));+ h$ u+ w& H: Z- D4 f8 u) {
- }& I' z) p) p" u& j' o2 o$ T0 Q
- + S/ S9 ?- k, h4 `) r5 h8 Z- G
- $title = $_POST["title"];
! f8 x. U C3 P3 C0 y Z+ W0 z' r) i - $content = $_POST["content"];
8 ^. y! ~ ^3 ~3 Q3 i0 t- m - $tags = explode("_",$_POST["tags"]);7 n' q8 }9 E2 S* B9 h0 {& O, S
- $cate = $_POST["cate"];
( @/ z2 E0 O8 b C - //print_r $tags;$ C6 r' |* a+ z* Y n
- //
7 {2 T' J* L/ V. Q) O& n - //
! t! s8 V( N0 h1 b+ N' L4 J - $wp = new WP();
2 |: ?; X8 ~$ j |% x! Z2 I' Z o - $wp->main();( E# g9 I9 | r
- //2 Y5 E0 l: [- N+ L# K: ]# Q
- $my_post = array();
9 O4 r* t' h% T- i q2 e - $my_post['post_title'] = $title;
+ O: U$ l9 b6 B - $my_post['post_content'] = $content;
- Y z! `5 A9 O - $my_post['post_status'] = 'publish';6 I, `9 i; D" ^4 m: f1 x
- $my_post['post_author'] = 1;
+ U; B5 m3 f' [4 [7 F$ G8 T+ ?5 d - $my_post['post_category'] = array($cate);
/ ?' s% o7 K2 a$ s# j5 | - $my_post['tags_input'] = $tags;
! _6 {5 e2 U; Q+ S `7 e) E2 } - //$my_post['tags_input'] = array('tag1', 'tag2');9 }- _' D3 c' p1 x# @9 M4 }
- //$my_post['post_status'] = 'future';) [8 T. W/ y |4 X# [, t
- ////$my_post['post_date'] = '2010-07-04 16:20:03';
9 k5 _+ i! T \8 u. \
( _- `- j( z1 r( A' V, _+ f: B) u- // Insert the post into the database( @5 F2 ^, i& X6 Q7 C5 B
- $ret = wp_insert_post( $my_post );* m& J+ f4 f5 x! b/ N( \ G
- echo $ret;
) U/ D% l7 W& G: k& K) f - ?>
" l1 f& U2 o# X5 a& R$ z5 k4 p
复制代码 我自己写的7 H& S/ K6 K/ l+ j
插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|