|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑
5 d* ~4 U0 Y0 a; y9 x
5 n2 G2 t4 D/ P: x I6 ]2 e直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持
8 r# x. j6 p& t r0 H
* h# F! @! i6 x1 E我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了
+ n; Z8 |# G" D7 D: I( R, v$ Q! @* x5 ^8 O& b! T$ s0 v [9 C
方便实用 简单 何乐而不用呢
! N& i$ q9 G7 ?- O+ ~5 K- <?php
- {2 ?$ C5 M' R1 n - 9 s4 N; \& ~6 i8 V7 `
- define( 'ABSPATH', dirname(__FILE__) . '/' );
! K- s( v3 h3 O, u, n( O, z - require_once( ABSPATH . 'wp-config.php' );
# Z$ i) v9 A- L0 S, Y7 o - require_once( ABSPATH . 'wp-settings.php' );* y, s" S, x% q0 L+ I
- require_once( ABSPATH . 'wp-includes/class-wp.php' );9 E/ F, l" K' w6 E
- require_once( ABSPATH . 'wp-includes/functions.php' );
8 g6 E j$ [' s3 }8 a5 l$ q - require_once( ABSPATH . 'wp-includes/plugin.php' );8 Z- t! q5 {& N- {6 W( q! J4 @
- $title = $_POST["title"];
$ C! t6 \& F8 K* ]3 @ A# n - $content = $_POST["content"];
/ I# ^/ _+ {" Y3 w$ H: r9 w; `0 n - $tags = explode("_",$_POST["tags"]);* i4 ?+ F4 \7 l: h; ~* ?
- $cate = $_POST["cate"];
( i* v) ?5 b }2 ` - //print_r $tags;) F0 g2 b" Y( ?
- //
# N `$ m6 O" N5 j8 O: |+ c5 A: Q - //- a: p* b7 O. }, J" K) D! ~
- $wp = new WP();0 _) l! t6 _+ ^* I( y3 F1 K1 c w4 r1 F
- $wp->main();
( i+ }2 ^; b- O8 `. r - //" t/ ^5 w1 d+ @1 W8 n
- $my_post = array();
# {( U4 x0 {/ M3 q - $my_post['post_title'] = $title;8 H# ^- J+ a7 w# f5 T' ~7 l' \ t
- $my_post['post_content'] = $content;$ R' H/ U0 {( l# Y4 n% {
- $my_post['post_status'] = 'publish';
( i4 k6 z* W& k( X - $my_post['post_author'] = 1;0 M2 P( u0 o0 P' f3 Y1 z5 u
- $my_post['post_category'] = array($cate);
8 J. S& \, y5 [ - $my_post['tags_input'] = $tags;6 e% f# b5 n! s3 t# x) J4 q
- //$my_post['tags_input'] = array('tag1', 'tag2');
c, x* h* b1 Q+ I0 v; v - //$my_post['post_status'] = 'future';% g# Y& b+ G5 V
- ////$my_post['post_date'] = '2010-07-04 16:20:03';$ g1 @3 D" c* W! c
r7 |( ~! \) g0 I7 A6 i: l: }2 a- // Insert the post into the database N; J" `8 d, C; \' l( y
- $ret = wp_insert_post( $my_post );+ l/ o5 i) B1 `* ^, a' c
- echo $ret;
1 W6 u o. ~+ ]2 l2 |# l - ?>
, z5 t. ~* s Y- v! g
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气
& |: s2 n. U: F8 s: _0 D4 i- <?php
& K0 H8 @/ M, _9 R% S% W - define( 'ABSPATH', dirname(__FILE__) . '/' );
2 w8 G8 b5 c8 \ h' q, u - require_once( ABSPATH . 'wp-config.php' );0 B! m9 m1 d+ w
- require_once( ABSPATH . 'wp-settings.php' );
4 R* c1 g, o9 o) } - require_once( ABSPATH . 'wp-includes/class-wp.php' );, T) ]2 O7 K: ?( S; e
- require_once( ABSPATH . 'wp-includes/functions.php' );
9 `% @) h( H) l, A! C- i F - require_once( ABSPATH . 'wp-includes/plugin.php' );
& ?' P' T0 r2 h! i1 ?. s% K3 X - $pic_name = $_POST["pic_name"];) O7 T+ X) ?/ c
- $pic = $_POST["pic"];4 f& t9 s( d2 P6 \7 t
- if($pic != "" && $pic_name != "")0 g! @- g" y8 d A
- {
7 B4 N- j, V0 n - $path = "images/";# a: e3 _+ m3 c: [9 W! q: m
- $pic_name = $path.$pic_name;# j1 _8 h) K" {2 o& K3 [$ k" A
- echo $pic_name;( H5 D7 L: B, G, w
- echo "||";$ C' {5 I1 V! t- K7 k2 x) Y
- error_reporting(0);
1 j8 j) Y; f% S6 G" w. u+ Q - mkdir("images");& V9 e: d, J) C, J9 a
- $handle = fopen($pic_name, 'w');
6 D, s8 a# A* `7 }% u - echo fwrite($handle,file_get_contents($pic));
% T) K! n( l+ m% P r# z - }
* M! l) r* E# ~ - ! G+ M' p2 |- k% p5 M
- $title = $_POST["title"];5 L4 d% m# {* G- y
- $content = $_POST["content"];+ n& S2 j3 E# O/ H& O7 U9 C. s, Y1 r
- $tags = explode("_",$_POST["tags"]);
5 r/ |% z1 g; z - $cate = $_POST["cate"];% E, O4 c6 `2 A; V2 I7 \+ H6 {
- //print_r $tags;
# r9 r+ G* ? |! x) F - //
3 r8 {1 ^( ?" x8 b' Z" z8 \% d* k - //
% J; V3 f& H$ q. J+ v! I - $wp = new WP();
! ]0 B" f8 C1 Q3 p5 ^; s* k5 o - $wp->main();
1 l* B# E4 Q, O" q% z3 [, d( ?1 } - //
8 D) Y4 |; b. t" `+ g# V - $my_post = array();
7 V0 y9 |0 Z( `' [ Q1 G - $my_post['post_title'] = $title;
& e* y3 E. a3 ]( z6 C5 C - $my_post['post_content'] = $content;
2 n; A6 p8 L2 t: t - $my_post['post_status'] = 'publish';
" S% i* q9 q1 i6 ^. ] - $my_post['post_author'] = 1;: x0 O' {8 l# h6 p
- $my_post['post_category'] = array($cate);7 H6 _' |; [1 x- s
- $my_post['tags_input'] = $tags;5 \( A! s, Q4 x- i5 y
- //$my_post['tags_input'] = array('tag1', 'tag2');
- C" r0 _+ L+ y2 p - //$my_post['post_status'] = 'future';
8 l0 b+ K& g' i" H8 k - ////$my_post['post_date'] = '2010-07-04 16:20:03';- ?) s2 [1 c* m1 S
4 t. R* L8 G( m+ s7 j& v: Y- // Insert the post into the database7 t' F, A, o3 a ]2 g5 }4 z0 N( a
- $ret = wp_insert_post( $my_post );
9 e( `% j8 j7 g* J9 _ - echo $ret;
/ }* K: ?, q+ o/ ? V - ?>
: J; a9 A1 {% b3 g& Q7 S: u' a
复制代码 我自己写的5 l+ M4 V# d' t. F
插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|