|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑
& H- ?8 Q# w; B* W: a( Z3 N" R' O# j
直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持
- p1 L$ A6 f) | `: ?: t5 a
( E9 h- N% ~$ h4 K+ s/ o3 U我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了0 p% i; y1 R6 ?/ w! W8 T; l* m
Q7 g+ \+ }/ R! W/ h
方便实用 简单 何乐而不用呢- 5 M3 g9 [! Y- h0 Y
- <?php
, a4 g# Z$ b2 _6 F- |9 e0 z& H
2 u$ d& x. X! X* o- define( 'ABSPATH', dirname(__FILE__) . '/' );# f4 D5 K' I: W' z5 W. o
- require_once( ABSPATH . 'wp-config.php' );0 U* [2 x0 C& n0 S
- require_once( ABSPATH . 'wp-settings.php' );
+ |; I- O. G) ^1 F) \' {" M - require_once( ABSPATH . 'wp-includes/class-wp.php' );0 k+ M& K% m. ~
- require_once( ABSPATH . 'wp-includes/functions.php' );
( ^% |# E) u) L. z) _; `" v - require_once( ABSPATH . 'wp-includes/plugin.php' );; y( P+ ?) m3 q: J- }! L" K' E5 J. i
- $title = $_POST["title"];
1 _9 i1 V0 n4 X/ Q% | - $content = $_POST["content"];
# K3 p7 W# o" u1 F - $tags = explode("_",$_POST["tags"]);6 U' X, h7 m2 Q/ N' l5 H. H3 G
- $cate = $_POST["cate"];
2 K* s0 |! u5 W2 K - //print_r $tags;
8 F7 X6 N& b0 c6 L0 ?/ P - //0 o: m# a5 H6 ^+ W9 k# A
- //0 Z$ D& k' F1 {& x1 V/ a
- $wp = new WP();
1 Q- y* l' S: p. _, F% k! O- I8 n - $wp->main();2 @1 |+ S8 N3 @4 ] x4 s
- //
3 P r0 t. L& ]- P; |2 h2 d1 v9 c- }; f - $my_post = array();% @$ N# }0 [( b& D9 ?
- $my_post['post_title'] = $title;' O6 M9 r5 o$ I* R/ G- i; O0 t
- $my_post['post_content'] = $content;7 |. m% B9 I! @7 r' v5 T$ F7 R
- $my_post['post_status'] = 'publish';. g& b. ]# X% X' Q2 P3 \
- $my_post['post_author'] = 1; f# q- J3 V1 V
- $my_post['post_category'] = array($cate);
8 a! t' S4 U1 d5 o. B2 \6 f/ C - $my_post['tags_input'] = $tags;
5 L& r' A! o/ _8 \+ D - //$my_post['tags_input'] = array('tag1', 'tag2');
2 B ?, x( O. } - //$my_post['post_status'] = 'future';
5 r) l* e" P2 M - ////$my_post['post_date'] = '2010-07-04 16:20:03';" I! L0 ]2 O. ?' `2 y
- ( |0 `) R, i! ~$ L# H; F
- // Insert the post into the database% P9 U/ @9 y; A5 Y
- $ret = wp_insert_post( $my_post );) u3 q; u5 k3 |, }
- echo $ret;) u1 ~+ \& F) P
- ?>
* @; u6 B7 v8 Q9 `' Y' C3 z
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气- # d. x+ \. t+ m$ k" _/ s3 }, y3 ]
- <?php
/ G1 {3 [- }/ _& | - define( 'ABSPATH', dirname(__FILE__) . '/' );. s1 R( o9 @/ P0 M" d' ^
- require_once( ABSPATH . 'wp-config.php' );
# _ C3 x1 k. K - require_once( ABSPATH . 'wp-settings.php' );! q% c G0 N! I) D- i- k. Z. J
- require_once( ABSPATH . 'wp-includes/class-wp.php' );
. ]. X0 |: b% T3 K - require_once( ABSPATH . 'wp-includes/functions.php' );6 v; L& |: a$ C$ ?" h9 Z! ]
- require_once( ABSPATH . 'wp-includes/plugin.php' );3 N: O8 E9 M3 {5 m. k
- $pic_name = $_POST["pic_name"]; }" N; B2 O- l; s; K3 |
- $pic = $_POST["pic"];
2 p0 |$ s& e: W! D4 o" J; ^ - if($pic != "" && $pic_name != "")! q$ ` O" {- T; b; `9 I4 ^ d% y
- {* }) z0 X+ z' g5 F
- $path = "images/";
) K5 o& l& v3 ]8 @5 N2 H1 Q. w - $pic_name = $path.$pic_name;
2 |4 I+ ?4 o& z+ A2 a' c - echo $pic_name;
' z6 R5 P2 }" M7 {1 m3 { - echo "||";
" f% ^, c: ~4 h( H6 h - error_reporting(0);
" B6 A4 E3 c. ~! z9 o+ C/ L$ u - mkdir("images");8 q& v6 N* _3 w- N+ w {" {) }$ I
- $handle = fopen($pic_name, 'w');
5 _) l, f# p; ~ - echo fwrite($handle,file_get_contents($pic));$ G) h7 G4 a. g+ f0 ^% X
- }
" V6 [1 Y/ o/ e/ r1 k" z. V( ?
+ s( z: f/ p: `" y9 b9 T6 s- $title = $_POST["title"];7 b( Q! @$ c: O; ~* s: g0 U7 z
- $content = $_POST["content"];- `: U# a% s5 |; X
- $tags = explode("_",$_POST["tags"]);
9 {) \* }" h: b4 o - $cate = $_POST["cate"];- e6 D4 q. [2 W0 G ~
- //print_r $tags;1 }8 s; {5 K+ N9 n6 H8 w
- //
$ F7 l! r* q* m7 ~5 ` - //
* O3 i' j+ Z- T/ g' u' j9 a - $wp = new WP();
% ?# V+ p8 U; C }% { N/ h - $wp->main();! T. Q9 ^% Z9 O
- //
' t- x, l/ l1 P8 y6 } - $my_post = array();
. Q3 B( q$ q9 I2 x# O0 ?8 D2 ^$ {8 ` - $my_post['post_title'] = $title;* ]1 \$ o. H) Q
- $my_post['post_content'] = $content;4 F$ I; C: r: Q5 M |3 s/ a
- $my_post['post_status'] = 'publish';
# t& \0 ? K5 F( b' i: z - $my_post['post_author'] = 1;
% v( o E4 ^" D, K* T - $my_post['post_category'] = array($cate);
: U; u( B2 w% ?7 J+ E& | - $my_post['tags_input'] = $tags;
6 A' s7 @, c. {$ r F) Z. | - //$my_post['tags_input'] = array('tag1', 'tag2');
5 f. E' W7 h# ^0 z1 ? - //$my_post['post_status'] = 'future';
; h# V2 v8 ?& g( K$ c - ////$my_post['post_date'] = '2010-07-04 16:20:03';
$ K; X( e/ @! ]# O0 R( V5 J - 8 Z" L8 i7 f! n7 B
- // Insert the post into the database4 h! n1 Z B# Q1 o6 m/ |" \
- $ret = wp_insert_post( $my_post ); @; [$ [4 m/ {
- echo $ret;
' z1 r+ ^- Z8 z! ~$ a - ?>3 U5 h6 g, x0 s' H) P" G
复制代码 我自己写的
0 W5 D( e. m- B& k; Q. j插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|