|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑
* M' U+ V p6 g0 j# y, z( k
1 a! c4 C( w" V5 C直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持7 U9 R3 n6 K2 a3 N7 f( f* i; X3 z: H9 p
" t9 `1 f* L6 W+ ^- R
我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了
+ ~' s2 @, m& r+ G' k% a$ Z0 Y
: c- L; s0 j% h方便实用 简单 何乐而不用呢
* o% s, o7 f9 _5 K: @" q& \5 h- <?php. G( {! r, P% `* m
, Y) E( w2 t$ |- define( 'ABSPATH', dirname(__FILE__) . '/' );- \; X3 ^5 Z0 w: M8 p
- require_once( ABSPATH . 'wp-config.php' );
1 @1 l6 A( n. }7 e, y - require_once( ABSPATH . 'wp-settings.php' );2 _2 z" l9 [5 Y" q/ p! _
- require_once( ABSPATH . 'wp-includes/class-wp.php' );6 \3 g: g# Y Y' P
- require_once( ABSPATH . 'wp-includes/functions.php' ); e# P- y. V+ w# d, r
- require_once( ABSPATH . 'wp-includes/plugin.php' );
' n7 J( Q% O* [ H - $title = $_POST["title"];. T Y2 U' e: K" R0 d( W
- $content = $_POST["content"];
H0 f' I* O/ m, e! d' M3 W! u - $tags = explode("_",$_POST["tags"]);" @/ A( f4 \& r( `0 H
- $cate = $_POST["cate"]; R) h/ D7 _. V m [) f
- //print_r $tags;
; A& v6 O( k9 S/ A8 C* d# @: m - //# b9 h8 y9 W f
- //
- j- l( `" A' T - $wp = new WP();4 j0 W# H3 @( B7 i: E# l- [
- $wp->main();9 }8 h2 L. G m/ l
- //; Z& _. J, c% \8 R4 z
- $my_post = array();# r5 H0 i5 R m9 Y% s( l6 ~1 F; ~
- $my_post['post_title'] = $title;
7 Z, `3 M, |% w( O" d2 Z; c - $my_post['post_content'] = $content;
: ?+ q1 Z4 }3 z; r4 b' N# P. _. A - $my_post['post_status'] = 'publish';
! z2 { h2 C/ {8 ] - $my_post['post_author'] = 1;$ N0 }( T. r2 u% m1 e) Z
- $my_post['post_category'] = array($cate);" n/ ?8 y& y9 g8 E9 K. H4 K. S' ` S
- $my_post['tags_input'] = $tags;
+ _& T- a- m' Q& a8 t - //$my_post['tags_input'] = array('tag1', 'tag2');
2 T% C4 Q+ j7 X# r/ x8 H - //$my_post['post_status'] = 'future';
! z; ^+ @! |) j6 x - ////$my_post['post_date'] = '2010-07-04 16:20:03';) E4 e; f# D9 e% K. ~3 c+ `1 w
( V# F! Z6 W2 n+ q$ [$ Q3 X- // Insert the post into the database
& ?) m9 c( ?6 ^- ] - $ret = wp_insert_post( $my_post );
# c+ v8 U+ c# ?- l9 u - echo $ret;. Q2 w% O0 W6 i9 c
- ?>* p" @# C& |/ \; R6 j
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气
/ k# A& e+ S7 u9 E8 v: F" V$ ?- <?php R, A0 G+ Z @) ^# V
- define( 'ABSPATH', dirname(__FILE__) . '/' );
4 c3 \% n! O, R) U - require_once( ABSPATH . 'wp-config.php' );
) g3 I D2 r! v& H( u1 f - require_once( ABSPATH . 'wp-settings.php' );$ m+ v1 A; S4 q1 f- X
- require_once( ABSPATH . 'wp-includes/class-wp.php' );+ n* N! i: M$ T# _+ I3 E4 _: Z! D) r
- require_once( ABSPATH . 'wp-includes/functions.php' );) r3 J' e2 H$ O* u: H, z& ^
- require_once( ABSPATH . 'wp-includes/plugin.php' );9 f1 M* M1 b% v7 f0 y, J5 t
- $pic_name = $_POST["pic_name"];$ v* V/ I) d9 _7 s% {. k5 N( D
- $pic = $_POST["pic"];# Q9 l: Q+ F* K8 S9 h0 l& A# L
- if($pic != "" && $pic_name != "")
# k1 o% K7 h/ ]0 i; {% y$ k2 ~ - {. E( y" J2 T0 n
- $path = "images/";% x& `9 L! }2 d% u
- $pic_name = $path.$pic_name;( b! }% J2 V8 X9 { Y. M& n
- echo $pic_name;* q2 R$ U4 O2 y7 q
- echo "||";
) S: s* ^; `6 w, I) X# v- _- x/ d - error_reporting(0);
8 M1 b& q- S: E - mkdir("images");
4 V; f2 Q/ d: p$ n/ ~! u0 ^ - $handle = fopen($pic_name, 'w');
9 o, C0 ]# Z, N& A1 R6 x - echo fwrite($handle,file_get_contents($pic));
/ g F, U5 B+ p - }
6 y8 O; q& n. g. |% a1 j$ S: O - 8 i/ d, T7 ~; r3 O
- $title = $_POST["title"];
" W N' ?# W4 b4 g - $content = $_POST["content"];8 K* k$ D& n" j
- $tags = explode("_",$_POST["tags"]);9 }$ k, k. g8 `; g2 J/ ^
- $cate = $_POST["cate"];7 G! d S) M) W. }' p9 Q
- //print_r $tags;3 G$ i! ?/ e+ H8 W" U9 [
- //& t) }8 \+ R) \" A
- //$ ~1 {% f# @: l9 c1 {
- $wp = new WP();, H9 o, R/ D1 K% |
- $wp->main();. y; C; V) w) s8 t9 q2 ^2 a
- //
( V w) N" w* e5 ~ U% C - $my_post = array();
9 F+ \1 P: s& h% y6 t5 c - $my_post['post_title'] = $title;1 k. e# q4 r) L+ x9 z
- $my_post['post_content'] = $content;
- p- O8 N# ^ j, ?: q - $my_post['post_status'] = 'publish';/ w# A0 T0 J+ h4 U# E2 m
- $my_post['post_author'] = 1;
b- G# \" Y* S. f2 l" {7 c - $my_post['post_category'] = array($cate);
% u" b- ]+ p' s - $my_post['tags_input'] = $tags;
; }" k/ Z# |, c2 W* Z' |* v# I - //$my_post['tags_input'] = array('tag1', 'tag2');1 O+ D" y( ^% }8 E! N! z
- //$my_post['post_status'] = 'future';
+ {! m3 T0 c9 _$ D - ////$my_post['post_date'] = '2010-07-04 16:20:03';* ]/ N0 r% n; c9 g6 l! |- k# w
1 v9 ~% Z7 {4 d' x( m! f+ y+ ^- // Insert the post into the database
" M2 i9 P: b& W8 V+ |3 X - $ret = wp_insert_post( $my_post );
- F0 C; a/ A8 e& L& N - echo $ret;
+ b3 i, M# }/ W ~4 e9 F: }( \ - ?>
+ Z1 T6 U5 \. O% f, c6 N7 ]
复制代码 我自己写的
/ D. w) i4 \3 R9 p3 Q插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|