|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑 & P7 ?1 m( f* u
& U2 C2 ^3 H8 K8 |! w$ D1 e7 b直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持
8 J+ ^, L. {/ T ?' [
$ m, B+ ]4 |0 w% K+ ~我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了
: X4 l2 K$ W# s: d( o v
3 F( z" @/ V. @+ Y* p- b G0 Q方便实用 简单 何乐而不用呢
$ f9 p% o d( M+ ~% U- <?php+ ]2 p$ B, i% A% t! ]
- * B1 y& Y. b W/ I5 f
- define( 'ABSPATH', dirname(__FILE__) . '/' );7 U$ x4 x$ ^9 o) ]
- require_once( ABSPATH . 'wp-config.php' );% P% \, N8 U+ D4 H, u$ ]
- require_once( ABSPATH . 'wp-settings.php' );6 C* P! Y5 y. Q
- require_once( ABSPATH . 'wp-includes/class-wp.php' );
0 c# Q/ W& M O - require_once( ABSPATH . 'wp-includes/functions.php' );& M* q c: i8 _3 V
- require_once( ABSPATH . 'wp-includes/plugin.php' );* |) c8 \" L% q# R7 t1 u
- $title = $_POST["title"];
6 G% X; A) J/ [" b2 O* z7 S - $content = $_POST["content"];; k: S Y5 S; D9 ^, }7 B. |
- $tags = explode("_",$_POST["tags"]);
8 ?8 l/ y. \( g1 i+ y" i7 z' n - $cate = $_POST["cate"];3 X8 ~5 n2 _: q
- //print_r $tags;6 H4 o% N2 P$ L- J9 W
- //1 V4 ]1 t4 v I4 ]0 A
- //, N' k8 J; J! f/ {
- $wp = new WP();
/ f5 w5 C$ N/ j% d, U6 r - $wp->main();, G2 K' P8 B% o1 s' |) D5 C5 C
- //
) B6 B2 h g9 w: S$ T9 C - $my_post = array();
$ i8 M1 |7 t! d, u- j1 R+ b+ N - $my_post['post_title'] = $title;
+ I6 C) u7 E2 H+ S. A9 p - $my_post['post_content'] = $content;
, C+ t0 ~- O; @) }' F1 S - $my_post['post_status'] = 'publish';
! B7 r& H+ w: K) [" ^3 e - $my_post['post_author'] = 1;4 c) \2 }7 [% \
- $my_post['post_category'] = array($cate);
[8 V# j; y: } - $my_post['tags_input'] = $tags;1 s/ b l+ ~) o& [0 X3 a
- //$my_post['tags_input'] = array('tag1', 'tag2');1 m8 g& z* ?8 d1 S
- //$my_post['post_status'] = 'future';
8 m& }1 l, V% K! | - ////$my_post['post_date'] = '2010-07-04 16:20:03';
0 `. D! Y6 s! r) V O - 3 ~5 L; c3 K% {, T/ f/ J
- // Insert the post into the database# q' }% w, b. H/ R! a
- $ret = wp_insert_post( $my_post );1 S, j8 x: A! O G) G1 W D' G( D
- echo $ret;
/ H# |, ?; D# R' A3 U& d! w Q - ?>
/ I/ o3 S, l2 x+ }7 U
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气- 2 i7 y. D) p+ q# |7 }, J
- <?php8 S5 f8 i" Q7 p; s+ A5 c
- define( 'ABSPATH', dirname(__FILE__) . '/' );' Z' F3 e! ^" l3 ~( @" I5 e
- require_once( ABSPATH . 'wp-config.php' );
k8 H" r* K& t8 A/ L$ B - require_once( ABSPATH . 'wp-settings.php' );% E8 t/ M8 C0 a5 p+ ^
- require_once( ABSPATH . 'wp-includes/class-wp.php' );' m$ Z# X0 l1 d% }5 H
- require_once( ABSPATH . 'wp-includes/functions.php' );
' [9 M* G% U' ?! P# S* [: E! g$ k7 G# A - require_once( ABSPATH . 'wp-includes/plugin.php' );
; N3 t( ~$ m8 J0 P* o( p5 X- O - $pic_name = $_POST["pic_name"];
& ]* h3 e" Y" i/ u8 R: s% Q - $pic = $_POST["pic"];
& h: X: f, D" G( G# a S7 o - if($pic != "" && $pic_name != "")- G- e9 L+ K: C" w; ?
- {% @( k2 }% Q( M, ~6 H
- $path = "images/";
" T2 u5 C' r* \8 D" _ - $pic_name = $path.$pic_name;6 |( S; l# A$ b& E3 \2 b
- echo $pic_name;
" d0 C4 U V4 \, Y6 _9 e w - echo "||";; n% v: I$ \' U, B2 H
- error_reporting(0);
. }$ V8 m0 W& Z" R5 F& x, ~ - mkdir("images");9 v; |- e: r9 g! S' b4 c
- $handle = fopen($pic_name, 'w');, D$ B0 V1 d5 y! s7 A& m. D
- echo fwrite($handle,file_get_contents($pic));
2 Y0 I( r6 a: e0 d# h( b - }4 A8 F) |' X) ~7 |9 b" X/ f
- ) w# n- g$ ~" t
- $title = $_POST["title"];
2 a7 p7 x0 `! c/ a+ s- V - $content = $_POST["content"];& j, [% C D# a6 U# e( c4 a
- $tags = explode("_",$_POST["tags"]);- _) w. ~- {! h1 y4 X- K0 [# T% d
- $cate = $_POST["cate"];
' o" K2 k0 }2 L8 t# m! W8 N' x$ R7 S - //print_r $tags;
6 D7 V- t- k$ x1 p- d0 [/ [ - //
% u7 `+ |0 |0 p0 {) i - //
6 T* _- C; B5 D/ ~3 K( f& z - $wp = new WP();
/ @! S& a6 D* g) }! X4 z' Y( o - $wp->main();& I% u0 Y% f1 C {# F5 k
- //
' d, [6 X' b r% h" E* Q- W$ { - $my_post = array();1 c5 O* |4 _# r# [
- $my_post['post_title'] = $title;
8 @) k# j; ~! x8 e/ V" ^2 B - $my_post['post_content'] = $content;
5 P; M8 T2 o: _5 G - $my_post['post_status'] = 'publish';
+ ?" }' e; l9 C" y# P/ ]. [( O - $my_post['post_author'] = 1;. \: r6 R, E2 c& ~, I8 c7 B
- $my_post['post_category'] = array($cate);
* @" W# W T, S+ }: y- n - $my_post['tags_input'] = $tags;
6 u# A* f8 Z. E* M, [% V( Z - //$my_post['tags_input'] = array('tag1', 'tag2');4 I7 p' i. s# @7 K
- //$my_post['post_status'] = 'future';
" V4 n8 g3 n- i2 d) J - ////$my_post['post_date'] = '2010-07-04 16:20:03';3 m0 ~7 T2 s- D! ]" E+ H$ d; t* l
- 0 T" U. n. {1 o! M/ Q# {
- // Insert the post into the database( }( |; i8 R. G* r0 r
- $ret = wp_insert_post( $my_post );
9 r6 ` F Q; c3 ~+ \/ u* l& M - echo $ret;
5 E+ [ b0 R" }0 R3 O+ y9 y3 ?6 T - ?>7 h V6 N: v9 {' Q
复制代码 我自己写的
' R2 k. ~& i0 S6 V4 H9 R, ?插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|