|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑
* r6 _. D0 ]$ X" G" y" y6 e2 l5 Z/ d5 Q4 d) T
直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持
5 d; O x2 ]% P# `# T4 E3 O
# N- M ]3 f; f( ] M+ S我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了
, }( U$ o2 e6 M' g- x% s9 a
h, Q1 k6 c( [9 E方便实用 简单 何乐而不用呢- 6 i1 U6 O6 X2 d7 a! S
- <?php
& [$ Z+ T, P; \, o, _) V% J6 n - 7 p! Z5 l9 B. h% L
- define( 'ABSPATH', dirname(__FILE__) . '/' );
8 X( M6 d' |( A2 C1 @: t - require_once( ABSPATH . 'wp-config.php' );6 v% D4 m4 ?- m( w. v9 [* Z! V
- require_once( ABSPATH . 'wp-settings.php' );
% e, W y( A# O( X) k1 f - require_once( ABSPATH . 'wp-includes/class-wp.php' );
5 _& {; O' A! Z; S - require_once( ABSPATH . 'wp-includes/functions.php' );
( P7 N3 r- X* z7 ? - require_once( ABSPATH . 'wp-includes/plugin.php' ); x& R: x } ]" k: w
- $title = $_POST["title"];
- E4 i/ m x& J) g- E - $content = $_POST["content"];/ A% @0 k. @- S/ \7 W2 |' j8 G/ V
- $tags = explode("_",$_POST["tags"]);0 e! t4 x; F) Q: C5 a6 C1 e
- $cate = $_POST["cate"];1 d- y' J5 v4 L. p
- //print_r $tags;
- d' C9 b' S1 r- W8 ?$ e6 } - /// T' L- A* B" ]8 J, `& W
- //
7 ?+ f% ~! W' p* T - $wp = new WP();* ?; ?! o& Y- ^% G* F# L8 T& v4 X
- $wp->main();
) y$ m, P* A& V" C7 H - //
( V( M0 G/ z- Y* R+ e8 p; s* v - $my_post = array();
. R8 h# D5 D, B( e7 I" I - $my_post['post_title'] = $title;; |4 n4 Z1 U" b0 c
- $my_post['post_content'] = $content;( R: J6 J- g$ L7 y3 G; m. r7 Y
- $my_post['post_status'] = 'publish';1 f# f" c" k' N' A: U! o
- $my_post['post_author'] = 1;6 j0 n# j! ~8 P+ y9 z. B
- $my_post['post_category'] = array($cate);; r F% \' N0 \% g0 k' F) t& @+ x4 U; y
- $my_post['tags_input'] = $tags;
0 u7 V3 {; \$ s' b8 a- {" s% u - //$my_post['tags_input'] = array('tag1', 'tag2');# [7 J5 }% K% }; H3 S7 e
- //$my_post['post_status'] = 'future';/ w0 b3 e; Z( m, ]* D
- ////$my_post['post_date'] = '2010-07-04 16:20:03';5 I0 K7 _; ^. F& I! X V
- ; @# A8 T1 w3 M$ x1 R7 _, E Y
- // Insert the post into the database6 m5 p8 m3 C$ y- |& @+ |
- $ret = wp_insert_post( $my_post );
. U1 l5 I. P7 `1 B2 ^ - echo $ret;/ N0 W& [9 r0 X# Z: h! p
- ?>6 {- N7 n( \" {6 Q2 s0 A. f8 @) H
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气- p8 w% A# ^ l
- <?php" [% y% v" ?' u* {" y2 m
- define( 'ABSPATH', dirname(__FILE__) . '/' );8 ? s' d/ _4 N
- require_once( ABSPATH . 'wp-config.php' );
5 S# y( h& \* P2 @ - require_once( ABSPATH . 'wp-settings.php' );3 _0 D4 Y j6 e t
- require_once( ABSPATH . 'wp-includes/class-wp.php' );
3 l- ]0 M9 j. _2 m& U - require_once( ABSPATH . 'wp-includes/functions.php' );8 z5 S% y& P* { Y
- require_once( ABSPATH . 'wp-includes/plugin.php' );8 q- Y- X8 |8 u5 F7 d6 y* X
- $pic_name = $_POST["pic_name"];
) W1 Z4 Z/ f) @! Q5 R - $pic = $_POST["pic"];
% o6 A9 t7 m' _% Z - if($pic != "" && $pic_name != "")7 Q! b- l/ M0 s3 I9 i4 ~; b
- {
: ?' Y( P% W4 H0 u9 v+ A - $path = "images/";% J, p+ O! t+ Y$ ?( p2 s) N. H
- $pic_name = $path.$pic_name;
7 T7 ^, N: g4 i( u0 Z; r - echo $pic_name;) c, Z' @! V' h% R( L7 w
- echo "||";
( U s' \! f0 x) p2 q& { - error_reporting(0);
C* u, N# c3 O' F% p: j8 x1 p - mkdir("images");
4 G& f5 ` ~& J - $handle = fopen($pic_name, 'w');8 b; w1 B. x# U) d
- echo fwrite($handle,file_get_contents($pic));. t: d# h7 c& d1 c
- }
; M4 o9 j- @& I8 e4 ?
* {+ v& c8 E! I$ u- $title = $_POST["title"];. _% f) E; x/ I. X! u0 C9 }
- $content = $_POST["content"];
+ p7 O6 \# {9 }- ]7 ^( \ - $tags = explode("_",$_POST["tags"]);
3 }- Q9 ~& s, t - $cate = $_POST["cate"];
6 E4 M. ^0 l/ y0 `/ |# { - //print_r $tags;$ P1 u) O7 o& q; ^2 t2 @- V$ z) u
- //, I# F9 v8 w# y/ v6 R
- //
, p6 h$ @& F4 E, H% _1 m - $wp = new WP();
% M& g/ |6 Q# S) ?8 c - $wp->main();9 C7 o5 F* i1 n+ o
- //
. i$ `$ O" S N1 S' Y, o - $my_post = array();" Z8 ?5 _7 d+ v9 e' M! O: F
- $my_post['post_title'] = $title;9 L7 F: S# R' z4 q
- $my_post['post_content'] = $content;
: ~5 ~: F$ {7 d, C& P7 u - $my_post['post_status'] = 'publish';
# \' p3 l+ y2 K - $my_post['post_author'] = 1;
2 ^" E( [' w$ f0 A7 l# h: P - $my_post['post_category'] = array($cate);( ~5 p7 M) D. k0 ~# r6 d8 g5 J+ ~
- $my_post['tags_input'] = $tags;5 G( i e- Y1 l
- //$my_post['tags_input'] = array('tag1', 'tag2');$ C: g2 c# [4 M& L: i7 ?5 J6 n
- //$my_post['post_status'] = 'future';
' m# y2 D/ O( J( q5 j, V - ////$my_post['post_date'] = '2010-07-04 16:20:03';. G, a R$ F7 b9 L; V5 M% y
. \% W; J3 W+ z0 I m$ A9 s4 [- // Insert the post into the database- ^3 O( u& M4 Z/ @4 ?
- $ret = wp_insert_post( $my_post );
# n- c% o$ c" @3 Z5 J X - echo $ret;& J1 ^5 v- m* }0 e9 [
- ?>
( n. P3 {! ]2 L( f% B
复制代码 我自己写的. T$ x; q E2 h
插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|