|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑 4 q9 m5 D$ E2 Y! r9 c
" V8 R$ i% K h( r+ t& V
直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持
( T- M$ K, F0 M7 J2 L
1 v7 N4 I4 I7 d. ^0 U! N; l# n: s我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了
" \& Q1 R+ D4 J& i" G; A E
% u# ]% u3 q* |, @: n& U方便实用 简单 何乐而不用呢- $ q; q; @) U7 q. D7 l( K- [8 S
- <?php# J$ K$ w M2 T( A
- " f/ T8 |( j; O4 t+ T4 b
- define( 'ABSPATH', dirname(__FILE__) . '/' );
2 e7 O" o [: C! ^8 x - require_once( ABSPATH . 'wp-config.php' );
8 q# F" @; ]7 x - require_once( ABSPATH . 'wp-settings.php' );3 L9 Q) ?' e# E3 h3 j1 _5 ?
- require_once( ABSPATH . 'wp-includes/class-wp.php' );
1 f9 i* U% g# g - require_once( ABSPATH . 'wp-includes/functions.php' );
E7 |# W! C0 u$ a' x2 D - require_once( ABSPATH . 'wp-includes/plugin.php' );
" D9 ?1 W5 a' R! i' H6 b8 T - $title = $_POST["title"];2 `8 C5 ]. t3 f. Y1 x! l
- $content = $_POST["content"];% a0 x" G$ m6 l0 |- ~$ l
- $tags = explode("_",$_POST["tags"]);( i/ O- A! T' ?8 z; X
- $cate = $_POST["cate"];, k. ], m# ?4 ~# g+ ~ t, c
- //print_r $tags;" e4 @& l, V0 N
- //3 N; ^, e: }: E# q
- //
9 A8 h4 r' _% G$ e$ c t - $wp = new WP();/ @! e; W+ b$ G! i- C; L6 v
- $wp->main();( t* c: c- D0 y$ M+ i; _$ U
- /// m7 R, \# V! ]& U
- $my_post = array();
7 C- I* w% L$ b+ F3 d- F k1 o- _- R$ S, @ - $my_post['post_title'] = $title;# f" u4 z1 ]1 J8 W
- $my_post['post_content'] = $content;
5 d: q3 n0 W- W& j& \7 ]( } - $my_post['post_status'] = 'publish';
, P$ J: c0 a5 x& O. X - $my_post['post_author'] = 1;
; e% v5 ?1 S3 T* o; D# U, o: v* N" I - $my_post['post_category'] = array($cate);4 _' h: V F5 g/ B5 n
- $my_post['tags_input'] = $tags;1 l, x0 @! K+ C! Z
- //$my_post['tags_input'] = array('tag1', 'tag2');
7 x+ S0 c. M, u2 p0 Q' T* b. v: W$ @ - //$my_post['post_status'] = 'future';
, o: n5 I% H! N; b - ////$my_post['post_date'] = '2010-07-04 16:20:03';
2 p) q6 s" ], a X
; V5 R, h) M$ W: J7 d% e0 b v3 t3 Y- // Insert the post into the database" T5 j6 j5 x0 N+ m4 N
- $ret = wp_insert_post( $my_post );
6 o1 X; n' @# _7 J4 u: e. U' b - echo $ret;. g2 e4 I! M2 m( I+ Z
- ?>
- R& {/ b# L9 E
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气- : C% W. ?+ e' i+ u
- <?php
p7 `- g" L3 O" U/ N - define( 'ABSPATH', dirname(__FILE__) . '/' );
5 k: o a+ {3 n" N# Z - require_once( ABSPATH . 'wp-config.php' );* f# k4 _1 z" M+ c9 }7 v
- require_once( ABSPATH . 'wp-settings.php' );: w! u8 ^4 l1 W! H% t' B
- require_once( ABSPATH . 'wp-includes/class-wp.php' );
. o/ w# l4 p' u1 n - require_once( ABSPATH . 'wp-includes/functions.php' );# g% Y+ U. S' [) |) ^, F
- require_once( ABSPATH . 'wp-includes/plugin.php' );
* z& r3 `3 g0 U, G- t8 E - $pic_name = $_POST["pic_name"];
. O% j! Y: B& V* C - $pic = $_POST["pic"];
8 L! x' i/ U% l; E# p6 B - if($pic != "" && $pic_name != "")* ^3 b& A: S* }- N+ ?- d+ ^
- {0 o6 E6 v# }8 Y7 o" O; i) s7 \
- $path = "images/";
) K$ i+ D9 F* j% C' g - $pic_name = $path.$pic_name;
8 B0 D' T4 q6 u1 a" h0 f, T - echo $pic_name;
9 |* t j+ K4 V - echo "||";) ~' p0 l0 d! ~
- error_reporting(0);6 S$ H, h$ ^5 A' i- d8 A; I$ V
- mkdir("images");; s6 o# t! P6 Z4 U3 \
- $handle = fopen($pic_name, 'w');
: G, _: X8 m+ g& l/ U- n) U" L - echo fwrite($handle,file_get_contents($pic));2 d' ^% [+ H8 }: `
- }
( E( E/ r8 [9 z9 a2 h P - 0 [8 W% E# P& h ?9 D& \' p0 o% j3 U
- $title = $_POST["title"];( _, J) N4 W7 G$ T* P( \- a5 y0 |
- $content = $_POST["content"];
# P1 T- m3 B3 A: t4 S - $tags = explode("_",$_POST["tags"]);) P9 A" r( I) n0 _1 ?- k+ }
- $cate = $_POST["cate"];
. n5 _* m0 ?: T, g, f4 i - //print_r $tags;
& u3 I$ Y0 a6 } - //
% D' d: h: o" ?) e1 m - //4 E7 E5 U/ ^2 F% H) S+ A
- $wp = new WP();( Z J, m1 e. y
- $wp->main();1 }. G) ?+ Z3 z! ]% S
- //4 f" B9 D9 A. _8 s& s9 o( O
- $my_post = array();
4 {& J( w$ [" b* m; w1 I8 c1 N - $my_post['post_title'] = $title;: U4 O6 f6 e z! M) x
- $my_post['post_content'] = $content;
5 } w/ Y( k5 P; b - $my_post['post_status'] = 'publish';
5 c% C$ S3 C, H2 g# t - $my_post['post_author'] = 1;& i: a w' T* r# W! M% |$ e" Q
- $my_post['post_category'] = array($cate);
& C/ K% e w/ } h2 q) F - $my_post['tags_input'] = $tags;
/ p6 n* { d! j5 ~ - //$my_post['tags_input'] = array('tag1', 'tag2');
# ?4 j/ ~6 [8 n7 S* p - //$my_post['post_status'] = 'future';
* O* i- n2 Z J" P' j - ////$my_post['post_date'] = '2010-07-04 16:20:03';- j4 ]! I0 p3 G% `/ s6 m
- % A" Q$ j9 s5 T. y/ O
- // Insert the post into the database
* ^+ |- l4 ^/ Z - $ret = wp_insert_post( $my_post );/ R% j/ |' W( x% z/ b X$ [
- echo $ret;* o0 n- b0 m0 L( [/ d9 J9 ?5 i
- ?>& i5 e u5 k V5 _' u0 D
复制代码 我自己写的- X7 S3 G5 t9 m; g
插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|