|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑
% S9 Q# F3 T$ h# R6 h. k
: w; {; l& g! Q- j+ M5 ?' `直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持: o, J# @" V2 ]
7 `" l2 {- G1 u& x我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了, P* L, r% \4 l" l; s5 s/ a4 Q6 C
+ }* n- @: y0 Z! o: @* E3 U$ q) ]
方便实用 简单 何乐而不用呢
/ D) _# c' a3 j5 M! n; ^- <?php
h3 g+ J5 G2 D9 A( u7 @# x7 d - ; q* G) ^; v. k% U. c5 y5 A
- define( 'ABSPATH', dirname(__FILE__) . '/' );" f- U/ k" J' l% k4 B5 [
- require_once( ABSPATH . 'wp-config.php' );* X0 O' _3 S$ g( W$ O8 i+ U
- require_once( ABSPATH . 'wp-settings.php' );$ J; t- M3 {* }) ?/ R- O( I; w
- require_once( ABSPATH . 'wp-includes/class-wp.php' );# j1 P" f& n4 O/ P5 _$ u
- require_once( ABSPATH . 'wp-includes/functions.php' );
6 W x5 i0 u% T4 b" t. G9 @6 g' S - require_once( ABSPATH . 'wp-includes/plugin.php' );, T' `" J+ u' T: Z
- $title = $_POST["title"];
7 t7 W* c& K* I8 }5 e6 I4 U- w* p - $content = $_POST["content"];& u9 @1 q& _" C9 V# Q2 [2 \. B6 _
- $tags = explode("_",$_POST["tags"]);
) B- z; e! {6 A" Q - $cate = $_POST["cate"];
4 h; f' S' V2 j6 O5 w& n2 k. Q - //print_r $tags;
9 Q$ J3 u- _* L# K, G7 e - //9 l9 f* I4 I; Z
- //
! g5 q* V+ b. v - $wp = new WP();
) U! S8 H# p* a: A+ B - $wp->main();
5 o. T2 s: o+ S5 ~- c - //
* w9 z. ~5 q1 s! l - $my_post = array();2 r1 @; s! e8 v2 m: k
- $my_post['post_title'] = $title;1 \! x8 i! Q+ l7 |# I+ c
- $my_post['post_content'] = $content;
g" E$ z9 G: D4 S, b9 t - $my_post['post_status'] = 'publish';+ E1 N# e, Z) J) x P1 p' z
- $my_post['post_author'] = 1;. }7 |$ `# K+ [2 b2 z
- $my_post['post_category'] = array($cate);
6 Y: A G$ p+ L3 }8 R' {' E - $my_post['tags_input'] = $tags;; V" T S; U2 @2 c! g W5 O
- //$my_post['tags_input'] = array('tag1', 'tag2');" s( {: S/ M0 P8 }
- //$my_post['post_status'] = 'future';* m: F6 ~7 V+ ]" A( m
- ////$my_post['post_date'] = '2010-07-04 16:20:03';
" ~# @/ K. `' N# t# `2 {
5 h1 A& W0 @4 g2 }5 P- // Insert the post into the database, D0 [- a+ c) S9 p# E
- $ret = wp_insert_post( $my_post );. Q# u2 h* Y' m2 B9 s+ P
- echo $ret;( @8 M \6 J8 l3 y3 K9 m
- ?>
% N! ]0 K- D" D0 v. u3 G
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气
- `8 r3 z3 p+ m+ X0 `. J- <?php
" e; D9 h; J! Q6 r4 t( Q - define( 'ABSPATH', dirname(__FILE__) . '/' );( ]* ]% p' M) Z8 P# h2 \3 }' {
- require_once( ABSPATH . 'wp-config.php' );$ m+ i& M( @' V9 L a! J
- require_once( ABSPATH . 'wp-settings.php' );, {! K5 g: X9 Q5 u' @4 g4 x
- require_once( ABSPATH . 'wp-includes/class-wp.php' );3 O7 K$ ^7 ^; _0 y
- require_once( ABSPATH . 'wp-includes/functions.php' );
1 m- P" b+ V) V( X0 [* R - require_once( ABSPATH . 'wp-includes/plugin.php' );
+ t" V0 D+ }6 y7 B: d" U - $pic_name = $_POST["pic_name"]; l x9 z+ [( ~5 r
- $pic = $_POST["pic"];
M/ B% C( A5 e0 o1 K - if($pic != "" && $pic_name != "")
* l- W- ]" E4 u# w) i" A- R2 m - {
6 i/ X9 C8 O" ^% _7 J - $path = "images/";
5 Z0 q" w N6 P0 o - $pic_name = $path.$pic_name;6 b/ Z X8 F& _1 n3 \$ A/ v/ ^8 Y
- echo $pic_name;
" i5 i; r9 S8 F7 D( N: t7 i - echo "||";
" h& W3 m& I4 c8 t9 j h5 P0 l0 _ - error_reporting(0);5 a1 l5 a- _4 e& Y
- mkdir("images");
0 w4 s( E5 [- w& E- q4 C+ e. } - $handle = fopen($pic_name, 'w');
: O9 Q" Y" h8 e8 Z ~. C - echo fwrite($handle,file_get_contents($pic));* m* M( ~3 V f5 \: [
- }2 X* g; T% v- T+ x7 K7 K1 {$ D
! W8 k3 G) D2 l8 V+ G8 k- $title = $_POST["title"];
7 t/ [* K/ }( Y v - $content = $_POST["content"];2 A" G& g- Q# F( ^: p( f
- $tags = explode("_",$_POST["tags"]);4 }; \6 l% _3 _: @3 R7 B; v
- $cate = $_POST["cate"];
9 y" y ]# H. S9 H0 K! \# s( m2 L - //print_r $tags;' t* v0 }, W6 g4 ^) I: i- |
- //$ @- ?( w& a' M0 G- d4 B
- //
6 p1 A. N' N9 {4 p! | - $wp = new WP();) W+ Y2 ~- ^" f/ t
- $wp->main();! O, N2 b: [! m
- //
. z/ s: |% m* L, i* l- \# @$ C - $my_post = array();
1 h- H* h: c# ~% O - $my_post['post_title'] = $title;
' T; l% J" S: \! f - $my_post['post_content'] = $content;
0 t. b) ?) o* t. A; y% b9 R - $my_post['post_status'] = 'publish';
. q+ x t+ N3 h3 y) f- ^6 k - $my_post['post_author'] = 1;) U1 w0 G. f3 ~# Y% F+ M$ Y
- $my_post['post_category'] = array($cate);
; g$ w& N8 t* m: u9 ~0 @ - $my_post['tags_input'] = $tags;
# i% @; D: F. j. i, p! X i - //$my_post['tags_input'] = array('tag1', 'tag2');9 p( W( E) |# q
- //$my_post['post_status'] = 'future';( y, R0 T: o7 p" i C! @3 l
- ////$my_post['post_date'] = '2010-07-04 16:20:03'; N C6 `" Q: i; K3 m8 m: C$ g% y
/ J2 _9 f" q9 ^: K2 g- // Insert the post into the database
0 H3 \/ P) Q) [& j) e+ T - $ret = wp_insert_post( $my_post );
, _5 ~5 `+ L# [! c - echo $ret;/ Z! y4 T( C4 Q
- ?>- f! S' ~. f. @6 |: G a5 t6 n
复制代码 我自己写的
: X5 j$ t4 Y" b' R$ u, n; Y插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|