|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑 5 Y% _! ~9 E# E: D9 E4 |
4 [/ U/ M- {, |: Z, t
直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持+ n3 H+ @8 `5 x' H6 v% P+ a. F1 F
+ B6 T; U: d$ u3 ?( y
我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了# j' M& {% }, M& e `
7 X. f" `6 L R, ]2 d
方便实用 简单 何乐而不用呢
, Q1 X% V c! }- Q/ }- <?php: N+ s( c0 B7 P
# w- N( [6 y* X3 M3 k! Q% S. g- define( 'ABSPATH', dirname(__FILE__) . '/' );
. {/ ]/ n/ N. N+ V" B - require_once( ABSPATH . 'wp-config.php' );8 `0 `- t6 X* w
- require_once( ABSPATH . 'wp-settings.php' );1 h( h5 U! p3 K* @4 R( N. {
- require_once( ABSPATH . 'wp-includes/class-wp.php' );, ~# `1 H8 F! H& w+ q
- require_once( ABSPATH . 'wp-includes/functions.php' );9 n, k1 L0 g' O$ a4 i8 Z6 q/ m! j
- require_once( ABSPATH . 'wp-includes/plugin.php' );+ _1 w$ I; {( B3 D3 _# j2 f
- $title = $_POST["title"];
( a$ n; G: M" r W - $content = $_POST["content"];
, ~% v0 ~, L6 \$ e! B$ S( t - $tags = explode("_",$_POST["tags"]);9 n0 k2 M8 |' g" @+ z
- $cate = $_POST["cate"];7 e" Y3 |" L# [
- //print_r $tags;6 {. A6 s0 S% p. E% F
- //) Y ~/ O2 U, F) a0 T/ H; ?
- //! D- Q& {2 X. _3 ~
- $wp = new WP();
, F7 C- ?2 |5 B7 n3 d" h+ g - $wp->main();
# p. Z7 k8 @0 W, h' |8 ?8 |+ D - //
_( V& x9 u: U" C. f - $my_post = array();, x: O* l; F7 r" Z7 I
- $my_post['post_title'] = $title;
( @" j; C: B4 C - $my_post['post_content'] = $content;
, M+ \8 V9 d9 O0 _ - $my_post['post_status'] = 'publish';3 o) l U* b/ F
- $my_post['post_author'] = 1;
8 q" |8 Q4 m& n1 i% D) g: e - $my_post['post_category'] = array($cate);6 M! m+ I! b, n4 `- P
- $my_post['tags_input'] = $tags;
% N( v* t: C4 r+ x( m5 l1 N - //$my_post['tags_input'] = array('tag1', 'tag2');
7 V7 O; [& d5 V) Q, L Y/ H8 o+ z d - //$my_post['post_status'] = 'future';
- K% y: w# |' G - ////$my_post['post_date'] = '2010-07-04 16:20:03';/ Q- b$ Y% A+ \
, k7 \4 {7 [4 ^- Q* p- // Insert the post into the database
; J Z' X& F$ q( \ - $ret = wp_insert_post( $my_post );/ R% n) i/ G5 q
- echo $ret;0 E0 a3 [& j: S8 K5 [
- ?>
% z! m5 x5 R* W. |
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气- ( C' S- P1 @/ d
- <?php0 J, ~( i) X+ ^* ?( a& N1 `
- define( 'ABSPATH', dirname(__FILE__) . '/' );; r& O* }& r0 i' \3 u
- require_once( ABSPATH . 'wp-config.php' );
8 N' r5 Y, I% C$ z, t' S; y - require_once( ABSPATH . 'wp-settings.php' );
, s ]" T$ L/ ?3 G- H - require_once( ABSPATH . 'wp-includes/class-wp.php' );$ r9 C. m8 ]/ [9 W: w8 q: I
- require_once( ABSPATH . 'wp-includes/functions.php' );
) z$ P/ y7 U1 r+ ]+ \ D - require_once( ABSPATH . 'wp-includes/plugin.php' );3 x: R, ?6 z- ~- r2 F1 X: V$ N
- $pic_name = $_POST["pic_name"];
3 @7 ~0 h7 @2 a) {7 {$ L - $pic = $_POST["pic"];
) _& C( Z9 t* a' z6 E) p8 H - if($pic != "" && $pic_name != "")
* a8 ~, a3 Z$ v2 }1 X* s* l$ T7 b - {* E: i# Y; O6 {2 S* P% w
- $path = "images/";
% L2 M# D4 q' ?4 t - $pic_name = $path.$pic_name;/ u3 u) h7 N; @ f& {
- echo $pic_name;* N$ ]! o% P( g% b- R" w
- echo "||";9 X( N4 \7 I/ `
- error_reporting(0);0 N( {* x8 d% B9 r' x0 K
- mkdir("images");
. q8 y$ ]3 r* R6 f# R4 S- ]0 q7 A - $handle = fopen($pic_name, 'w');" F8 T' M" N! M
- echo fwrite($handle,file_get_contents($pic));3 u- k, p' G3 \6 o
- }
$ { c# ^; Z3 d2 Y# @+ r5 { - 1 g l6 D* A" e: }. }+ d2 U
- $title = $_POST["title"];2 [ q" V* E& p* [5 U4 O' ?
- $content = $_POST["content"];: v0 U* P# T/ g1 ~# K$ _: j
- $tags = explode("_",$_POST["tags"]);
, y* J* G- J$ G' y - $cate = $_POST["cate"];
0 m D/ A4 x/ f8 S$ F; j - //print_r $tags;
' x$ ?0 z2 y3 \3 n l5 Z8 }$ m - //" N2 v* m- [- L/ _1 P9 s$ y
- //
" p$ o, {( {2 C6 M {" V8 X - $wp = new WP();/ s7 [0 s) {# M5 K' Q1 E) ]$ j0 f
- $wp->main();: V" p8 N3 d# A: B
- //
) R9 f4 P$ m% d - $my_post = array();/ a) c" ~8 [' {) O4 v8 S9 B9 o4 P
- $my_post['post_title'] = $title;5 W7 `% i. |; i( x6 ]3 i
- $my_post['post_content'] = $content;, I( l7 Z- }$ X4 k ^8 v
- $my_post['post_status'] = 'publish';: ]! o. d# }3 T& Y- _5 L; E
- $my_post['post_author'] = 1;
* p) {/ _8 K0 [4 ]6 { - $my_post['post_category'] = array($cate);9 i4 m( {9 t0 B) R- c
- $my_post['tags_input'] = $tags;
; K2 a4 Q) \+ I5 H* X) T4 u) | - //$my_post['tags_input'] = array('tag1', 'tag2');2 x) ^: a4 U, Q; L# P( ^( f7 T
- //$my_post['post_status'] = 'future';; h. g1 u# _! S# A, I! m; \$ ~
- ////$my_post['post_date'] = '2010-07-04 16:20:03';
8 e. Y- B) z1 q6 i( Q- Z8 c7 r - 9 Y% O I1 Q7 `! E. ]
- // Insert the post into the database3 |1 V9 }" j5 \* S; N, i
- $ret = wp_insert_post( $my_post ); W8 U- X- u& Z5 u! e" Q
- echo $ret;' Y$ H e8 {: h+ q) f
- ?>
+ T" i* s# W# v6 k4 x, a: {
复制代码 我自己写的+ e0 u* K0 ^5 I; l) F% J
插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|