|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑
0 I3 J. _0 y& D: V, \6 M. I- l' l7 Z' @/ n
直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持. M& B* k) o5 N- ]+ N
2 A. v2 { _% v7 [! Q
我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了
+ z }/ Y- f# O1 g, [4 T8 @2 s8 j& u! k/ H/ O) D( W: }
方便实用 简单 何乐而不用呢- 1 B, [2 T% T. F2 |4 ^& _" @4 a
- <?php
- M! D6 U6 Z1 X' ~6 L
( T2 D( u! ?7 Z, q% {7 J- define( 'ABSPATH', dirname(__FILE__) . '/' );
# T+ N4 f+ L+ E: P7 o4 D - require_once( ABSPATH . 'wp-config.php' );
0 V; K8 I* l4 C/ H7 o6 _2 R - require_once( ABSPATH . 'wp-settings.php' );
' s4 E' ~0 D) M9 _, w1 E - require_once( ABSPATH . 'wp-includes/class-wp.php' );
! C! G" R% ^/ L# n4 H - require_once( ABSPATH . 'wp-includes/functions.php' );% \% T" t, a5 i; `7 T% Y
- require_once( ABSPATH . 'wp-includes/plugin.php' );9 `0 X: ^8 v* l Z2 ~! ~+ K
- $title = $_POST["title"];
6 K( Q Z! ~( i) o$ ]/ l - $content = $_POST["content"];6 J0 ^( {" o4 ^, H# O& \
- $tags = explode("_",$_POST["tags"]);
# g3 B/ Q1 ~" v$ c0 ?5 b - $cate = $_POST["cate"];* v, _4 q* J1 B6 B
- //print_r $tags;! u; ]$ h1 b8 U0 |0 u
- //4 m; {' x) F0 D- S; p% c' _
- //6 o5 }$ q6 e* D
- $wp = new WP();: ^0 J) `0 v8 y' l: O5 Q# z( T
- $wp->main();; ~- G( a% [3 h7 d; m% h- m9 I
- //
; Q! M* i3 K: {7 a. `; o - $my_post = array(); B# ]" |! @0 F; {
- $my_post['post_title'] = $title;' R9 i, y: h4 {: c- \
- $my_post['post_content'] = $content;. e' m/ s7 V5 Z. T
- $my_post['post_status'] = 'publish';
; {( w$ H0 |9 ^# r1 U - $my_post['post_author'] = 1;
9 O5 t% O0 K+ I: \* r9 e% z - $my_post['post_category'] = array($cate);
6 D: U& }2 o' q8 \, I2 A - $my_post['tags_input'] = $tags;
9 v, Z! z3 A' a4 ~! V' m - //$my_post['tags_input'] = array('tag1', 'tag2');
8 |4 F+ u! z0 U9 {4 o" b# M - //$my_post['post_status'] = 'future';
5 x) c' j; u0 j - ////$my_post['post_date'] = '2010-07-04 16:20:03';
9 n% i1 N; j' ^" Y$ n - 8 h Q; t3 N* A. J" {
- // Insert the post into the database
. k- E, H* S4 O' B- B0 E - $ret = wp_insert_post( $my_post );
7 l' }& A5 W# u6 U9 @9 Z* | - echo $ret;
, x( L5 R V3 B6 U5 { - ?>
' U3 P2 p& v6 ?7 V* ~& D
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气- 8 y. H) e) s5 g$ _: ]
- <?php" n$ J9 s' d1 l; @0 r4 J- f! h- r ?7 D
- define( 'ABSPATH', dirname(__FILE__) . '/' );9 [1 y$ I7 C. n' K3 {5 H! A, t
- require_once( ABSPATH . 'wp-config.php' );
8 l5 @; U' J4 i j7 U8 \+ M6 K$ G9 x - require_once( ABSPATH . 'wp-settings.php' );
. z. n* b3 I( _ - require_once( ABSPATH . 'wp-includes/class-wp.php' );7 @! d& f* }1 }5 w
- require_once( ABSPATH . 'wp-includes/functions.php' );( E+ h1 C8 i; r# B9 |
- require_once( ABSPATH . 'wp-includes/plugin.php' );
- e& ]6 @7 o& B4 u1 Z0 g% f+ _ - $pic_name = $_POST["pic_name"];" ~1 }6 i3 o8 u% R& D+ i
- $pic = $_POST["pic"];' ~ t+ A; M) N7 l
- if($pic != "" && $pic_name != "")
* G: _/ U# `) y9 P' U2 } - {
4 \/ y6 s! L5 k7 O9 v - $path = "images/";
. @$ c' C# j L) b/ f* S1 E( z* ]1 s - $pic_name = $path.$pic_name;
" V/ {7 q3 l4 p( B; j& a - echo $pic_name;
( g+ t% x& i6 L& O) n - echo "||";9 ^8 y! J9 f# X6 w
- error_reporting(0);
$ X/ }* `9 j% X" j+ v4 c - mkdir("images");6 \- I3 h! c {
- $handle = fopen($pic_name, 'w');
4 C; _$ K# V2 S2 O6 n' V - echo fwrite($handle,file_get_contents($pic));5 L: i8 O0 }9 E& o8 ?% `
- }* {. t- F3 [8 y
- * G1 I% G- W/ q6 u @3 C
- $title = $_POST["title"];
/ }; E: q4 t. ?8 U' M. M( W1 F - $content = $_POST["content"];
" _/ X1 O* Y: @( z# p- J - $tags = explode("_",$_POST["tags"]);. w* P8 [, W% m, ]
- $cate = $_POST["cate"];
; h% t- G0 I: P) ?% j - //print_r $tags;
0 E6 }9 t7 }( x2 Q2 e - //" }% C( j' s6 Q }5 c/ P
- //
/ h+ ], n7 ~. D% Z, ^ - $wp = new WP();
, v. `/ F5 o2 R3 Z - $wp->main();) ~/ P" _2 |6 n/ \' i2 M
- //
9 e5 b4 x0 }0 P4 l - $my_post = array();3 f3 I& \" y2 U( V2 E
- $my_post['post_title'] = $title;
- l! B2 Z- {# ^; r$ U - $my_post['post_content'] = $content;
% R2 ?: `5 P) z M% ~# `" o6 v - $my_post['post_status'] = 'publish';2 ~* S: @# h* T' A, M* @2 [
- $my_post['post_author'] = 1;
% b+ N. i2 e. D+ Z2 U - $my_post['post_category'] = array($cate);. S4 C" Q0 R1 r9 f- [( J+ V/ c
- $my_post['tags_input'] = $tags;8 u& L E% o3 i) R
- //$my_post['tags_input'] = array('tag1', 'tag2');& M" `2 r1 E, a) G6 g
- //$my_post['post_status'] = 'future';
! l2 P* x0 J+ G9 ]6 f - ////$my_post['post_date'] = '2010-07-04 16:20:03';
4 E# q- g3 V& k, g - 8 A3 I( I% _* N8 Q7 C! w' f+ N2 ]
- // Insert the post into the database) ~3 r/ Z* [6 \. Z3 T9 Q+ Q
- $ret = wp_insert_post( $my_post );
: ~, ?# T+ S3 B6 m+ c0 e6 t9 e - echo $ret;0 ] M; R4 ^1 y8 s7 p5 J
- ?>0 G. D {% Q2 f4 ?
复制代码 我自己写的
1 z* j# e. B+ W4 ]插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|