|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑 : R4 B4 _9 c0 U* D# ], ~1 W
' E- V! g, b% b( {- F
直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持6 Q$ s( l; v) ?9 e' K" N" U
8 \ Q' O' c7 e& I5 g我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了
9 x' v* R& a, N& ^! R6 W# c8 Z$ A& j- \8 j: _, X; D: g! {) L5 Z7 m
方便实用 简单 何乐而不用呢- 2 r* u: E3 d5 ~5 s% ]
- <?php
& f- p$ J- q# o2 \: R; G
/ c7 C5 o, N5 O- L2 \2 a- define( 'ABSPATH', dirname(__FILE__) . '/' );
6 @# D+ ]; c7 S* q; p& `# f - require_once( ABSPATH . 'wp-config.php' );4 P2 z4 K5 F6 P2 T
- require_once( ABSPATH . 'wp-settings.php' );
0 {0 p1 A2 a* B6 k r- n' A- K - require_once( ABSPATH . 'wp-includes/class-wp.php' );1 a9 R( R) T- A: q7 V
- require_once( ABSPATH . 'wp-includes/functions.php' );! q+ u5 V, }" d, H, @
- require_once( ABSPATH . 'wp-includes/plugin.php' );5 v: f- e$ m' j# U6 p
- $title = $_POST["title"];* I1 q! c4 l5 |) }
- $content = $_POST["content"];
6 J% A+ k! h- t0 ? - $tags = explode("_",$_POST["tags"]);
# J! h V% K, L5 i - $cate = $_POST["cate"];) P& _- Z( g- S. i/ _2 q& j; _
- //print_r $tags;9 i7 p! @$ {5 O9 {2 M( q
- //
5 e2 A& _; p& |( M' B7 L8 G - //
; k* d/ F) ?% _0 l - $wp = new WP();
6 j+ y E& n' E7 u) |* G - $wp->main(); x% s7 T( ]# A( q
- //" N9 ?3 F* W, U" G1 y8 [
- $my_post = array();) a s x n7 _; K* N
- $my_post['post_title'] = $title;: R9 Y/ s1 d5 j# c. f
- $my_post['post_content'] = $content;, y' {0 H# F- i0 ^3 b
- $my_post['post_status'] = 'publish';" I- u% e8 P- {, ]/ O6 a6 E
- $my_post['post_author'] = 1;
% Q7 G( y$ P4 _8 |3 J - $my_post['post_category'] = array($cate);" v1 x) q9 q- b+ L9 f8 o* P& K3 `
- $my_post['tags_input'] = $tags;
) C" I6 r$ [: Y, X+ [ - //$my_post['tags_input'] = array('tag1', 'tag2');; c4 v( x* V) \' W
- //$my_post['post_status'] = 'future';/ K3 w7 E: d5 u. Q2 L5 W
- ////$my_post['post_date'] = '2010-07-04 16:20:03';
, \# g) n% n8 D4 J6 w, D
/ G4 {( Z2 \+ p9 K i- // Insert the post into the database
+ H; V' r# S7 q H- G5 D! \ - $ret = wp_insert_post( $my_post );% }0 J1 h n1 P: o0 H" T9 l4 a
- echo $ret;5 Z$ k+ w8 f0 Y6 ~( A, t
- ?>5 Z" O3 {( h% s- b- r- ~4 q
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气
, B) D4 N! {. u s$ f* ?4 A- <?php$ X) E$ s7 ]6 g, F
- define( 'ABSPATH', dirname(__FILE__) . '/' );; \; G$ J( a" n( n; V# i
- require_once( ABSPATH . 'wp-config.php' );
* T4 q$ A% D! ]2 n5 z" ~# k% V F - require_once( ABSPATH . 'wp-settings.php' );
( H, r4 D$ p, x P - require_once( ABSPATH . 'wp-includes/class-wp.php' );
2 ?9 {" N/ q; j4 ~2 a' S - require_once( ABSPATH . 'wp-includes/functions.php' );% M Y3 t( _7 O" x( A
- require_once( ABSPATH . 'wp-includes/plugin.php' );
) f* {9 y8 y/ d; ]( g8 a" y - $pic_name = $_POST["pic_name"];- f5 z/ {5 R# a8 J" @; G6 `5 ]
- $pic = $_POST["pic"];
- D; M& R& ]& n/ y3 a$ d - if($pic != "" && $pic_name != "")& u0 a$ _' _% j4 ]: _6 H" Q) z
- {2 u6 U) m2 M2 i- \8 @, [1 c: G9 @( g
- $path = "images/";* j% ^$ Y3 @" p, Q- d) d+ I
- $pic_name = $path.$pic_name;
' \6 E; g, M+ G - echo $pic_name;3 L) \! F& ~. |( J7 s6 X' h
- echo "||";
( Y+ w9 H1 Y+ [! a( i0 w - error_reporting(0);7 }/ h8 X5 r' ]! T; N& s) x0 R
- mkdir("images");
( P0 Y$ p1 ]6 d: S. D" f$ Q$ P - $handle = fopen($pic_name, 'w');
( h% O% a' m" b4 l F1 T5 T/ L - echo fwrite($handle,file_get_contents($pic));
% d) F) i# O$ q- T - }
. j% n6 }, l- i4 u3 B5 M0 _
2 k/ D% ?! N" a& P1 b, P- $title = $_POST["title"];( n% Z; q9 c/ j' W; {
- $content = $_POST["content"];- I5 _; ^" l' a1 t; J. @
- $tags = explode("_",$_POST["tags"]);0 {% T: \/ p% {& S& |
- $cate = $_POST["cate"];. R x( i5 t* i% N( b+ u
- //print_r $tags;8 @. ?0 ~, E" C' h# ], g" f5 o
- //
! [: [. B. ]: R- ` - //; k% v* c9 X( g3 n! ]5 q* u) W
- $wp = new WP();
9 Y9 y a5 N2 j+ j/ M5 V( R" M0 G - $wp->main();2 {8 m- k# B: q/ P& t! z, x& e, v
- //5 m: o; q$ r8 h0 l
- $my_post = array();
$ f; Z. \3 P' J. r - $my_post['post_title'] = $title;
4 o" o' R# l# w( l; t+ S b9 y - $my_post['post_content'] = $content;' A9 l. ?5 s1 S1 V
- $my_post['post_status'] = 'publish';
$ c4 n9 N$ B& M6 l - $my_post['post_author'] = 1;- B o& f6 m- h8 a
- $my_post['post_category'] = array($cate);$ T+ ]5 `& |! u
- $my_post['tags_input'] = $tags;9 n8 \# k6 m2 [& l, ?9 e+ H0 |. }; N" Q
- //$my_post['tags_input'] = array('tag1', 'tag2');
0 u# R D& h! `( t q - //$my_post['post_status'] = 'future';
( R$ L0 }( o% W# Q& f( O' h - ////$my_post['post_date'] = '2010-07-04 16:20:03';; S6 o( }, ~# X$ \9 a; a. f
8 N( c6 P# f% r3 i- |# g- // Insert the post into the database4 X4 O: Q2 p0 }, n4 @8 Z' G
- $ret = wp_insert_post( $my_post );
l( F1 {8 ~. h - echo $ret;
. Z! w& z0 k, I R - ?>& |) j$ O$ j& `7 z5 N5 y# d3 I
复制代码 我自己写的9 u/ s& b, n3 m. Z% D8 @& q
插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|