|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑 6 }9 W' {2 q3 ?
' @% {' v+ Z; H$ n; M" {) O
直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持
' h' k: _ c# O5 o4 G5 G6 }( F1 t2 i+ L) g7 m6 v2 N6 q2 I: O
我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了/ v2 d8 c/ d' J! k
& K/ I W, g8 ~ D8 ]
方便实用 简单 何乐而不用呢
5 }% I9 e$ p- y; i% `- <?php
% ~ S9 e( L& r- m' `& m/ l4 k r
, T; M; R7 o; D; F$ g- define( 'ABSPATH', dirname(__FILE__) . '/' );4 g) v) w3 G( @8 W# w* B$ k% X8 A
- require_once( ABSPATH . 'wp-config.php' );
+ [! c/ P( \) a4 n6 ^7 g) _/ ~. `9 u - require_once( ABSPATH . 'wp-settings.php' ); I' A2 j" j0 x
- require_once( ABSPATH . 'wp-includes/class-wp.php' );
+ |( w0 X7 K3 m% @/ f% R - require_once( ABSPATH . 'wp-includes/functions.php' );' r; Z; W2 U2 r% n' B7 w
- require_once( ABSPATH . 'wp-includes/plugin.php' );
& s0 [3 I2 w. e - $title = $_POST["title"];. V: R' g J7 o5 |1 p) E
- $content = $_POST["content"];' q1 v, C" W9 j3 q4 e
- $tags = explode("_",$_POST["tags"]);! u: {5 l8 G+ u$ d2 w- s
- $cate = $_POST["cate"];
* y1 f2 j5 ?+ i" Y( k+ P$ t. ^ - //print_r $tags;1 E! x, @( m" Q+ o& ]0 [
- //
+ P, }' N+ A3 g& _/ \3 p* W - //
! Q7 t1 z* x, m+ i+ H - $wp = new WP();
2 O# N1 f4 N, ~% |% w - $wp->main();
( d& h+ E t$ A+ M, q S - //
* g% |5 Q9 r s4 e/ Y - $my_post = array();
" y0 ]' r( G; }6 U8 M. x' z! [/ ` - $my_post['post_title'] = $title;9 ^ A9 V; |- g: `8 t# `( w
- $my_post['post_content'] = $content;; X( l" b4 m1 _ n- N W
- $my_post['post_status'] = 'publish';
, K+ `- h6 v; B( D - $my_post['post_author'] = 1;
2 r6 g% x4 u7 R( H - $my_post['post_category'] = array($cate);' W3 N+ l( W6 F
- $my_post['tags_input'] = $tags;$ A" T0 u: q& S, b& O) I8 M
- //$my_post['tags_input'] = array('tag1', 'tag2');
2 |0 l- c6 n8 T0 N5 M. b - //$my_post['post_status'] = 'future';) F5 [7 a3 i1 O- d- m
- ////$my_post['post_date'] = '2010-07-04 16:20:03';
6 h' L4 c5 Y$ K4 y' [
8 ~% O3 g; x- ]9 @- |" G- // Insert the post into the database+ p5 G/ i& t. n: Q) M+ `
- $ret = wp_insert_post( $my_post );
q6 _# n J: @% _ - echo $ret;* n- q. N% W2 i1 i" y
- ?>
; e ~# C: I v! I
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气
5 a* q" S# L+ {) M' {+ R- I" }- <?php' b1 [* ?/ a1 d! A
- define( 'ABSPATH', dirname(__FILE__) . '/' );0 [* y1 ~2 y/ u' Z
- require_once( ABSPATH . 'wp-config.php' );; I& G6 L* G' `' f! `0 A. i
- require_once( ABSPATH . 'wp-settings.php' );
* G; ^/ d! ]! g+ K, B( V - require_once( ABSPATH . 'wp-includes/class-wp.php' );/ h9 N2 |! d, ]; E, m
- require_once( ABSPATH . 'wp-includes/functions.php' );
1 g; c8 V' m0 r# h6 E |5 t - require_once( ABSPATH . 'wp-includes/plugin.php' );% W+ X8 V4 k$ I8 d( D$ E, F' K
- $pic_name = $_POST["pic_name"];
4 D1 k- j: b: J# V5 }# D1 L - $pic = $_POST["pic"];- S/ \+ @( D3 G$ a' u+ y! q: n9 s
- if($pic != "" && $pic_name != ""). \* B& J `& }' I% Z/ n: c* b
- {
8 [$ p+ _5 I- ~% f; r6 J3 H& D6 J - $path = "images/";
$ Q7 X8 a/ n3 E( c9 \. ]* C T - $pic_name = $path.$pic_name;
3 D2 c5 M# F+ z" L# j! H) A' @# S - echo $pic_name;# R) S: e, B. }- l3 \) H2 I% y
- echo "||";5 S7 W" U* r& Y) m
- error_reporting(0);
# O0 `+ J2 V' h. Q$ {8 I$ Q - mkdir("images");
* ?0 b# U6 K' |5 ~ - $handle = fopen($pic_name, 'w');
( N! K5 Q- {, ]/ {# \0 V7 l5 @ - echo fwrite($handle,file_get_contents($pic));
. i( C0 T3 w: Q' b6 x' t - }
8 U6 D' w6 T. [
$ m4 y8 G% \' z% a9 ]! ~9 Z- G- $title = $_POST["title"];
" }9 [$ t; ~/ [7 u/ g - $content = $_POST["content"];) w1 R: s6 C' Z: o9 h* `) @
- $tags = explode("_",$_POST["tags"]);+ U. Y5 Q2 q' [0 r; n6 b: X1 \
- $cate = $_POST["cate"];6 ?. z# P. M( ]+ T
- //print_r $tags;7 Z- R% P/ M! P; G- S% q
- //
" I# N$ j* C# C* U8 t q - //
& B! p5 [ b, w& J" m8 v0 R - $wp = new WP();" E: L9 q8 J' r; m
- $wp->main();
/ j) Z( h4 B! P1 m9 ?# n - //
' |) z+ {- y, x( `+ y - $my_post = array();
1 Z, a. q. @* J+ T9 m4 @ - $my_post['post_title'] = $title;) S& e0 r( t L% f# h) I1 P* g
- $my_post['post_content'] = $content;
2 H l/ H1 M0 f, j, z0 @- A - $my_post['post_status'] = 'publish'; h3 j6 [9 V( X) z* \6 [7 c
- $my_post['post_author'] = 1;
9 E7 t \( N1 a# b9 T) c - $my_post['post_category'] = array($cate);
5 \- f. Z! }+ _& ^4 _ - $my_post['tags_input'] = $tags;
. h2 e; w3 I( S - //$my_post['tags_input'] = array('tag1', 'tag2');
u6 m4 l0 a7 U: Q - //$my_post['post_status'] = 'future';4 y5 ]! Z, S% ], o8 q4 a! D
- ////$my_post['post_date'] = '2010-07-04 16:20:03';! i# m8 l9 y. B
, m7 J) }0 k0 K9 Z) U0 S; ^6 f, t! d' E- // Insert the post into the database
* P0 W R+ @2 j6 d - $ret = wp_insert_post( $my_post );) o, Q. e- j: z7 x8 f# ^* L4 n
- echo $ret;
( C9 |7 @" c5 m6 [ - ?> H* y6 L- w, J4 ~" Y2 L, N
复制代码 我自己写的1 u* W- ^ A8 ~4 {( f
插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|