|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑
, c5 }4 }) j" J! a& i9 R& T$ ~6 i$ K$ j4 W" K. a9 ?5 C; @
直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持& T2 A( v& Q; m- h9 {5 @! l* p
4 ~; y/ `6 M5 ~% o5 X- P我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了
2 m9 R! p }+ x2 A/ L' [ z$ x8 N& }0 g
方便实用 简单 何乐而不用呢
- K7 b! d3 h& a9 x1 B$ P& s- <?php
3 w: O5 G9 i7 G& r+ N - 8 n, c. M4 r% R. \& D; t2 Q
- define( 'ABSPATH', dirname(__FILE__) . '/' );
. H: i5 ]+ U9 L, Y - require_once( ABSPATH . 'wp-config.php' );
# G- }9 h r/ n$ Z" v - require_once( ABSPATH . 'wp-settings.php' );, x# c7 H7 v) Q1 D/ f: M O
- require_once( ABSPATH . 'wp-includes/class-wp.php' );
H4 |( G8 b: j1 n - require_once( ABSPATH . 'wp-includes/functions.php' );
( W5 M! N2 ]1 ^$ q4 o$ p# R - require_once( ABSPATH . 'wp-includes/plugin.php' );
" P5 D% P. w3 G4 ^ - $title = $_POST["title"];9 R2 V; q7 t6 O- D: t) {; k
- $content = $_POST["content"];' b3 A: C( m( ? ]9 i7 n# w
- $tags = explode("_",$_POST["tags"]);* t2 y) T& N. U$ `
- $cate = $_POST["cate"];
7 ]0 ]+ |0 R/ D& a$ i/ H# \ - //print_r $tags;
" O2 B2 r: B, \ - //
7 ^2 O( r4 e6 ]9 C, Z/ B% S" D% u! J - //
; _) S. `- T8 V - $wp = new WP();
! t5 o g3 O( u5 j3 r+ j - $wp->main();
; U* A U- ~! U4 H$ c0 f - //+ @8 L# P% s+ j" q# _/ o5 w
- $my_post = array();
) Z: x- h8 [1 r: u6 y% t1 w - $my_post['post_title'] = $title;
1 p1 o& U* S# U; n1 B8 u) f - $my_post['post_content'] = $content;
/ r1 c# Q, S+ V! v Z. M - $my_post['post_status'] = 'publish';
& _/ L' z8 t& x1 D% T - $my_post['post_author'] = 1;
* F* i7 V' N! M! P - $my_post['post_category'] = array($cate);$ t4 d: [0 I: E
- $my_post['tags_input'] = $tags;
* o J& p1 }. V$ D' W2 u) Q ~ - //$my_post['tags_input'] = array('tag1', 'tag2');
& B3 N5 X) D& g5 y1 |# H - //$my_post['post_status'] = 'future';( R8 w" b [' L8 o$ Y8 S
- ////$my_post['post_date'] = '2010-07-04 16:20:03';! O! ?9 u$ q! h. k
% v; R7 }* f5 L3 s7 z7 L- // Insert the post into the database* e+ t! \3 K: k, y& L! G1 ?4 P
- $ret = wp_insert_post( $my_post );
( m5 Z# Q s) D - echo $ret;% |9 X0 K3 G- ^& w1 _
- ?>
% B5 M3 O8 ~" L8 G! O; j% t" j! F* I
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气- & Y# }# l J) y3 Q# A
- <?php
0 Q" k, x' k6 W3 h - define( 'ABSPATH', dirname(__FILE__) . '/' );
9 o4 N9 ~. G5 v3 l$ @. X - require_once( ABSPATH . 'wp-config.php' );4 W, c4 U3 |* l# T. f. M
- require_once( ABSPATH . 'wp-settings.php' );
: ?& {; L$ Q( O# V6 T3 E# M5 d0 \ - require_once( ABSPATH . 'wp-includes/class-wp.php' );
9 S9 ]- D C/ y$ d: W - require_once( ABSPATH . 'wp-includes/functions.php' );* U$ P) E* o( @
- require_once( ABSPATH . 'wp-includes/plugin.php' );
w' d- J' b) ~. g! `2 P - $pic_name = $_POST["pic_name"];
9 f; z5 W6 I z* m& w( B - $pic = $_POST["pic"];$ L% y" f0 E" J. u
- if($pic != "" && $pic_name != "")8 z! s6 c9 j7 u& N$ u+ j
- {0 }" y# L! b3 L9 L- P3 p, T5 l
- $path = "images/";* f3 q) b0 }) s! W+ L2 I# Y% ~
- $pic_name = $path.$pic_name;
4 p% N4 O6 Z" o, M: ]7 M* z: }" G& O - echo $pic_name;
3 G$ d8 Y* e! {( V$ b7 E N+ U - echo "||";
* [* N: p3 {/ B. L) y - error_reporting(0);9 x9 t' t0 D# w& _
- mkdir("images");' O( Q$ T) M, x
- $handle = fopen($pic_name, 'w');! P p# o9 r. ]% b& S
- echo fwrite($handle,file_get_contents($pic));
$ v0 H. `- S; B - }
, G3 Z/ [; U9 @6 k+ s( k
- d/ I0 K; T$ L% p6 B- $title = $_POST["title"];
' G, _3 {; C5 C& T! a, f - $content = $_POST["content"]; x$ ~5 {/ {8 ?7 }
- $tags = explode("_",$_POST["tags"]);
8 V7 R6 \7 c* g5 m) ^5 { - $cate = $_POST["cate"];6 m. w8 o8 _. G6 i8 ~
- //print_r $tags;
2 G" l, B, C% I - //
" g3 G6 t; |. ?8 w# R1 ] - /// R+ }3 q* Y# R
- $wp = new WP();
* v" h* {9 p+ J - $wp->main();$ D8 l& L- d8 @. D! a( a
- //$ r \/ ~" J7 b$ P" b5 U, }3 ?% S
- $my_post = array();& x# C& z$ \6 v8 j0 S5 a
- $my_post['post_title'] = $title;# W& K9 K1 ^5 k2 C. i( [
- $my_post['post_content'] = $content;
' r/ ]+ X N- T0 w5 y$ ? - $my_post['post_status'] = 'publish';
/ S4 F3 X1 v; C3 H% c) v C$ j - $my_post['post_author'] = 1;# d) z& `2 T9 y8 h( e
- $my_post['post_category'] = array($cate);) e" t' |0 r7 |
- $my_post['tags_input'] = $tags;
+ M7 }* m" ~0 j" a d1 R" W6 W - //$my_post['tags_input'] = array('tag1', 'tag2');, i% t1 j& r& \% s8 y2 x
- //$my_post['post_status'] = 'future';
2 Z1 V/ b! C) w - ////$my_post['post_date'] = '2010-07-04 16:20:03';
# @% W l! x( _0 T, f9 { - - U& [' d! ?) Z- C" B' W
- // Insert the post into the database
% [2 Z+ a* {$ F+ [3 \' E# R - $ret = wp_insert_post( $my_post );
3 `4 L; ^- n+ [+ W6 U. O0 G - echo $ret;
; _8 `3 I/ Y% G - ?>9 S0 V8 K* F6 I- H7 i% r
复制代码 我自己写的
8 b' A' }/ O. Y8 j3 `+ w. }+ ?插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|