|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑 / w- `! I) O5 f" |
! D8 e" f1 f" I. Z' }: |
直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持. h! \/ @" [ X( ~/ \
% ?8 k& R; K6 p
我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了1 ]% a' C3 } _; O3 S$ A( w. f
# s4 N9 f A$ I( V& j2 V方便实用 简单 何乐而不用呢- 8 K1 d4 _7 ?6 G4 ?+ D3 M3 c
- <?php
o$ f- t7 h; h9 C
" O0 [) d1 I/ p( f n/ ~* h' N0 J- define( 'ABSPATH', dirname(__FILE__) . '/' );
x' Z9 ~4 o0 y) t) J - require_once( ABSPATH . 'wp-config.php' );
' b4 f: d: B" s( `$ N - require_once( ABSPATH . 'wp-settings.php' );
y3 t# r" N e3 z( A - require_once( ABSPATH . 'wp-includes/class-wp.php' );2 p# [( R( M4 `+ o7 H0 F+ z9 b
- require_once( ABSPATH . 'wp-includes/functions.php' );
# R* k( b- r- R2 H) Q. S3 B - require_once( ABSPATH . 'wp-includes/plugin.php' );0 R( V( ^; S# a, U1 ?) Y4 Y
- $title = $_POST["title"];8 f A) A! G& k1 g
- $content = $_POST["content"];
$ o/ ^3 l: L7 O, j7 a# y% U7 L - $tags = explode("_",$_POST["tags"]);3 h: l" r( ^2 S# h
- $cate = $_POST["cate"];
( I! z" B i, {! c& Y: s5 h0 z - //print_r $tags;8 D7 M( Q& U* c2 ~/ @+ n) ~
- //# ^3 U" z7 d7 m7 C# i- j0 [ w
- //
& U9 g; b$ y% A - $wp = new WP();! D$ i$ ~% A( D& L4 H9 E q- O
- $wp->main();
" ^' b, Z$ X6 L- E8 R - //
- q- u( v" v/ w5 j5 O: ] - $my_post = array();7 ? _$ K+ ~/ |' Q
- $my_post['post_title'] = $title;
, i& W* |: c2 ?! F - $my_post['post_content'] = $content;. x8 O) Y! \* o* S2 z1 [
- $my_post['post_status'] = 'publish';
+ y5 Z5 X2 ]% e- I - $my_post['post_author'] = 1;6 t5 l0 m& R- y$ f- `
- $my_post['post_category'] = array($cate);
4 x. z" x$ d# H5 a# O - $my_post['tags_input'] = $tags;
% ~/ x* l" ^! Z9 ~& l) D8 C5 ] - //$my_post['tags_input'] = array('tag1', 'tag2');3 g" k% ]1 Q' e
- //$my_post['post_status'] = 'future';
" r$ b( A9 k! {* x) C4 z - ////$my_post['post_date'] = '2010-07-04 16:20:03';
& Z" L8 l& v* D' E$ M - 8 P+ B% V. m( }6 }0 h4 Y, g
- // Insert the post into the database
: u3 G8 P+ o! a; m2 x - $ret = wp_insert_post( $my_post );
1 R, J8 _9 X3 G; p+ L0 N - echo $ret;# I" T! `$ q% y8 Y: ?% y9 h' z+ L
- ?>
+ e+ |; [: S) h$ j# X1 w% Y+ m
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气- , U3 ~: B8 ~& v" j! y5 M
- <?php
% }* P& g) C$ ~6 W - define( 'ABSPATH', dirname(__FILE__) . '/' );
& C! ?' V5 \* o7 e: S6 d* o: O - require_once( ABSPATH . 'wp-config.php' );5 B' L- ~; c# @ R, g
- require_once( ABSPATH . 'wp-settings.php' );
: R0 @" c, |7 a% P - require_once( ABSPATH . 'wp-includes/class-wp.php' );
; ?; }! g: q ~9 ^, c. A - require_once( ABSPATH . 'wp-includes/functions.php' );
# A# k' S, Y* D% {# k - require_once( ABSPATH . 'wp-includes/plugin.php' );+ l: X# l2 h4 O3 Z2 U7 {; l7 l
- $pic_name = $_POST["pic_name"];# u) T( Q1 u" N: R; {6 J W: S
- $pic = $_POST["pic"];
* d3 h6 v. a" A8 T" N7 l4 V - if($pic != "" && $pic_name != "")
1 z- T& U) r3 _2 e# M2 v - {& Y( m4 I8 _: g3 R
- $path = "images/";2 T3 k: I. t9 x
- $pic_name = $path.$pic_name;
3 y9 p0 M, R! D' B4 f5 F- ^4 E - echo $pic_name;
; `/ H, N5 z( S$ Z$ f+ Z$ O - echo "||";# H( I' K5 M) E2 c
- error_reporting(0);
" Z5 j1 ]) v6 m4 r - mkdir("images");
. C4 u2 Y+ t, }6 b - $handle = fopen($pic_name, 'w');1 X& L9 w; X" b2 _$ }0 {
- echo fwrite($handle,file_get_contents($pic));3 I/ x8 X3 z. p" [8 N; t
- }/ o5 g' B0 l6 a4 P P
- ; i; m5 H) Y: }9 g* `3 j X
- $title = $_POST["title"];+ T* r( G7 ~# l$ z4 V* b5 b7 A
- $content = $_POST["content"];. R; I4 i/ i6 L; X$ U' a' g
- $tags = explode("_",$_POST["tags"]);
" I# U4 f" Q3 d" } - $cate = $_POST["cate"];
l r4 U& [. `( z: z - //print_r $tags;' v v l y4 Z1 T
- //# B$ Q/ q' o1 R
- //$ s& X0 J' ^5 \0 u+ h" r# [
- $wp = new WP();8 t& ]- U& k$ C3 i
- $wp->main();9 D2 _. [6 }4 R
- //6 F- K, {4 I, ]7 P7 K% @: W5 _; i
- $my_post = array();/ [ k8 K+ C7 k7 e& W$ g9 t
- $my_post['post_title'] = $title;6 ]) v4 ~" d: D* H
- $my_post['post_content'] = $content;- Y- C! j. F" R. A; d
- $my_post['post_status'] = 'publish';) }) T5 F3 r+ T% d% V
- $my_post['post_author'] = 1;
6 [1 O3 j& N/ f& W" u& x5 l - $my_post['post_category'] = array($cate);
7 |5 D3 O4 ~# I - $my_post['tags_input'] = $tags;
5 O$ {% |& g/ T! N8 F - //$my_post['tags_input'] = array('tag1', 'tag2');
7 B4 s' f0 I% P! G; l - //$my_post['post_status'] = 'future';
# ~4 }* o& r% ^ H! p' H" h - ////$my_post['post_date'] = '2010-07-04 16:20:03';
( h: _* f' x6 K \* ^5 x+ E* x0 z - 5 r- F3 {) v3 R$ i3 I8 F
- // Insert the post into the database4 c5 Y: k( ^& H( [/ E
- $ret = wp_insert_post( $my_post );# q9 N( B; t' d0 \5 |- H; {
- echo $ret;6 Y) {4 O+ ?$ y% a/ n1 C9 m
- ?>
) X; K! S. y4 j* O* C
复制代码 我自己写的
5 W E. Y, k, E g( _. \插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|