|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑 5 ~" ?- H# y R8 n3 S
X2 h1 \& g% Y2 O4 I2 T直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持+ S+ w0 w) l o, ]& t% |
# e8 N& K& _5 Q5 z
我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了
' k, o) o1 o7 j* J m7 d
( L' @& Q5 k2 {* T& S方便实用 简单 何乐而不用呢- # p( `! V, [/ |9 o$ ? \
- <?php
2 H# f! Y. [7 w A# m1 a0 g - 1 ]5 j1 ?: W" L( t
- define( 'ABSPATH', dirname(__FILE__) . '/' );9 ]4 [; ~& K+ K- V
- require_once( ABSPATH . 'wp-config.php' ); A I) q2 M" m3 r7 t5 O; p
- require_once( ABSPATH . 'wp-settings.php' );; T% O: h- s% j9 g# P; r
- require_once( ABSPATH . 'wp-includes/class-wp.php' );
: q" T# z* R3 H N n% D3 t( R1 I* u - require_once( ABSPATH . 'wp-includes/functions.php' );, z$ q$ ^( {) a
- require_once( ABSPATH . 'wp-includes/plugin.php' );
1 T, f. N) b7 i; \% A4 C4 t& [7 b - $title = $_POST["title"];
; r$ Z- A, Y1 F& _ - $content = $_POST["content"];* e$ g x4 _' [ `* }3 f
- $tags = explode("_",$_POST["tags"]);
( p+ y% e6 L+ ~7 n( } - $cate = $_POST["cate"];
6 u4 G# V- C/ J: H+ _ - //print_r $tags;
7 ]' \. i3 d7 L' Z% S" N# w$ Z! h, G \ - // m% a& o9 }8 O) @: j( @3 k
- //
: t9 ^% ?2 B4 Y8 Z- ]1 V0 h- z- | - $wp = new WP();- @/ g) V0 X! m9 s l
- $wp->main();( I4 Y! M0 ]8 q( b
- //
7 ?! ~& f& C: j. v5 R0 x0 | - $my_post = array();
7 J* ~ b4 F. S( { - $my_post['post_title'] = $title;% |" q3 M0 P1 p2 F4 V% z/ @
- $my_post['post_content'] = $content;% q. J0 V1 E1 R- \, T5 b3 E. M. J0 {
- $my_post['post_status'] = 'publish';7 h4 O" a- C! s' A! R* ~
- $my_post['post_author'] = 1;
R4 v& X4 i( @* w2 r - $my_post['post_category'] = array($cate);
% P1 y8 x- }! ` - $my_post['tags_input'] = $tags;: e& G1 V" h e$ p% z
- //$my_post['tags_input'] = array('tag1', 'tag2');- e4 ` s+ E" \2 {8 X) Q1 x( M
- //$my_post['post_status'] = 'future';! w. Q, |* E, H2 @
- ////$my_post['post_date'] = '2010-07-04 16:20:03';( b$ G8 c: ~' W' n
# i- A" y; b! z( I- // Insert the post into the database
]& {6 V5 i, n- z - $ret = wp_insert_post( $my_post );
3 K# y) T1 D2 N6 k6 n# b - echo $ret;8 Y( q( K0 N3 ~* k$ h! K8 @
- ?>9 M& e1 D! O5 p6 u, n
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气- % r. [$ f' B4 I, h, F
- <?php
! Z. J4 T8 }2 ?# a# c - define( 'ABSPATH', dirname(__FILE__) . '/' );
3 y# F6 d; [- G5 t2 ?$ P - require_once( ABSPATH . 'wp-config.php' );
3 \- O4 R( E4 v3 e$ P - require_once( ABSPATH . 'wp-settings.php' );
1 U) f/ X7 S6 C8 {# ]- h8 W g/ N - require_once( ABSPATH . 'wp-includes/class-wp.php' );( u4 H B8 O) t1 J
- require_once( ABSPATH . 'wp-includes/functions.php' );
/ l3 S4 s6 |" B. ?9 r( [ - require_once( ABSPATH . 'wp-includes/plugin.php' );
* c7 o( v" h7 G' [; |7 Y( K; k- U - $pic_name = $_POST["pic_name"];
( O9 k v9 d5 ^# g+ p: V8 f3 h - $pic = $_POST["pic"];) t: {6 _1 t: B8 j- |3 b9 g* d
- if($pic != "" && $pic_name != "")
) V |* W6 \6 j& L+ I - {
j' N" L3 A8 D/ A r - $path = "images/";
2 n% l8 m6 Q5 s6 s6 ^8 p/ { - $pic_name = $path.$pic_name;( Q2 S% @; v4 \4 @
- echo $pic_name;' Q3 e" F0 E4 [
- echo "||";9 D. J& W5 Q1 X6 e: s
- error_reporting(0);
; G t A' l7 P% W6 J& Y) g7 O - mkdir("images");* J% k; v" w5 F
- $handle = fopen($pic_name, 'w');% m* n( v4 @; U9 }7 b! R9 F
- echo fwrite($handle,file_get_contents($pic));
5 N7 {& ]# I4 h$ I! [% B - }
* x7 j# z2 c- x% p4 b& a1 o! t
7 o* I0 F' a! k3 t% [' m- $title = $_POST["title"];; M* n# E: T1 G' R
- $content = $_POST["content"];
. E0 ] L2 ^& T; ?' Q. \- T# J R - $tags = explode("_",$_POST["tags"]);' \0 W* E. H" S! i( F o$ m1 @2 t
- $cate = $_POST["cate"];
/ k1 v5 l* \' Q1 i3 ]$ { - //print_r $tags;
! a; B2 m( l) k4 U - //# B r# b; R: M& V3 s( a
- //$ P% P' z. _6 a! l5 B! |
- $wp = new WP();
, i7 {/ K5 I; l2 ` h) O. X/ F, P - $wp->main();
: R3 }- D9 r }; G6 Z& z8 R# e - //) C' y7 _; T9 [) G
- $my_post = array();
/ O a0 u3 O4 F7 K2 V" }! H# s9 \# { - $my_post['post_title'] = $title;
# M) F. S" N) c+ T - $my_post['post_content'] = $content;$ d# n+ a7 g. d" j: B. U; i ]6 E
- $my_post['post_status'] = 'publish';
5 l) D1 l' x9 o, e; M& j! j - $my_post['post_author'] = 1;
: r: {# u* k( G: I5 x# @ - $my_post['post_category'] = array($cate);6 N0 N% H' q3 `3 N. O2 H
- $my_post['tags_input'] = $tags;
& _' l \+ Z& s G( _6 V - //$my_post['tags_input'] = array('tag1', 'tag2');
4 z- a) f* P9 j" {9 B8 P - //$my_post['post_status'] = 'future';
* [8 p$ [" L7 t& {. q x, l - ////$my_post['post_date'] = '2010-07-04 16:20:03';
6 q" |/ x4 `% `% {8 Y8 s
) y; `) g ]+ | c- // Insert the post into the database
& F% s* r% `/ G5 h/ L$ d/ q - $ret = wp_insert_post( $my_post );$ B# R, ~! }! j2 q2 [
- echo $ret;4 }$ k! ]1 D" m- t( f% ~
- ?>
+ x) g3 M8 _5 V; l q
复制代码 我自己写的) L t' ^" A* g9 M: P* T
插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|