|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑 * m" O' v. Q) Y1 M! C) A4 H( }
# }+ U8 B$ c7 `直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持% |. X2 P9 v% t- w0 J
: H& ~0 j* F# P/ a5 s! R/ A我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了
3 s$ h# @& B: S* U* m1 R& U% J
# e3 k# G) T+ G5 m0 {; v) A& S2 J方便实用 简单 何乐而不用呢
5 k* i! Q2 a$ I! l" O: Y- <?php
) W$ ?# l: y; R/ z1 ~5 J" l; b6 B
0 }6 c6 c6 O) e0 z- define( 'ABSPATH', dirname(__FILE__) . '/' );
0 R. v6 o9 {( B9 k - require_once( ABSPATH . 'wp-config.php' );
2 e( r) z3 V# y ^% G9 B" w - require_once( ABSPATH . 'wp-settings.php' );
; A, N1 \# E7 C - require_once( ABSPATH . 'wp-includes/class-wp.php' );6 |" `) X# N! I1 F; X
- require_once( ABSPATH . 'wp-includes/functions.php' );
( D% P/ P/ r/ \ - require_once( ABSPATH . 'wp-includes/plugin.php' );! v' }' b, F" C" _6 s/ W
- $title = $_POST["title"];
" F1 F( Q2 ]3 A) E, ` - $content = $_POST["content"];
y4 c4 m$ Q3 | \* h* [" p* G - $tags = explode("_",$_POST["tags"]);
6 t2 e- X! M9 [; e# y8 J - $cate = $_POST["cate"];
4 C2 l M2 P6 L8 c, b3 q - //print_r $tags;
" H: ] m( `) ]3 _6 ] - //. z6 c0 F* l' w" ]- z) q- \% K
- //
7 [0 T3 e( h' t5 o* j - $wp = new WP();1 P6 h0 I% X% K$ `+ X. p
- $wp->main();
, E: q5 s( k8 S+ Y4 M# J - //
! e U4 I4 F) m0 Y - $my_post = array();
" {5 P$ @3 f+ L - $my_post['post_title'] = $title;
, ^5 j$ i1 k! A" R; [* O: k, ^ - $my_post['post_content'] = $content;/ Y. z3 h* I* S1 Y# ], L* V
- $my_post['post_status'] = 'publish';
7 B( y6 r8 Q. q$ i - $my_post['post_author'] = 1;% H' T. {/ w% k+ K+ |4 U- C
- $my_post['post_category'] = array($cate);$ O9 a* Y2 R% R' b: B
- $my_post['tags_input'] = $tags;& r) G+ j" D: m9 h' ~3 I
- //$my_post['tags_input'] = array('tag1', 'tag2');
& o2 @% j- Q* I8 ?# D - //$my_post['post_status'] = 'future';. R5 a. |$ K- D* P% m
- ////$my_post['post_date'] = '2010-07-04 16:20:03';- x: D' J% Y3 B/ A# z
- % m( L8 {% H+ n9 S: l6 N
- // Insert the post into the database
5 M: y, K) G; ^; g - $ret = wp_insert_post( $my_post );" r( ?$ [/ q7 x* A' n/ K- l; s
- echo $ret;/ r7 w+ f' [& F, ^/ k+ H
- ?>2 W* ^7 K8 a/ d2 i4 o- S
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气- ' p8 f+ l1 h: ?4 y( H, b
- <?php$ b i! G7 J1 @
- define( 'ABSPATH', dirname(__FILE__) . '/' );# T4 `1 b0 J# R1 P8 b
- require_once( ABSPATH . 'wp-config.php' );3 B0 m7 G2 k0 \' k
- require_once( ABSPATH . 'wp-settings.php' );. R# K2 Y: k9 b0 W+ M0 p! `6 n
- require_once( ABSPATH . 'wp-includes/class-wp.php' );( W0 ?/ A% u4 b/ a
- require_once( ABSPATH . 'wp-includes/functions.php' );
5 X/ Y x' W/ I) x& z - require_once( ABSPATH . 'wp-includes/plugin.php' );
1 X( m# n5 R: G' w* ` - $pic_name = $_POST["pic_name"];% w6 X+ ^- j' {. e- `% N
- $pic = $_POST["pic"];
* g. c/ w. o# z% [ - if($pic != "" && $pic_name != "")
$ f: V& S# c& a+ h. x( h - {
7 S a$ d4 q" E" ?6 k - $path = "images/";
0 V. o6 S8 v. p - $pic_name = $path.$pic_name; h6 L" ~ C; T" h6 {9 V$ {/ K9 p
- echo $pic_name;
5 [! D D1 T* b' ]5 T% w4 K - echo "||";
+ @6 }3 `) y0 X% N$ D( e5 q - error_reporting(0);
, W5 ]( d @2 r. W - mkdir("images");& G" v Y+ b, Z; K1 K
- $handle = fopen($pic_name, 'w');2 h% N2 C. g6 k0 s/ b
- echo fwrite($handle,file_get_contents($pic));& z: O4 [* j3 W
- }
+ H' p7 m3 @8 b2 Y$ G* k - 6 o- z" x6 e Z+ M& S" p2 z
- $title = $_POST["title"];
% o+ Q3 `3 H$ p: \/ Q - $content = $_POST["content"];
* B+ u2 Y" {8 L. z- [( L' V - $tags = explode("_",$_POST["tags"]);
- h9 B6 k6 p4 a' K, \' | - $cate = $_POST["cate"];
+ j) j) H. R* i" p) ^ - //print_r $tags;
- R9 g* [" B7 @% |0 Z- d1 O) ~ - //
. z! [$ ]% s" {3 [5 e7 s - //
0 g! r! m3 ]! a: a# ]0 m - $wp = new WP();
# z: a3 y9 X. q/ O$ x - $wp->main();% P) ^7 s/ D* o: o* U) z7 B0 N
- //
. B2 y0 B" V% w - $my_post = array();: m1 _; V }' k
- $my_post['post_title'] = $title;
# R) I6 B' }+ e - $my_post['post_content'] = $content;
( G& l. Q: M0 K0 x9 ^2 A - $my_post['post_status'] = 'publish';9 a& H* g( V" T
- $my_post['post_author'] = 1;
8 l9 Z( j% {- l8 B/ ^: ?: @3 ` - $my_post['post_category'] = array($cate);7 g) \* e2 { ?" p
- $my_post['tags_input'] = $tags;" ?$ s) s0 c" c. p9 c# T" F
- //$my_post['tags_input'] = array('tag1', 'tag2');
* @6 Z; i% d$ [7 B. a, k7 D - //$my_post['post_status'] = 'future';
1 z/ e( O3 M1 e7 _ - ////$my_post['post_date'] = '2010-07-04 16:20:03';, c" K. r2 Y( J J9 a/ @
- , q8 P5 Y" |* t& M3 O( E
- // Insert the post into the database" C7 C: ^# W( d [: @% Q0 c8 B2 |
- $ret = wp_insert_post( $my_post );
, i% a) t/ K' Z - echo $ret;
" `6 e7 ^9 S, @5 j - ?>4 m5 Q6 }3 i) Z3 s
复制代码 我自己写的
B" r* F+ |& A+ b! T. i2 ?插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|