|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑 / _9 G' K4 O$ Y
( M; Z) Y* t6 J8 h- \直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持
8 v& c/ I2 W8 Y4 [7 [+ d; W
- S( N* h) Z, q: x我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了
4 Y. x6 W1 t' R6 [# p5 ?6 C ~7 m9 T
方便实用 简单 何乐而不用呢- , y+ r* \3 B" f$ w
- <?php
9 k# c# n& r& B ~: u& Y3 e
" I6 Y/ a( N8 J, Q( U- define( 'ABSPATH', dirname(__FILE__) . '/' );# @+ J9 _9 o$ h6 Y4 y6 F+ i+ I1 i
- require_once( ABSPATH . 'wp-config.php' );
& [( U6 j2 f; |" Y - require_once( ABSPATH . 'wp-settings.php' );
4 g+ F. A5 ]8 T9 W* Z/ h - require_once( ABSPATH . 'wp-includes/class-wp.php' );$ Q3 l1 `' E7 a% e$ B# W6 c
- require_once( ABSPATH . 'wp-includes/functions.php' );
3 z% f2 {9 z( m - require_once( ABSPATH . 'wp-includes/plugin.php' );6 @: n8 ^' G& z% F: a7 A. y1 C5 A
- $title = $_POST["title"];
7 S8 ^9 r5 c1 j* O( w6 }3 Z; { - $content = $_POST["content"];. ?' F) o2 x6 P/ B. H/ {( d+ L+ M
- $tags = explode("_",$_POST["tags"]);) F& e& R0 a l( z6 E3 U
- $cate = $_POST["cate"];
3 C# r4 |& A, G4 [ - //print_r $tags;
8 v0 r$ `1 `+ [# j: O& o3 T - //# ?& ?) O% E: d
- //
- [9 w j4 \+ n0 a7 D( o& d& } - $wp = new WP();
1 ]. f# f; K( p1 ~ - $wp->main();4 t( d8 e" l: r* |" v' c% d
- //
9 x$ S0 Z/ |3 i ^, d - $my_post = array();
! M. W' r( w& K8 r - $my_post['post_title'] = $title;
& Y! H6 J; }7 Q - $my_post['post_content'] = $content;
) Z# ?6 M7 s2 z7 x' o - $my_post['post_status'] = 'publish';
2 ]2 b9 `9 E8 c1 C4 f2 i4 e0 f - $my_post['post_author'] = 1;, r w4 f |# s; X& W
- $my_post['post_category'] = array($cate);
) a; h) `9 ?5 x7 f1 Z - $my_post['tags_input'] = $tags;$ R* Y! a' v& k; F; |
- //$my_post['tags_input'] = array('tag1', 'tag2');- I; \- l' v3 C% v6 T* B& ~; t7 T
- //$my_post['post_status'] = 'future';
9 ^1 l9 K! _& H9 p! w2 W( i! d - ////$my_post['post_date'] = '2010-07-04 16:20:03';* @5 `) p. D9 S
- $ D1 v1 F. R6 m6 u6 @
- // Insert the post into the database" j( ?# b% \; u) W. D
- $ret = wp_insert_post( $my_post );
8 r$ a4 O% g& y: R1 K5 N3 E" R$ B! W# C - echo $ret;9 q1 u' z1 m; I, ~! V9 d
- ?>! }$ v6 a9 I. L- v4 t. O
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气
/ g/ C$ V" ^+ s6 C- <?php9 n0 r3 S) z0 \+ E
- define( 'ABSPATH', dirname(__FILE__) . '/' );
4 h3 B, x. w- @) q% J9 S: n - require_once( ABSPATH . 'wp-config.php' );
% `/ Z( H. T% o - require_once( ABSPATH . 'wp-settings.php' );2 M/ H4 h6 Z/ l8 D- i9 t
- require_once( ABSPATH . 'wp-includes/class-wp.php' );
5 B& f% D" g) s' [7 K+ Q - require_once( ABSPATH . 'wp-includes/functions.php' );, y( V, b, L' K" B1 ~
- require_once( ABSPATH . 'wp-includes/plugin.php' );4 K3 C. C; Y) n
- $pic_name = $_POST["pic_name"];
$ G4 v5 @2 u, c0 V - $pic = $_POST["pic"];0 n4 r' J0 _9 M& m$ ^* G
- if($pic != "" && $pic_name != "")7 v! |2 d5 g9 _7 ^1 o+ A1 [
- {5 e# W/ c9 [6 P5 k# Z4 |
- $path = "images/";
$ b m% x S# l9 M - $pic_name = $path.$pic_name;
( G6 e5 j# v3 ^0 v$ q. c7 X - echo $pic_name;
7 l$ i' P* S" Z i' c - echo "||";
& W. U8 R/ G3 ^2 | - error_reporting(0);
) y7 i7 x! k. I+ b0 V7 F) K! d - mkdir("images");
4 ?- Z2 {' b0 c4 |1 { - $handle = fopen($pic_name, 'w');! W8 @ b ]# y6 ^ ~) _# }8 H
- echo fwrite($handle,file_get_contents($pic));
6 y1 C9 l. t% p! V - } \& u- `: y( j" ^+ M3 Z% v1 h
- 0 [5 R' s x9 B$ O& M
- $title = $_POST["title"];, V6 z9 |( t- E3 ~6 u- {/ D
- $content = $_POST["content"];: W3 }6 Z9 ^0 o* o; P$ U' L+ T
- $tags = explode("_",$_POST["tags"]);
4 A: F8 l% F, F" t ]3 C$ o4 O. R - $cate = $_POST["cate"];
. E# F, C& q* |; z - //print_r $tags;
4 v7 g9 B) z3 {- U3 [# x" i N - //9 N; S; a$ _/ {0 b$ u: T
- //
; k' v$ z" z0 |0 C, \( n8 N: s9 u - $wp = new WP();% Y$ \7 F( l/ D" o
- $wp->main();
" h6 ^. c6 G1 m6 v# J+ f2 \9 @ - /// o2 @* k6 p- W: g) D
- $my_post = array();9 b6 J$ V9 _# s# r6 Y8 n D
- $my_post['post_title'] = $title;2 \8 {6 O! r! K- F: C' [: G& c
- $my_post['post_content'] = $content;5 L, p& U( D: P/ T" e0 \$ o. y
- $my_post['post_status'] = 'publish';( z' e# j5 {& `; w1 e) N2 Q
- $my_post['post_author'] = 1;
& @; A8 r9 e: R: f" i( n" G - $my_post['post_category'] = array($cate);; e8 q i6 p9 `! d
- $my_post['tags_input'] = $tags;4 s. ?1 u# ~, m# B) |
- //$my_post['tags_input'] = array('tag1', 'tag2');
7 S8 G1 N, o$ w# _1 v+ A - //$my_post['post_status'] = 'future';1 x& A3 x# I: v2 Z
- ////$my_post['post_date'] = '2010-07-04 16:20:03';! i3 T. {! n, z9 \5 g% }( k& E
6 d9 [: A3 `) V B8 i$ \8 j- // Insert the post into the database# |! J( z4 W! R/ B, O
- $ret = wp_insert_post( $my_post );
! Q9 m- P( m+ w4 e. C' c - echo $ret;# T8 N. l% A* X- U. X" S( y
- ?> f4 d! j2 Y1 z
复制代码 我自己写的
- `+ \! ]: N9 ~/ W% N3 M1 s插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|