|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑 * h. q$ X5 m1 _
% G3 l" Y; u7 _+ ^) ^直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持1 |5 R- f) b4 a8 Q( [
/ x9 q+ b3 @. f" @! p我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了
/ `9 q- m& `4 k+ B% R0 F. l" `9 m! m4 e
方便实用 简单 何乐而不用呢
! r9 z/ q0 V/ C! K9 z& \( c2 z; J8 S/ L- <?php2 M9 ^5 N0 h5 m7 _
- 2 |9 a1 e6 V0 A
- define( 'ABSPATH', dirname(__FILE__) . '/' );
" t9 m. M+ Q/ s q' J - require_once( ABSPATH . 'wp-config.php' );
4 o" a/ c! t, F6 v7 H& D - require_once( ABSPATH . 'wp-settings.php' );( L$ k; n9 G" c$ i/ d0 ]+ w6 A: E9 |
- require_once( ABSPATH . 'wp-includes/class-wp.php' );) `! [8 j& J* B* K
- require_once( ABSPATH . 'wp-includes/functions.php' );" b8 Q. F" O7 ^& Y) T% @( d
- require_once( ABSPATH . 'wp-includes/plugin.php' );
) I7 ^' i6 ~* p G: j2 ~0 C - $title = $_POST["title"];* b$ x+ M$ [. |9 Q
- $content = $_POST["content"];
2 p6 ^" d9 W& Q, t2 y" Y6 c - $tags = explode("_",$_POST["tags"]);
. D$ c* b) X9 n# P$ m - $cate = $_POST["cate"];
! f) Y- H/ c2 @% ^" k1 B - //print_r $tags;
; ]2 R) @! _" ~2 v3 ^ - //. b, r. B) n% d
- //4 k* p; L& O8 z) g- d4 e3 n
- $wp = new WP();
7 W8 t/ A9 H" F# b9 { - $wp->main();
/ a1 J$ p y. s - //
" W! r8 y3 ?* Z$ |) Z5 Z7 ~* k7 G - $my_post = array();
& _9 {$ t8 i7 L$ C; @9 B2 e - $my_post['post_title'] = $title;9 A! y$ X) U0 c7 _* }( h
- $my_post['post_content'] = $content;
/ }3 g9 q( p) } - $my_post['post_status'] = 'publish';
2 {$ q0 ~9 ~9 m% A9 x+ ~ - $my_post['post_author'] = 1;
/ t4 ~, a p X5 |# F* ^% x - $my_post['post_category'] = array($cate);
: _) |* p8 D4 ?/ o3 Z6 ^ - $my_post['tags_input'] = $tags;
8 K, t m. q7 t/ |. g5 R - //$my_post['tags_input'] = array('tag1', 'tag2');
9 y5 E0 j- f# l8 ? - //$my_post['post_status'] = 'future';
5 X# ?" }, q8 E' I% k% s$ a3 N* | - ////$my_post['post_date'] = '2010-07-04 16:20:03';
. K% e- [. H! t- u7 f
0 M& g7 a: ~, h- // Insert the post into the database
" a# E" e: C- d9 w- X4 W" M! M4 Y - $ret = wp_insert_post( $my_post );4 b6 g3 q! A6 K
- echo $ret;) N6 l: q. a. w- T( @
- ?>
; s8 u# b4 o: z# C- I
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气
7 R$ L, s9 v/ f! M7 g6 @2 Q- <?php, ] K% |6 _& W* I: H
- define( 'ABSPATH', dirname(__FILE__) . '/' );" a) X5 d3 G& K c+ R
- require_once( ABSPATH . 'wp-config.php' );$ M6 f: i: j6 _% f( P' R5 y
- require_once( ABSPATH . 'wp-settings.php' );1 m0 K& {( `1 @/ s7 W
- require_once( ABSPATH . 'wp-includes/class-wp.php' );6 M' C6 N3 ?8 f* u
- require_once( ABSPATH . 'wp-includes/functions.php' );. Y# v3 {7 E9 `; a4 F
- require_once( ABSPATH . 'wp-includes/plugin.php' );
% a" d+ Z4 X* V2 B - $pic_name = $_POST["pic_name"];
* N3 i' H1 S. n3 n - $pic = $_POST["pic"];1 B* }: _9 `* P! o
- if($pic != "" && $pic_name != "")3 V4 ?5 n% K7 |
- {7 T& l6 a' C/ p* Z9 E: E: |
- $path = "images/";: h' f% J, N6 t! f, u7 k
- $pic_name = $path.$pic_name;' M5 r$ P- v3 r4 ?8 F. }
- echo $pic_name;
; T7 O! G" g* V' o - echo "||";
* i, g0 `% _5 _ G0 Y8 Z - error_reporting(0);- o1 R# v' R/ [4 c
- mkdir("images");
" X5 I3 |2 E0 k( W0 T: {" X/ G& n - $handle = fopen($pic_name, 'w');" ]; l# i" E6 _' ]' v; i# N
- echo fwrite($handle,file_get_contents($pic));5 ?4 z2 l$ a- V; \5 b5 T' ` e. R
- }
% P G8 |# j& E! O* E# b
; {5 m2 j! `2 d5 I- $title = $_POST["title"];
7 j; L7 [8 f F, \: T+ n4 ~% A9 ~ - $content = $_POST["content"];2 D8 H; |7 N' Z( C
- $tags = explode("_",$_POST["tags"]);3 F! ~1 t: u2 O5 }# l) T
- $cate = $_POST["cate"];2 z* v6 n( L) g* c( C
- //print_r $tags;$ L$ E6 k" ~, B7 y/ h( H6 g- ^
- //0 F } U" b: K1 ?- t% I: ]* t
- //
5 O' Y8 Z! [. R6 m) ]; |3 i - $wp = new WP();3 z$ ?4 W4 H: R- M
- $wp->main();
" h( L$ X; B/ }; p - //
$ P# W# r, M3 }, i1 y - $my_post = array();
: T; W% C+ v# [4 ~ - $my_post['post_title'] = $title;& l/ |( E3 H' M6 I7 H0 {' K
- $my_post['post_content'] = $content;2 _ _9 l) A. h8 ^# L
- $my_post['post_status'] = 'publish';, t8 T. O3 E- P) ~7 ]5 ?1 w
- $my_post['post_author'] = 1;* {, r% \" c, Y8 D
- $my_post['post_category'] = array($cate);6 N0 [+ n/ d# t" z( w
- $my_post['tags_input'] = $tags;$ X; ~, @; E1 i' X" ~0 r
- //$my_post['tags_input'] = array('tag1', 'tag2');# ^5 j+ F2 E6 n3 q- F2 S, Q( G
- //$my_post['post_status'] = 'future';
* r- m7 r5 k$ O9 w6 |5 ~; r0 E - ////$my_post['post_date'] = '2010-07-04 16:20:03';0 Y1 o$ x8 {5 U" d# |
% F9 D8 E3 Y7 N: p" v- // Insert the post into the database
. X2 \. p# X0 w' V( y1 D5 J9 i - $ret = wp_insert_post( $my_post );/ ]! F9 C( C6 ?8 N4 C0 a- e
- echo $ret;
: h5 l5 Q9 y# @$ }8 G4 Y6 P - ?>4 g# C; v+ N- p7 Z$ Z- `% q
复制代码 我自己写的
v; u X3 M( o, m0 L插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|