|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑
3 x% }0 m0 A, D2 P9 q- A. A2 N, Y, ^3 u, n% c# L* w# q p1 \
直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持
- X& R- P4 B5 S/ |: D. P* P' t. Q
我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了$ ^+ J: e: k! _7 K: T h
7 R% G' A$ p5 i" J* k# r方便实用 简单 何乐而不用呢- & n+ Y" s5 F- @. d
- <?php
9 s4 z% b' P& @
' b8 x6 C; A" n* c3 A% N- define( 'ABSPATH', dirname(__FILE__) . '/' );) `) U; Q* m2 F4 Z$ p7 a- L' V
- require_once( ABSPATH . 'wp-config.php' );, C8 E9 n7 O+ R" ~
- require_once( ABSPATH . 'wp-settings.php' );; X1 W, s3 u. C8 ]6 j
- require_once( ABSPATH . 'wp-includes/class-wp.php' );0 S$ p3 K; W1 b: \" [
- require_once( ABSPATH . 'wp-includes/functions.php' );
0 V* l. H9 [* r |4 R4 L - require_once( ABSPATH . 'wp-includes/plugin.php' );
- [7 m: _ X# i - $title = $_POST["title"];4 x6 m# ~' Y& U/ ?, {
- $content = $_POST["content"];7 Z. z6 i! M, I3 B' F9 A' i
- $tags = explode("_",$_POST["tags"]);) X& Y1 a! ]7 Q+ R
- $cate = $_POST["cate"];
1 M7 t2 }3 p( A2 _& s - //print_r $tags;
" D! {, F0 ^3 \ h8 T1 G9 f - //
: L% v! y4 q4 V0 s - //
7 K$ U" ^2 D) s2 y/ z/ Y. ~ - $wp = new WP();. H" }1 f1 ~% ]1 V
- $wp->main();: K. z) k0 r; }5 K3 ^7 L
- //# f) ~- [7 i( \# O
- $my_post = array();0 r( G) O% L. B: y
- $my_post['post_title'] = $title;
- y% l# h; f& p1 u* H - $my_post['post_content'] = $content;
! U9 C* R$ }! }1 {2 F - $my_post['post_status'] = 'publish';+ Y; v8 g8 f( B6 S: J# L
- $my_post['post_author'] = 1;& q2 l% z4 ]' S( C6 A6 R0 V* a
- $my_post['post_category'] = array($cate);4 c+ O0 ] J) ^9 }! f
- $my_post['tags_input'] = $tags;
, Y) O* q, |- O - //$my_post['tags_input'] = array('tag1', 'tag2');- |5 |9 i" F" l/ f! s
- //$my_post['post_status'] = 'future';. h, _5 e, w$ S8 l" N" e
- ////$my_post['post_date'] = '2010-07-04 16:20:03';
/ Y9 U. k- b. {, V
2 d9 Z* p8 `1 K2 e$ t- // Insert the post into the database6 z. z& x. U8 Q y# R& ^/ R9 ^/ O9 a
- $ret = wp_insert_post( $my_post );
+ @9 D& `: z' }6 b - echo $ret;
' [5 J" z/ i* A, | - ?>
" F! p# x/ F7 w8 E L3 h" x
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气
& A; H; ~8 x! x9 q2 \; r8 T! E; L- <?php
) k$ T; a* G+ A - define( 'ABSPATH', dirname(__FILE__) . '/' );
( m9 \3 o4 d5 ` - require_once( ABSPATH . 'wp-config.php' );: r4 ]* @7 ?3 [5 E- D: W3 @4 D2 f
- require_once( ABSPATH . 'wp-settings.php' );5 z0 J* ~' f# |5 V8 X* s1 P
- require_once( ABSPATH . 'wp-includes/class-wp.php' );
# [0 h7 s3 E$ Y8 q% X7 s* T - require_once( ABSPATH . 'wp-includes/functions.php' );1 f" S: q; M: [4 z' q
- require_once( ABSPATH . 'wp-includes/plugin.php' );. A% P4 z5 Y0 R; I
- $pic_name = $_POST["pic_name"];' |+ f. J' n7 @: X( [
- $pic = $_POST["pic"];& o& L" i) B! m- Y
- if($pic != "" && $pic_name != "")! r9 P2 ~' F1 V1 k. e, X
- {
( F$ ~5 x3 U1 a% e - $path = "images/";; R' K' y! D% B
- $pic_name = $path.$pic_name;- [' K4 X: V) i X! p
- echo $pic_name;) W: O& e' s, }6 \9 N
- echo "||";! ^! Y( A; y8 y6 f7 M9 ^* J* {
- error_reporting(0);
- L5 o7 e# u' [3 x; h4 [) c - mkdir("images");& p5 h9 o) |' m- d+ e6 [) S6 b+ g
- $handle = fopen($pic_name, 'w');
, L* N N6 D0 f - echo fwrite($handle,file_get_contents($pic));$ G1 d3 v" q2 Y1 P, H
- }
2 _$ Y* r" G* g1 ?# I+ _6 e5 u - % r M% D( e3 B8 M- n/ a `
- $title = $_POST["title"];
* o( C* X: U2 L1 D - $content = $_POST["content"];" S$ S( K# N- m) V- v+ s
- $tags = explode("_",$_POST["tags"]);, v0 C. c* `( s% U7 R2 z
- $cate = $_POST["cate"];
: S$ U7 c, A& S - //print_r $tags;
* ^, h, d# A/ g) v" Z+ Z - //$ ~% M+ p$ U I4 g. P3 X) i
- //1 b: i& l( E: d
- $wp = new WP();! f6 ^, R/ `' y( i8 c h
- $wp->main();
1 V. M% w4 V5 c) S% J% i+ Z; S - //
& M& C7 z5 L& K+ B6 P8 f - $my_post = array();- b! I& y3 {+ k. ^* l
- $my_post['post_title'] = $title;
) d: f6 O3 P0 q% L - $my_post['post_content'] = $content;
# |' \8 w8 z; X" f% Q/ }% ] - $my_post['post_status'] = 'publish';, A2 K9 G' y6 g( w( c
- $my_post['post_author'] = 1; q4 O/ V; A! r0 U$ e7 m
- $my_post['post_category'] = array($cate);
! F( O, t! b3 ^( K- J* {* K - $my_post['tags_input'] = $tags;0 \4 e$ y1 R+ F5 V
- //$my_post['tags_input'] = array('tag1', 'tag2');
4 p- n/ z7 Y* a# U u6 X. Y1 Z - //$my_post['post_status'] = 'future';
2 t. C5 M* U5 c' \ - ////$my_post['post_date'] = '2010-07-04 16:20:03';
h+ t% `( S x2 i% {: O - ( m" K2 g; i$ F8 v' K
- // Insert the post into the database
8 T$ D+ \- a: l: I N- s7 A - $ret = wp_insert_post( $my_post );" J0 q9 Y1 p6 h" C
- echo $ret;1 Y4 |6 k/ e& J5 x, Y! E
- ?>
/ x" {+ {% |9 A6 ]1 e
复制代码 我自己写的
4 t7 N" R4 C: c r" _, ]插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|