|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑 2 Q1 O# ~% `1 `8 ?, r. X8 D% n' Z5 }
! \% @+ [& V7 q6 p% I直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持- v" P8 z* E1 H$ g* H$ d
: Q+ E# C6 |6 ?+ g! H& V: Y我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了- k; u& u% c4 K+ E+ Q4 P3 a
1 Q" s9 Z K7 f; O! ^8 [方便实用 简单 何乐而不用呢- ) n" ~, R1 N' d* J1 f- g
- <?php
2 M9 i) k; Z* N7 R1 y$ ? - 2 }1 C* C4 F+ w4 ~% U) A# l
- define( 'ABSPATH', dirname(__FILE__) . '/' );- Q2 u* ?4 m- q: f* n, ~# |
- require_once( ABSPATH . 'wp-config.php' );, N" b$ l8 Q) P- Z( e0 T( r
- require_once( ABSPATH . 'wp-settings.php' );
U: x a+ u& V7 g9 M - require_once( ABSPATH . 'wp-includes/class-wp.php' );6 J- Y8 @# b+ T+ }
- require_once( ABSPATH . 'wp-includes/functions.php' );+ W2 D. ~( K* P
- require_once( ABSPATH . 'wp-includes/plugin.php' );
9 z& N! K: h1 W - $title = $_POST["title"];; s" T$ v2 i4 l. n3 f
- $content = $_POST["content"];9 X. Q& U- C; t5 l0 d4 V3 p
- $tags = explode("_",$_POST["tags"]);4 H3 w5 t/ P3 n& b" B" H& I
- $cate = $_POST["cate"];
( Y# l$ x% k9 N# B- { - //print_r $tags;
" r& B" J* \. _* ]* K9 t! D - //1 e1 \, H) v' Y' N5 [
- //
# _& N, `4 ~% J - $wp = new WP();/ f! o3 T- c! f: R" [
- $wp->main();* V& C% M1 M* V1 G2 E' N$ l* Y; n
- //0 e+ v( t! t. l" b
- $my_post = array();
. q1 J4 d1 A: H# V/ B) K - $my_post['post_title'] = $title;
7 M9 n7 [( L1 o+ m @# v) m9 e" d - $my_post['post_content'] = $content;; [1 _* i' L% }. O M* o
- $my_post['post_status'] = 'publish';
+ v8 \6 L- e8 u8 m2 J' L$ r - $my_post['post_author'] = 1;8 f# D5 U+ L$ ~3 p5 ?- C
- $my_post['post_category'] = array($cate);9 C! Q# m. | ~* T4 k
- $my_post['tags_input'] = $tags;
& ~- {# i3 V9 b4 v+ [ - //$my_post['tags_input'] = array('tag1', 'tag2');
& a4 {, Y& X r+ X) K) E4 ]' J2 ~ - //$my_post['post_status'] = 'future';7 B' n6 ] g" ^# P
- ////$my_post['post_date'] = '2010-07-04 16:20:03';( V6 B$ u% r4 }8 F7 q: x
- 5 \& k5 X& z- f; t( J( |( x
- // Insert the post into the database
+ q' O4 p, L5 y' J2 V! ? - $ret = wp_insert_post( $my_post );2 D9 A F! W- }0 K3 ?# q( e! h
- echo $ret;
' F8 w, U/ o- {2 I0 G; P - ?>7 f" P! \, K7 K- U, `6 N
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气
2 k- m7 Q) I+ T2 Z5 ]- <?php
+ r8 R4 Q. z- A - define( 'ABSPATH', dirname(__FILE__) . '/' );
$ l& Y n* x" x3 k - require_once( ABSPATH . 'wp-config.php' );
4 G( B2 m5 G% o" y) V - require_once( ABSPATH . 'wp-settings.php' );% {$ ?2 O1 |( j9 Z/ |- p" C
- require_once( ABSPATH . 'wp-includes/class-wp.php' );+ ~# R6 {0 D6 [7 v
- require_once( ABSPATH . 'wp-includes/functions.php' );
' Q' D: Y; L0 \& B L9 b - require_once( ABSPATH . 'wp-includes/plugin.php' );
/ U2 L/ M# T5 h+ c8 V - $pic_name = $_POST["pic_name"];
" c3 B" ~$ E! s! j - $pic = $_POST["pic"];& S# k+ E0 Q2 E j1 D1 J/ _
- if($pic != "" && $pic_name != "")
3 K5 H* T, a7 w \3 v0 Z! Y - {
- K, U z' u. z+ u! {5 A5 L8 d$ R9 L - $path = "images/";) G+ Q5 w6 L& |, f2 @- r, E; m$ w2 v
- $pic_name = $path.$pic_name;
, f/ [8 T0 I: Q6 s# k: u - echo $pic_name;6 @2 S5 z" I7 |2 F6 a, C! w4 R! i" p
- echo "||";
. ` [) J: K6 b - error_reporting(0);
' `9 ~, ?6 i8 M4 r5 e - mkdir("images");( \ a u, C' I$ D( a
- $handle = fopen($pic_name, 'w');1 M- u v4 \! ]) S. D" N5 H
- echo fwrite($handle,file_get_contents($pic));* u4 w# B+ Z! s1 X
- }9 K7 p# p* ?" x. J7 i
1 W# B& `& S7 o* E8 v# h- $title = $_POST["title"];7 V5 A5 s( s. H( G! ?: t
- $content = $_POST["content"];8 Q2 X9 M5 U4 R% x+ @
- $tags = explode("_",$_POST["tags"]);% v3 R1 F* o0 d, q& `8 k
- $cate = $_POST["cate"];
% @4 e$ k3 q4 R2 [ - //print_r $tags;: `" k% _+ F$ I6 ^) q8 j( t; D
- //
- L S( c" c1 G1 J( K8 B - //
& N* Y; H! b2 ^5 z7 E/ V - $wp = new WP();( w1 }* p# F+ d0 e* w+ x# t
- $wp->main();3 v* N! q8 J9 s1 q( h2 \
- //3 ` }! {0 C6 I- W4 t* H+ [
- $my_post = array();
) q8 @/ T$ _, L - $my_post['post_title'] = $title;
* u4 p$ p; S! }7 R; ~; q2 R% _ - $my_post['post_content'] = $content;2 I; b+ e) R3 l9 B: _) I/ Q/ E( h
- $my_post['post_status'] = 'publish';$ H4 x0 v8 Y2 `. j
- $my_post['post_author'] = 1;4 d" f: E3 \7 H0 m) w5 w
- $my_post['post_category'] = array($cate);
# ?" x" a7 h+ v+ N5 U - $my_post['tags_input'] = $tags;9 Z( j- y) }1 Y9 k4 l. V
- //$my_post['tags_input'] = array('tag1', 'tag2');
- a1 @/ o) r$ G- S - //$my_post['post_status'] = 'future';8 l7 a# }* ?+ O8 D$ m3 N3 s
- ////$my_post['post_date'] = '2010-07-04 16:20:03';
) K3 f9 i7 j8 Y6 x2 K: s6 R
2 R. ?3 W L- H+ w- // Insert the post into the database. B1 f* d0 }1 A
- $ret = wp_insert_post( $my_post );
, h/ [6 x v0 A0 H) M - echo $ret;
3 Z7 l+ n2 s& m0 l; R1 ~ - ?>
T C* w# {; u+ w" F$ E& [1 h" b7 O
复制代码 我自己写的
& A$ q3 b* X/ o插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|