|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑
" `; g1 W3 l# B/ @- ] t
! o5 a8 P2 e: N+ |7 e直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持
h- ~) O+ }/ t6 {) [1 {# f( W6 a! { x; K% `! ] ?' ]" O
我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了% a- T4 F K" _) `
# s* ]( q0 [ x) |# [! e+ x方便实用 简单 何乐而不用呢- % j {% U4 @5 I- q+ C0 W
- <?php
. G; M; T- X) @! I! `
) [6 L* D8 R e* l A2 c- define( 'ABSPATH', dirname(__FILE__) . '/' );- O4 e W& n: _. {4 y2 o! D/ ^
- require_once( ABSPATH . 'wp-config.php' );6 V; Y6 k4 U+ g- [6 ? X2 I
- require_once( ABSPATH . 'wp-settings.php' );5 V( V2 I5 Y; p6 f2 r
- require_once( ABSPATH . 'wp-includes/class-wp.php' );
& A5 s) Q. k1 ]- s! l - require_once( ABSPATH . 'wp-includes/functions.php' );
6 E: ~5 r. ?; j - require_once( ABSPATH . 'wp-includes/plugin.php' );
2 ?! w0 d/ R% r) z - $title = $_POST["title"];- J- q" H( I3 D H* n P
- $content = $_POST["content"];
4 l3 N5 D/ W$ w3 U: A6 G0 m7 H - $tags = explode("_",$_POST["tags"]);
: d2 @2 x8 L4 m4 C5 b - $cate = $_POST["cate"];
+ @, Q# ?' M7 n( |$ t" Z9 g) [ - //print_r $tags;4 ?0 J6 q7 Q2 M9 l+ F4 J
- //
9 J" Q% z3 `' E - //
% C/ N8 K% E. D. I' {7 {# P - $wp = new WP();
7 G0 m, u0 }: V& ~* |$ ?* t% y - $wp->main();" L t* v% K7 M" f
- //
3 c. G" Y' |. h/ M: b8 [. p4 o - $my_post = array();: s4 j+ k J% x( M+ I+ W: ^& m
- $my_post['post_title'] = $title;2 s/ k& k7 c0 m7 ?5 t$ Q
- $my_post['post_content'] = $content;) d! d# T) L% T; \# O
- $my_post['post_status'] = 'publish';
- l& \# d7 }: p4 ~* ~8 _ - $my_post['post_author'] = 1;
$ E3 k! ?/ z4 T - $my_post['post_category'] = array($cate);2 d# f6 w2 H6 U
- $my_post['tags_input'] = $tags;! J0 c- a% e9 J7 O# `
- //$my_post['tags_input'] = array('tag1', 'tag2');
! v# ~4 Y- i3 h, Z* M - //$my_post['post_status'] = 'future';
+ [& t6 j& E, _0 w/ U4 y - ////$my_post['post_date'] = '2010-07-04 16:20:03';1 h+ \. P. N4 `+ Q2 L/ m* P* |
- 7 S! s8 E6 r. b) O
- // Insert the post into the database( U; w- }, e9 c) V$ ]2 D
- $ret = wp_insert_post( $my_post );
E. L5 D0 k: _3 _6 @ - echo $ret;8 `* o; ^1 ]. x$ x- d' w+ \$ s
- ?>
6 z- x3 }' c. g# z
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气
' A3 ?0 C. \, Y" M8 X8 E% q- <?php: I" `. O' t# S s
- define( 'ABSPATH', dirname(__FILE__) . '/' );+ N7 B6 L1 B, W# u& i; b F3 @
- require_once( ABSPATH . 'wp-config.php' );
% E$ }1 ~( ]- Q0 U* R - require_once( ABSPATH . 'wp-settings.php' );
5 B% S2 f7 N& s4 U - require_once( ABSPATH . 'wp-includes/class-wp.php' );( Z3 U& l; o! u0 \
- require_once( ABSPATH . 'wp-includes/functions.php' );
: J% u3 z1 {. ]/ o& X2 Q& s8 Z - require_once( ABSPATH . 'wp-includes/plugin.php' );
3 X$ t1 f1 O$ T - $pic_name = $_POST["pic_name"];. h6 L( z9 M- p5 i' Q1 u" W; c
- $pic = $_POST["pic"];7 \. ^: u: l: B1 G4 q- P4 C
- if($pic != "" && $pic_name != "")% O2 W" y5 C( }- a/ o
- {
0 d1 q+ c$ F" _( @' `+ g - $path = "images/";
( Y% f; J2 | d+ I. s6 X - $pic_name = $path.$pic_name;
9 g* o- ]2 ~: E - echo $pic_name;
3 y( M( C0 x# S$ Q& `' E; k* _ - echo "||";3 z. K' s: H/ e1 } `
- error_reporting(0);; F9 l @ h, L
- mkdir("images");
" P# b7 L3 r. T3 a& W. w/ L6 C+ |0 N- A - $handle = fopen($pic_name, 'w');2 _$ P1 d" U4 W
- echo fwrite($handle,file_get_contents($pic));* l I% O) z9 i7 `, G9 I0 P2 n
- }
& p' A, c/ |8 g& _- ?
6 a$ K4 n" y) c" I6 i* u- $title = $_POST["title"];4 ]* R4 _3 m ?2 h+ i
- $content = $_POST["content"];! r: D# R' u# z; a) C8 |7 V3 p
- $tags = explode("_",$_POST["tags"]);
2 C9 e9 ^. A5 b$ G. X - $cate = $_POST["cate"];
A6 _7 a) V! u, c' Q, G; ? - //print_r $tags;3 P5 [3 Q2 b+ t3 c9 k! a
- //
' C1 B; O4 ^0 _- M/ w; e - //! o# W! m1 J0 @- X' y
- $wp = new WP();
. z7 H; n% @8 l" J& i( D: w6 M - $wp->main();$ U: Z& d2 H/ e: A/ r
- //: \5 l k2 i9 z7 A
- $my_post = array();# n! c) v7 {3 J& @* S# Y- P
- $my_post['post_title'] = $title;; t0 r! Z! f# I6 `9 ]5 X
- $my_post['post_content'] = $content;
1 ` l& \4 A4 G0 j% T - $my_post['post_status'] = 'publish';4 x( R/ Z( z. ]
- $my_post['post_author'] = 1;
8 I; o" a* A/ U& N - $my_post['post_category'] = array($cate);' A5 I g! Y! X% Y' a6 m, W) ]
- $my_post['tags_input'] = $tags;
, s$ b3 _. I4 B3 u5 v* D6 | - //$my_post['tags_input'] = array('tag1', 'tag2');
9 y, t- J2 d3 q - //$my_post['post_status'] = 'future';) j+ Z# G: i7 z7 m3 `& Y5 Q/ ^
- ////$my_post['post_date'] = '2010-07-04 16:20:03';( R1 r0 v$ ~. w" A
- & ^+ ^, y, }( |+ M
- // Insert the post into the database9 k* m# ~* g; f
- $ret = wp_insert_post( $my_post );9 I& p% E3 _9 V; T
- echo $ret;# ~4 ~: U, j! Z H8 O
- ?>$ x: o' d8 _& Z5 ^ z
复制代码 我自己写的
; X4 d4 @; C% f- c. I插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|