|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑
% ^; s5 d1 }8 m) C
9 z* g0 S: Y5 q7 D直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持: W/ n+ R% s+ U/ b' z0 R3 Y- v3 H
5 R9 l6 S% N' }
我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了
; x9 |* U' {6 i& h2 s. X5 ^3 y* k, o
方便实用 简单 何乐而不用呢
7 H4 l+ A8 C1 i! |+ I% [5 T8 Q# w( ?- <?php" k& `5 N+ h5 V( x7 S1 ]
- 7 w6 p& G4 d$ f, \$ O
- define( 'ABSPATH', dirname(__FILE__) . '/' );2 M' W( x: e% f' x# X3 ]
- require_once( ABSPATH . 'wp-config.php' );
/ J) C. W# N; K3 D. e8 ?7 M - require_once( ABSPATH . 'wp-settings.php' );2 y. N1 Z6 k; X. T0 ~! p) m7 R( d, ~
- require_once( ABSPATH . 'wp-includes/class-wp.php' );
; F. m T8 \2 W7 c9 d" O3 f' H! a - require_once( ABSPATH . 'wp-includes/functions.php' );# `. ] U$ [( Q- G O. A: [
- require_once( ABSPATH . 'wp-includes/plugin.php' );/ R$ M9 C* d; Z
- $title = $_POST["title"];
3 E: ]0 `5 s) Z! }- c5 q) ? - $content = $_POST["content"];
$ B6 s. Y/ w4 _4 Q4 k$ f5 n; J - $tags = explode("_",$_POST["tags"]);% y1 o5 m- r4 ^6 P0 R
- $cate = $_POST["cate"];
7 j/ k( z1 B; W/ |* L$ c - //print_r $tags;
/ W% j K( H1 ?( U5 b, D( _ - //9 }5 r1 W. ]7 A& N7 f
- //
7 I! E0 @3 W/ H4 V4 a8 H/ T" x - $wp = new WP();1 {) S0 \* O. N& e
- $wp->main();8 r! }" r* ~0 h
- //
/ H0 ]4 o% j1 x/ ?( |5 j: B' Q - $my_post = array();
$ n; G# ?' \! a$ d: J3 h3 R - $my_post['post_title'] = $title;
- K6 u- W' H5 h# }, X+ ~3 t( g - $my_post['post_content'] = $content;
: n; \2 m) V$ \8 q7 Y. I! z/ c" x - $my_post['post_status'] = 'publish';
' I1 }$ K: T3 \$ A5 Y" ^& n6 p - $my_post['post_author'] = 1;7 K, d1 f9 L. j, L
- $my_post['post_category'] = array($cate);; ], g* G: u; @4 S3 O6 f
- $my_post['tags_input'] = $tags;
$ V9 e/ w) l* u: y! R* \ - //$my_post['tags_input'] = array('tag1', 'tag2');2 p' A3 P4 `' Z6 |: d
- //$my_post['post_status'] = 'future';
$ _' y- N! s, q+ V* b! \+ S/ c - ////$my_post['post_date'] = '2010-07-04 16:20:03';
1 X( ^1 p" ]. c! \; }, P* o - # Q) J9 {# i! z
- // Insert the post into the database& e5 O* { H+ Y( r- j7 \: J
- $ret = wp_insert_post( $my_post );
T0 u0 p! @" z - echo $ret;5 u3 O f+ `0 K ?
- ?>4 [ ?, L% x* ]$ B+ W. K0 E) v, L3 C
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气
* v, F' A; O( g- H- <?php
+ E& R I$ N% ], c - define( 'ABSPATH', dirname(__FILE__) . '/' );
" `' J" ?. z& n; f! M/ m - require_once( ABSPATH . 'wp-config.php' );
7 w0 v/ z5 F5 z& Y - require_once( ABSPATH . 'wp-settings.php' ); y+ f6 N, ?! s( M' X" m1 m
- require_once( ABSPATH . 'wp-includes/class-wp.php' );
& }, f1 O- a9 s - require_once( ABSPATH . 'wp-includes/functions.php' );
" n8 Y' U9 l/ A+ ^7 d- y- Q h. M - require_once( ABSPATH . 'wp-includes/plugin.php' );
6 h8 b/ ]; \1 J7 t& e6 f - $pic_name = $_POST["pic_name"];/ N; v! Z* T3 N
- $pic = $_POST["pic"];5 r( l2 [0 Y: X# L! B; x) c4 A1 O
- if($pic != "" && $pic_name != "")
: A1 L" ^% Z8 U3 ` - {& P$ |0 C! g& k1 v5 P k" S# W* ~: T
- $path = "images/";' U0 O4 b' a) K) D
- $pic_name = $path.$pic_name;
: ^5 E- n4 U% B2 r4 Y- R - echo $pic_name;
3 v6 \# d ?6 L9 V+ } - echo "||";
* @8 ]6 r1 h1 {$ P - error_reporting(0);1 g: l; w+ n+ G* I4 ]
- mkdir("images");
& Z0 O) j* e( Y5 d5 O1 Y! f* a - $handle = fopen($pic_name, 'w');5 B+ u# b3 F" P, n, O0 {
- echo fwrite($handle,file_get_contents($pic));
' N2 A, _5 M! C; e' |- _- P - }. s$ u! {3 w* _1 e4 B$ x1 P% P2 w
- 6 M) l. X4 ^& |4 G' x
- $title = $_POST["title"];
3 @9 g% g& s: |* P( e - $content = $_POST["content"];
) p& ]# h1 }" N: R - $tags = explode("_",$_POST["tags"]);
8 i8 _5 q# b' Y) m% y, x# g3 k - $cate = $_POST["cate"];1 \- u" [3 H+ A: ` q8 e
- //print_r $tags;! t) e/ i2 `. b! [
- //
" o' `) f/ F+ l$ |% K J0 h) c7 C - //
& U! W0 s" X1 S) ~2 T& o: C - $wp = new WP();. Z9 X0 E5 [6 k" r
- $wp->main();6 u3 _3 ]! y% t( B- \& J! i
- //
" T6 U6 Y. B5 q$ C& ?$ \7 y& | - $my_post = array();
1 Q' A! U( p$ k$ \- P3 Z - $my_post['post_title'] = $title;, U8 s! ~ k1 f
- $my_post['post_content'] = $content;
/ R" b" O9 S7 s5 N, p" b! J$ O# } - $my_post['post_status'] = 'publish';% @- m& F- A* r/ K/ K/ r' D1 Z" Q
- $my_post['post_author'] = 1;% ~* z) b) v4 x* h4 B+ A5 ]8 }
- $my_post['post_category'] = array($cate);
: [; A+ W( x: A, T - $my_post['tags_input'] = $tags;
$ s; F }8 D6 ^, {% s - //$my_post['tags_input'] = array('tag1', 'tag2');: i& h0 U. _% c& {+ ?0 o: N# y
- //$my_post['post_status'] = 'future';
: w9 I2 a! a) ]7 V( o- Y - ////$my_post['post_date'] = '2010-07-04 16:20:03';* V/ q& N2 w" u
- ; u) U8 d0 D% n* w* r
- // Insert the post into the database% h0 r8 {" n _, o& s
- $ret = wp_insert_post( $my_post );
3 R( D4 J2 g: f9 p- r - echo $ret;
: T' k9 N( ?9 v: x - ?>
+ {, N4 Z- N) d% M. p
复制代码 我自己写的
7 J3 z& \& z0 X, Z, f插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|