|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑 2 F6 C0 R1 @) D5 D5 z1 m
+ A/ E. u4 z4 U. z2 ~直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持
: c) S) R+ z; t! S$ A& b" S) t+ w1 P9 a& \9 \; D4 E- S! e- O
我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了) t& ~. @# y7 ^0 K: \# D
! J2 p0 _( Q- w) X$ O7 u
方便实用 简单 何乐而不用呢
) H3 q) B. k+ L o o9 E; G- l- <?php
% b4 A4 y/ c8 C b* n+ c- P5 ]
( ?# J9 \' n' C! u! A5 v! _- x% I- define( 'ABSPATH', dirname(__FILE__) . '/' );6 O& n3 O4 ~) ]
- require_once( ABSPATH . 'wp-config.php' );* Z+ t4 V' W$ ^. X: h
- require_once( ABSPATH . 'wp-settings.php' );% |& A! d6 ?7 }! c" F0 z3 `
- require_once( ABSPATH . 'wp-includes/class-wp.php' );
j8 w' S6 ~7 T7 k0 v - require_once( ABSPATH . 'wp-includes/functions.php' );1 b' C1 `* O% {
- require_once( ABSPATH . 'wp-includes/plugin.php' );# e9 }4 E2 X7 W O7 R ~
- $title = $_POST["title"];
7 n* E6 l4 n6 g - $content = $_POST["content"];0 J$ U& \* S0 T+ x5 K
- $tags = explode("_",$_POST["tags"]);
4 N A& W8 R3 e3 d( g - $cate = $_POST["cate"];
' c; u* D0 C1 x5 I - //print_r $tags;
' Q2 Z* r" s7 j; F. |8 }, r - //
* {0 S1 V3 `( `, O - //
4 }6 `! K9 t9 E. G2 Z" ?7 W - $wp = new WP();
3 ?$ w- {1 W( j) B3 G - $wp->main(); L. ^( t' x1 ]9 ?
- //. I* a j" E8 O4 U8 } b7 y4 ~
- $my_post = array();2 y) y6 }4 v1 p* x
- $my_post['post_title'] = $title;9 S, o2 z( I% f% T; M
- $my_post['post_content'] = $content;' O! I1 Z6 o8 R* I1 Y( [$ _
- $my_post['post_status'] = 'publish';; h- S, c* e/ f {% s
- $my_post['post_author'] = 1;
! E5 q6 a7 H! \3 p" B, G - $my_post['post_category'] = array($cate);
. R# a$ c- l9 N+ D; @4 F$ t( G - $my_post['tags_input'] = $tags;# |. s m$ e5 E1 B; H$ x
- //$my_post['tags_input'] = array('tag1', 'tag2');" p8 |: ~( ^3 x4 \( W
- //$my_post['post_status'] = 'future';2 {" Q+ w' c6 `0 w/ P
- ////$my_post['post_date'] = '2010-07-04 16:20:03';7 b, K$ K% J9 Q# t' Q/ g* F
3 }) t. b7 R* r$ S* \0 `/ s$ S- // Insert the post into the database$ s- b; |, c9 ^6 |
- $ret = wp_insert_post( $my_post );
# Q$ g$ \( u, k9 W( g( e - echo $ret;; R, M2 t) S# }( i" N. X/ J
- ?>
+ I, `) h2 J' l1 T# Z* [! W
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气
& Q# O/ B6 L9 R1 G- ?1 j" H1 `( _( V2 m( r' n- <?php
2 l' r, M% |5 t- ~ - define( 'ABSPATH', dirname(__FILE__) . '/' );
7 a8 P+ l6 o" E7 \& w' Y - require_once( ABSPATH . 'wp-config.php' );" ?# W6 K$ ]) b. ?# v* c: Z
- require_once( ABSPATH . 'wp-settings.php' ); @4 ]# o2 s; e0 R3 t
- require_once( ABSPATH . 'wp-includes/class-wp.php' );# i1 ~2 W7 @% r1 X: z7 L% K
- require_once( ABSPATH . 'wp-includes/functions.php' );# |: l, y T+ h4 @
- require_once( ABSPATH . 'wp-includes/plugin.php' );8 n$ u% \1 {# E3 r" O0 ^9 \! ~7 ?: h8 F# F
- $pic_name = $_POST["pic_name"];
0 t. t! d) P) L8 m2 b - $pic = $_POST["pic"];
% k$ |0 g4 Y: V K - if($pic != "" && $pic_name != "")3 X. H% F }4 @8 y# \, ~: Y; G* _. ~
- {
$ F3 e! \8 ~7 O& y - $path = "images/";
) C( r& K7 C: R& d - $pic_name = $path.$pic_name;
& V/ }+ o5 K+ c+ F' B+ h+ n - echo $pic_name;
- W, A& G3 L8 O: p - echo "||";( p3 q& Y# s4 H( i4 J9 O
- error_reporting(0);
0 ?/ i7 q( k! n6 j - mkdir("images");
7 T& i" X2 j: p K! | - $handle = fopen($pic_name, 'w'); O% Z7 T X8 ]2 I5 a
- echo fwrite($handle,file_get_contents($pic));
) m6 d+ k: M1 }5 X% X; O3 w& o, h2 g - }( \# N( H( o% G4 l7 G5 w9 ~% f0 V
- * Z1 W/ e8 I: ]5 n& J7 N+ Q
- $title = $_POST["title"];* @$ J, u3 q" x& T
- $content = $_POST["content"];% h$ u. T7 g8 f, e1 ?. p
- $tags = explode("_",$_POST["tags"]);3 e+ F+ D' u) B ^6 ]6 a' D
- $cate = $_POST["cate"];
" H" F! ^) j" w - //print_r $tags;
2 l' [) o7 ]* D, b! S- |" ]8 m - //
; G4 d6 P+ j/ L0 L# V* L. w+ C2 ? - //
. y' s( t* [ z2 q( T0 t9 ]8 q5 M - $wp = new WP();
: Q0 z) n. X' X' u - $wp->main();# q. {5 l2 A: T
- //
- M, y# M: z4 ?1 K - $my_post = array();
0 u0 @( t9 u$ y3 N7 N& L) G% s* _( q - $my_post['post_title'] = $title;9 j4 C$ O* ^' E0 K' O
- $my_post['post_content'] = $content;
; u6 q+ `1 y, q9 m6 J - $my_post['post_status'] = 'publish';
4 J( y" ^5 U M2 d, N - $my_post['post_author'] = 1;
' q( L: {+ w6 k7 f* [4 o4 }) T - $my_post['post_category'] = array($cate);
. o2 ]& b8 |0 {" b% _ - $my_post['tags_input'] = $tags;! ?! w* D( p' x' T6 e
- //$my_post['tags_input'] = array('tag1', 'tag2');# r& A; z. Z. b- I# I
- //$my_post['post_status'] = 'future';
6 r. u% ~' J' b* O* \' t - ////$my_post['post_date'] = '2010-07-04 16:20:03';
! [: r; R* X% g& G/ s+ K - # v- E& Z# r7 ~% @5 o# t4 L) S" z
- // Insert the post into the database K/ p. D% m% e' ?. P' c" M
- $ret = wp_insert_post( $my_post );! K" ?; C9 o2 Z; N9 q3 U
- echo $ret;& f" E4 k. p( [; q+ ^
- ?>
$ X9 M: _: \8 z, A6 w& i5 `$ `
复制代码 我自己写的
( a, O. j: K M插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|