|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑
2 J2 p0 G% x9 l/ H
/ j( r3 {3 `$ N2 w, w: n. x6 p直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持& H4 c: H1 x: e
! a* \; L ^' k! J9 j1 _
我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了
( U1 y) E. P+ e% Q
- c" G: B& H& S' j! f方便实用 简单 何乐而不用呢
" |7 K- D$ l0 j* i, a2 G w" @; I- <?php+ d) e! h t F# O- S
, H! Z! `' q$ s% D2 X) I1 v* b- define( 'ABSPATH', dirname(__FILE__) . '/' );
3 m7 r) p2 j9 I/ }5 ~0 p: t - require_once( ABSPATH . 'wp-config.php' );
, f0 W: D# _& {, I - require_once( ABSPATH . 'wp-settings.php' );
- ]3 }7 B6 r, [+ R0 p - require_once( ABSPATH . 'wp-includes/class-wp.php' );
) x; K$ V( \; A8 G - require_once( ABSPATH . 'wp-includes/functions.php' );9 n/ @( N; Z6 q1 d& i7 `
- require_once( ABSPATH . 'wp-includes/plugin.php' );2 N' J- ?% j/ K3 K
- $title = $_POST["title"];
9 w+ J0 G6 y5 w - $content = $_POST["content"];
* C8 |: M# N) ~$ m) f. a& c: G, | - $tags = explode("_",$_POST["tags"]);
% T0 P2 c3 g3 ]5 a: P u - $cate = $_POST["cate"];
* J3 d6 h) V6 i/ B+ e - //print_r $tags; O: ^$ j0 P# A- C1 U# C1 d( g
- //% k% Y7 M! z4 Y: q
- //
( U" ]1 j; y2 |) h5 S - $wp = new WP();7 N) b/ p2 C4 ^4 E. V0 D
- $wp->main();; N# `/ |8 L V( O8 F* P
- //
, O/ o* J3 d4 E' c - $my_post = array();
& {/ \( ^# ~+ V1 s8 _/ y- ? - $my_post['post_title'] = $title;
* _0 z( t" h0 S# q2 Y - $my_post['post_content'] = $content; o' k; l [4 T5 K$ N; ~
- $my_post['post_status'] = 'publish';
9 K4 O5 V7 t- h& C. ?3 x - $my_post['post_author'] = 1;8 w4 [3 y2 i; ~% y2 ?* h0 \. z
- $my_post['post_category'] = array($cate);. [ _0 e1 U/ m% V( G# _7 Y9 H
- $my_post['tags_input'] = $tags;
$ `+ r9 M S$ {6 E - //$my_post['tags_input'] = array('tag1', 'tag2');
% A* a. ~0 N! z7 {% S2 d2 @7 t7 ] - //$my_post['post_status'] = 'future';
# m" m& N& W2 m: b - ////$my_post['post_date'] = '2010-07-04 16:20:03';
3 O0 T4 {1 |9 B/ p W) L& G
5 ~4 E' X* q8 s) e$ Z+ v- // Insert the post into the database
, W1 G! Z, j/ M3 o) P - $ret = wp_insert_post( $my_post );
& O; X+ o+ U- |6 q4 M - echo $ret;0 W3 {% Y# q$ K: B, r; }
- ?>9 T; I+ F- `/ W% t1 a# _6 r4 I! F
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气- ; x4 d X) Y7 _3 Z+ T
- <?php
* Z6 n+ y, X1 B1 q7 _8 t' F- k3 O - define( 'ABSPATH', dirname(__FILE__) . '/' );
, V0 \& M% o1 e ~0 W - require_once( ABSPATH . 'wp-config.php' );
/ `5 r/ n) p. h% M! l3 A- {( [ - require_once( ABSPATH . 'wp-settings.php' );# z+ r2 s. m3 H+ F
- require_once( ABSPATH . 'wp-includes/class-wp.php' );
& y7 |: b7 m* w1 H0 H7 \2 H6 x - require_once( ABSPATH . 'wp-includes/functions.php' );) E5 _8 R0 |: [1 O2 b$ b9 }3 Y. F
- require_once( ABSPATH . 'wp-includes/plugin.php' );
% ]: ~* d1 W7 M$ e5 Q - $pic_name = $_POST["pic_name"];8 Z* s+ n3 ?$ }
- $pic = $_POST["pic"];0 ]5 j. F/ l, |5 t
- if($pic != "" && $pic_name != "")- o1 |* K' ~6 y6 {
- {: T1 I" G N, h" f+ T' S
- $path = "images/";
% F5 |- \- l9 o$ m% E N8 H1 B - $pic_name = $path.$pic_name;
0 A4 ^7 O, t) T, {8 ]! q: Q - echo $pic_name;
/ c6 m6 `9 i# H9 W+ P# ^# ]4 j# B$ L - echo "||";) q& q/ ]( a1 T
- error_reporting(0);
; G0 J* k4 c q2 X - mkdir("images");
% I& Q9 Y" L# @% G- g. ~/ a - $handle = fopen($pic_name, 'w');2 m/ U- @* H7 p. T
- echo fwrite($handle,file_get_contents($pic));0 R9 I" o# ]0 ^( i
- }
% k4 H) A% ]- ?1 W
. s! v) g3 T4 g- ~$ {6 Q6 _- $title = $_POST["title"];3 E) ?$ o& y, }$ _4 D2 G
- $content = $_POST["content"];
( G& k! \0 @) M* u9 k: p6 O - $tags = explode("_",$_POST["tags"]);
2 i4 j) u. ]; h* H- h$ Q# D - $cate = $_POST["cate"];
4 n' c8 W. _" w1 h3 q- H7 x$ [ - //print_r $tags;
1 T0 e+ F1 y) B - //6 s" p/ R& |# ]; ~ t
- //
' c8 R$ b5 f2 o! D! m' A1 @ - $wp = new WP();
, ~1 ]' A% J' }# I% G6 e4 X - $wp->main();. i7 r. M8 u s8 g8 g
- //. B( W- v0 u5 {* j+ w/ P' `: |' J5 `/ f
- $my_post = array();) X) r- e( O& ~5 A+ j- F( t0 Y
- $my_post['post_title'] = $title;
& R# z( a# C! f' |) y - $my_post['post_content'] = $content;3 H I" g C" @. v* V
- $my_post['post_status'] = 'publish';9 P9 q! `! C2 o; `. u2 g! f8 o
- $my_post['post_author'] = 1;
; b( l0 R0 a$ D1 B1 f - $my_post['post_category'] = array($cate);+ @- r! R( |4 I3 o
- $my_post['tags_input'] = $tags;- }2 ?* {. V8 D. q: n# K0 ^8 |7 y
- //$my_post['tags_input'] = array('tag1', 'tag2');) [0 ^8 z( x: ?. C
- //$my_post['post_status'] = 'future';
0 I( h* g( _* B- B9 J" o) K - ////$my_post['post_date'] = '2010-07-04 16:20:03';. o3 ~ ]% Y6 u" i- w r% V6 u
- m- }, B4 x3 i3 L1 x- // Insert the post into the database/ m; e A8 H+ E) `
- $ret = wp_insert_post( $my_post );/ t8 u) P7 x+ R0 F4 k
- echo $ret;
( e8 M" j6 U% Q - ?>; ^% c- ~8 ?. k5 R. j/ o" u4 R2 g
复制代码 我自己写的
2 S0 Z8 {4 R7 m4 q' L# B% ?插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|