|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑
' [3 b2 P6 |, u' ^1 h4 p2 m9 E; o K* J6 \: ^
直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持
3 Y1 ]" r! L' T# \( \, [7 m x2 U3 L! }
我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了
2 E2 z$ U3 ]' Y5 {: w
; J2 a3 g7 R$ x- }3 w方便实用 简单 何乐而不用呢- ; b% c' r! J* ?1 b/ Z1 a
- <?php- [- U$ Z- i+ O# |5 ^) e c0 ]& i9 Y, N
- ' h6 ~% H$ j: o; }6 F X9 x: |+ C5 i
- define( 'ABSPATH', dirname(__FILE__) . '/' );2 Y! t- o" \2 u) w, R2 i
- require_once( ABSPATH . 'wp-config.php' );
5 L3 d! t& d& A - require_once( ABSPATH . 'wp-settings.php' );
% j& f# g; w" Z! S6 U. G - require_once( ABSPATH . 'wp-includes/class-wp.php' );" M% B1 J: C5 w k! ~
- require_once( ABSPATH . 'wp-includes/functions.php' );6 c! O' r3 ]8 R7 N4 u% } w
- require_once( ABSPATH . 'wp-includes/plugin.php' );0 S ~' R8 Y& \% ?7 W/ l
- $title = $_POST["title"];$ M% l, H8 k1 n6 I( E
- $content = $_POST["content"];
% L- e. ]# W' h- @9 q - $tags = explode("_",$_POST["tags"]);
% C" u" a5 H6 N0 E/ v9 o - $cate = $_POST["cate"];+ Z0 ~6 V1 z" n# `3 U. \" \
- //print_r $tags;
/ n7 f0 v6 M3 J- i+ [ - //
% U& | M0 c/ U* T! p3 Y+ U1 | - //
a- W& K8 I' ]4 U3 \ - $wp = new WP();
5 z9 Z$ n2 Q0 U* k3 K - $wp->main();
, O7 ?1 q2 O$ D+ p& N4 X3 J - //
" ~( Z0 h: a ?9 z) g5 @& @ - $my_post = array();
8 B/ @) B! i6 ]' z; G' h1 U - $my_post['post_title'] = $title;3 n. J* h8 x" _$ a+ b, l
- $my_post['post_content'] = $content;4 ~! t& y1 \) R; U; J
- $my_post['post_status'] = 'publish';
1 j1 l' o0 e. v* A+ o: `! E8 ^1 y - $my_post['post_author'] = 1;
" O4 G" q" O- D9 N y - $my_post['post_category'] = array($cate);7 K4 J9 L* f4 q. G \3 E6 r
- $my_post['tags_input'] = $tags;
. N8 x7 |0 n% p4 H! p* ?! a/ k - //$my_post['tags_input'] = array('tag1', 'tag2');
( N. V/ V0 G. _/ z - //$my_post['post_status'] = 'future';; n# z1 a2 X. ^* h1 B7 f
- ////$my_post['post_date'] = '2010-07-04 16:20:03';
# d3 t* g3 ^3 S0 Q* n" `' d - & j$ y+ G) c, s7 L( t2 t/ Y
- // Insert the post into the database
5 n' E5 S& R' U& t# | - $ret = wp_insert_post( $my_post );
5 {+ ~9 u- ~/ {3 T# n Z' Q4 w - echo $ret;
$ R; [7 O4 @ ` - ?>
) [4 Y8 x( L0 u- l, g. S
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气
3 B/ ]* x- \5 ^! q- <?php& H5 L8 R. s0 ~& O. ^# u
- define( 'ABSPATH', dirname(__FILE__) . '/' );! r F# G2 L4 N* d: o Q
- require_once( ABSPATH . 'wp-config.php' );& |% G0 `- a0 p
- require_once( ABSPATH . 'wp-settings.php' );
* T- M6 z1 M' V( u% s3 q* u - require_once( ABSPATH . 'wp-includes/class-wp.php' );
! I, m7 }/ I; R% Q0 L. y - require_once( ABSPATH . 'wp-includes/functions.php' );
) S0 ^2 y- q) ? O' m" ~ b X - require_once( ABSPATH . 'wp-includes/plugin.php' );
- u) _. w) z" A$ J9 Y# ^$ U, Z6 m0 E - $pic_name = $_POST["pic_name"];9 D4 [8 k4 M- S- q) S$ o
- $pic = $_POST["pic"];
% k# o* l5 e# F3 z - if($pic != "" && $pic_name != "")
+ H1 t/ p0 l: e - {) V; w, u/ `. e
- $path = "images/";7 @0 ~) i) _3 b1 g U
- $pic_name = $path.$pic_name;1 \( [9 _5 f. }
- echo $pic_name;' ]8 V6 y! L; O! X( I
- echo "||";( Q9 ], d8 d9 ?; g O0 e
- error_reporting(0);
9 w5 L8 C1 S9 y( ]) }, r - mkdir("images");
; y. l3 s; p3 Y6 K5 V8 N - $handle = fopen($pic_name, 'w');
" W9 h. l' s& c# d; ? - echo fwrite($handle,file_get_contents($pic));8 O8 }/ d* D1 v/ {5 J
- } X& s2 s% G3 q) g2 m8 k+ E
- & O5 e. Y, v7 R
- $title = $_POST["title"];
) p, } c- j! E - $content = $_POST["content"];. R2 I B, S. ?, m! |- i3 |2 b
- $tags = explode("_",$_POST["tags"]);; f) q9 ?% g& r! [3 _2 _6 o! W
- $cate = $_POST["cate"];, M% {- Z+ K. S2 h) b" g/ U: _
- //print_r $tags;, K; p) `4 a% G! T* c# E
- //
. F$ i- c0 \5 ^4 ~ - //4 `/ V1 i% M, s2 j1 m: z8 G$ z2 ~
- $wp = new WP();
4 |9 U2 H8 P4 ]0 {5 q - $wp->main();5 N8 ]% B$ Q0 @8 ]$ \
- //
8 r7 d/ d! E8 Q# F - $my_post = array();7 B' r# H0 b, z
- $my_post['post_title'] = $title;
% [7 A7 V7 t* y9 U - $my_post['post_content'] = $content;# t# [. W2 H+ \" ^2 k
- $my_post['post_status'] = 'publish';. d9 ^+ m. v. G w
- $my_post['post_author'] = 1;
$ j" r& Y, ? D. a! G0 Q: S0 J - $my_post['post_category'] = array($cate);
$ c" c: Z* B3 ~$ g' B' h' H8 ^ - $my_post['tags_input'] = $tags;& O2 w8 q# M2 @7 t# s. W
- //$my_post['tags_input'] = array('tag1', 'tag2');
! W# d2 H. ?& O9 n8 y - //$my_post['post_status'] = 'future';' R3 M& _. T X& h: p
- ////$my_post['post_date'] = '2010-07-04 16:20:03';
: Q" G" m& _8 W/ P+ E - C' P6 o- k) G' ?& }
- // Insert the post into the database0 [ [9 o* ^' \5 ^: _5 {
- $ret = wp_insert_post( $my_post );2 j i2 G$ p" |* ]7 X0 |/ h
- echo $ret;* c% Y. N7 M7 k. x1 ~
- ?>+ a) m9 ]: i( h; {' h
复制代码 我自己写的9 E4 ?6 o9 w* _9 B
插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|