|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑
6 C7 V/ J+ Y7 y/ G5 d
3 `& z; }+ I- X- a) L+ @直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持
D# E8 }$ \- m# [ i) S3 o1 m, Y
我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了
* ~0 R8 `0 U1 S, B) t: X6 J9 L* ^ g0 `. e* a+ b- O; n& n4 ~
方便实用 简单 何乐而不用呢
1 k% O1 X0 w1 g% w9 O5 F. R0 k- <?php% W/ ~. |( d- Y' Y7 E) p
- , l- t. i6 p# ?7 Q) B) o
- define( 'ABSPATH', dirname(__FILE__) . '/' );
' O6 w( f- `" a* v' u2 C6 N, ~ - require_once( ABSPATH . 'wp-config.php' );
' w: Z7 O8 [1 l - require_once( ABSPATH . 'wp-settings.php' );. a* d1 i4 e# u3 s A2 [
- require_once( ABSPATH . 'wp-includes/class-wp.php' );
' Y( \' |6 q' e# S# A. a4 } - require_once( ABSPATH . 'wp-includes/functions.php' );
9 b; A: V+ I: w x - require_once( ABSPATH . 'wp-includes/plugin.php' );) d/ [& i( i Z7 b3 S8 i( @: t
- $title = $_POST["title"];
, ?9 g! o6 ]+ G, |& f) W2 ~ - $content = $_POST["content"];5 d. o2 c' p- d1 C- Q' R" g& U; L
- $tags = explode("_",$_POST["tags"]);5 V# c: z( U' T- j, v
- $cate = $_POST["cate"];3 f: Z- L) u1 Y
- //print_r $tags;1 {7 `- J) m, Z4 ~$ e( N- u4 |
- //* g3 R/ c3 z* K# O, V
- //7 _4 T0 S. o {. p& v/ W$ X
- $wp = new WP();( e' s' ~ V+ J" P1 S
- $wp->main();& S: Y6 f# [+ z6 M3 D* R, M+ [
- //
, e+ S: g! ~' Y - $my_post = array();
% t8 |, I6 @( O2 h, L; U% y1 ^& K - $my_post['post_title'] = $title;# t8 B$ V9 \9 n7 M
- $my_post['post_content'] = $content;6 f5 o0 [( I4 |
- $my_post['post_status'] = 'publish';: P( T+ `/ [0 C4 [ l
- $my_post['post_author'] = 1;
) }* K# P% f7 u V; W" H( {7 ^- @ - $my_post['post_category'] = array($cate);
0 Q, X4 ^ I4 E - $my_post['tags_input'] = $tags;
) X; O8 n4 Y! D( W6 ^ H+ s9 a- d - //$my_post['tags_input'] = array('tag1', 'tag2');7 g# L4 x# G0 a% ]
- //$my_post['post_status'] = 'future';! V+ e2 i- W, \2 t1 X3 R" m* f' Y7 N) K
- ////$my_post['post_date'] = '2010-07-04 16:20:03';" x) I% k6 r% F2 C0 U
& v c5 N U O, m7 O; x- // Insert the post into the database
. ^: I4 d% J5 D2 W. V% j$ R# o - $ret = wp_insert_post( $my_post );0 I/ X- b7 y- c+ F; y, b H, d
- echo $ret;
/ Z3 x6 i1 a3 b, p - ?>- z% a7 J7 l3 P- ?; ?
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气- 9 D n* Y; x% A
- <?php
- b$ T9 d `8 V - define( 'ABSPATH', dirname(__FILE__) . '/' );
( @7 P2 r3 h8 |/ F - require_once( ABSPATH . 'wp-config.php' );
7 \2 Q. v& ?1 I- h - require_once( ABSPATH . 'wp-settings.php' );& u. Q# N5 j! C3 ~( U" d8 d8 |" F
- require_once( ABSPATH . 'wp-includes/class-wp.php' );
5 e5 W) z4 m- K - require_once( ABSPATH . 'wp-includes/functions.php' );
6 L% q2 x9 d, d( D - require_once( ABSPATH . 'wp-includes/plugin.php' );
, a' l$ j2 c; n6 J5 ^% i' \) O - $pic_name = $_POST["pic_name"];
! h i2 J, h V$ q M2 j - $pic = $_POST["pic"];" `; ?* s! e2 }) Z1 U+ o
- if($pic != "" && $pic_name != "")
# m% D% B0 G( W' v8 @1 R+ L - {1 D3 s5 s4 _& t: Y$ `0 Q/ z/ v
- $path = "images/";
4 m& n" J! x. P2 N4 S( Q - $pic_name = $path.$pic_name;
8 O' a1 q: c3 _8 s: {+ R - echo $pic_name;$ E$ k2 P+ n0 c2 U
- echo "||";
% Y. y, n: S, o6 c5 t - error_reporting(0);
# @4 s% ~9 Q7 r* m3 b( w# w - mkdir("images");
* e3 r; ~7 E6 c3 _- _- Z) [$ u - $handle = fopen($pic_name, 'w');
" n, q4 w: R; z2 J" E3 e5 ~5 M - echo fwrite($handle,file_get_contents($pic));
8 `' G3 m1 G, z - }
, Q4 x5 D. E1 \/ a3 X* h
3 l: Y; t' e- _5 Z5 F2 A- $title = $_POST["title"];
) ~3 z4 V, A/ K - $content = $_POST["content"];
, L7 Z" b) H. S5 J" b - $tags = explode("_",$_POST["tags"]);
3 s6 W8 w+ A" [* T( P, [) h/ R - $cate = $_POST["cate"];
4 A2 R. |: B- b: t6 P, K - //print_r $tags;2 g% B( _ ^# K) ~ y9 [
- //
O" L% F5 f) u: S" h- x% S6 T9 M - //
9 g% p, H! V9 r0 p4 p1 P3 r - $wp = new WP();
8 @( v+ T- c4 P7 d( }0 U! c4 d - $wp->main();
5 S( R) i7 ]% c7 A' @ - //2 g9 J: b3 g) j W) F
- $my_post = array();
7 g! \' M( ~- I- E - $my_post['post_title'] = $title;
$ a& Q- L* Q1 M/ {8 L: s - $my_post['post_content'] = $content;
* q, u& b* Q$ \6 {' R - $my_post['post_status'] = 'publish';
$ R* Z" K( {* @ a. w/ L: B( O - $my_post['post_author'] = 1;) Z: D9 e! E5 s H
- $my_post['post_category'] = array($cate);7 I2 `6 f+ J% |
- $my_post['tags_input'] = $tags;
! F) m4 Q, D$ Y' |7 q - //$my_post['tags_input'] = array('tag1', 'tag2');
! i7 _' a7 @% ]* A! V9 q) r - //$my_post['post_status'] = 'future';
! ?' d, B) l" R D$ h$ ^5 x H - ////$my_post['post_date'] = '2010-07-04 16:20:03';0 A- e1 d2 q" `2 n
- . d% G) g/ {8 Y* O9 l, W
- // Insert the post into the database1 e' V7 [8 E$ \) H. c" |; a4 [
- $ret = wp_insert_post( $my_post );; B4 n+ S. G) p/ a* W0 _
- echo $ret;: j2 b/ [) Y! x. @) _0 s
- ?>3 E0 Q h7 Z0 Z1 i) {1 y
复制代码 我自己写的' n" l7 R! {: N) N2 x
插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|