|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑
9 x2 }; c2 C# Z( a' o. q+ s& Y# w+ G- ~
直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持; z" H: {! [6 v/ {
4 ]" R1 j) f+ H$ _, ?我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了
$ v& y* D; l3 S
7 o& O: A* E& G$ ^: u( _* k方便实用 简单 何乐而不用呢- ; _$ D! C4 k3 I) j9 Z! Y/ A
- <?php
& p# h1 ]3 Y6 Y9 D* b - 4 c# f, d) D+ t9 d5 I* [2 h& X
- define( 'ABSPATH', dirname(__FILE__) . '/' );/ R8 X& V( g5 _8 o, B( W+ b$ z5 z
- require_once( ABSPATH . 'wp-config.php' );
9 p5 v# ~' Z% n: N; R. [& K- f/ W - require_once( ABSPATH . 'wp-settings.php' );
o- h1 X& ~( I2 o# \% U. i: a2 F5 D - require_once( ABSPATH . 'wp-includes/class-wp.php' );9 M8 S; T9 }1 Y- A2 V& d7 j; s; {: Q
- require_once( ABSPATH . 'wp-includes/functions.php' );6 P/ k9 |2 K, x
- require_once( ABSPATH . 'wp-includes/plugin.php' );
$ L; ^3 y- ?+ N& n1 k; M9 g% x3 i - $title = $_POST["title"];* O# v @: q g# I% `
- $content = $_POST["content"];
`3 g% q% V& ]! c D" R. a8 ~ - $tags = explode("_",$_POST["tags"]);
- ]; c, S; e' y# T6 }4 j/ { - $cate = $_POST["cate"];- l) I2 g) z4 `. R7 q/ g
- //print_r $tags;
/ k, e: \4 g/ l* Q$ ^ - //
1 p! V+ r8 r [, }( O - //
% l4 V: r) B/ t& y8 E& X* Y - $wp = new WP();
- V7 v: ]% j# R5 v8 g4 t# A8 f - $wp->main();
/ y6 d8 n! P$ g - //& [3 [3 r" P& {) m
- $my_post = array();
$ F6 @7 k" z: d - $my_post['post_title'] = $title;0 P0 z/ g+ s [ S0 \& d7 J4 ^$ x0 w
- $my_post['post_content'] = $content;; s& R {# q# a" }: z2 C
- $my_post['post_status'] = 'publish';
% z' T; b# C, A# F6 }, Z( O - $my_post['post_author'] = 1;
7 s0 P! ]: k4 |. W9 @1 O - $my_post['post_category'] = array($cate);
# P( B# {+ k" @/ y2 O6 r& Q/ f, q0 |2 y - $my_post['tags_input'] = $tags;
: c" X- S3 N/ h2 k, Y - //$my_post['tags_input'] = array('tag1', 'tag2');
3 y) y2 C* L) }7 I8 r) ]6 S6 M7 R - //$my_post['post_status'] = 'future';9 p I) h- u1 v+ y4 r
- ////$my_post['post_date'] = '2010-07-04 16:20:03';: \1 C3 E9 x* _" b1 C
- * x5 |6 T) u. Z5 {) r. A3 w
- // Insert the post into the database
+ [# {) {. g' Y - $ret = wp_insert_post( $my_post );
# ]# ?8 f! x0 c( u# ^ - echo $ret;
: b2 {* G: V2 T1 T - ?>9 N+ o' v) ]* u6 ?3 J
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气- . o8 C* U/ e1 v8 m
- <?php
9 l) m+ X4 R$ J1 ^ - define( 'ABSPATH', dirname(__FILE__) . '/' );
6 r1 N+ A. v3 @ - require_once( ABSPATH . 'wp-config.php' );
' J* i& ~8 _) j" j0 t* B b+ a% ^ - require_once( ABSPATH . 'wp-settings.php' );4 j" J5 d9 R; X0 \- b& q/ F9 Y
- require_once( ABSPATH . 'wp-includes/class-wp.php' );6 u& L( s* r. ~2 v! r# F. E2 h
- require_once( ABSPATH . 'wp-includes/functions.php' );
8 ?8 P! J. V9 ] - require_once( ABSPATH . 'wp-includes/plugin.php' );/ B+ f7 l- d! D. w5 w4 @/ q
- $pic_name = $_POST["pic_name"];3 q! O$ `' a4 u/ x9 [
- $pic = $_POST["pic"];0 o) m0 d, h2 P6 E% N! W3 Z0 X
- if($pic != "" && $pic_name != "")
7 W- F/ a; r; Z* M0 G - {" X2 x9 f, A4 O' q
- $path = "images/";
4 ^) g( J" f7 p6 Z9 h) I3 {: Y! h - $pic_name = $path.$pic_name;) v( c% p$ L* b& }/ v5 f2 k
- echo $pic_name;
: S, Y# t. o' X4 i2 y! C! Y - echo "||";
! X) V7 [+ [ x, _ - error_reporting(0);
; i! M! ]! e8 h* ^ - mkdir("images");8 z2 e# o4 \$ G0 W/ p
- $handle = fopen($pic_name, 'w');, u1 h1 K! Y! Q6 ]
- echo fwrite($handle,file_get_contents($pic));
$ F& R6 z. |( E; [ - }6 X! B( k1 v l7 C) S
- 1 p- p6 n; g! j1 W$ h! C
- $title = $_POST["title"];/ y6 y) Y; J- d- Z
- $content = $_POST["content"];0 N! B X7 l: q9 @ X
- $tags = explode("_",$_POST["tags"]);
1 F3 T3 y0 a# F- `$ G! Y$ z. r - $cate = $_POST["cate"];# W* |* _- L, I; N2 W
- //print_r $tags;$ ]' u6 u5 t. [ O4 W
- //2 |$ Y& i f( ?( ]
- //' ?. {6 F6 ^2 H" h
- $wp = new WP();9 R& d# [, W3 ]' V- |. i
- $wp->main();
( C$ |2 _% P/ Q/ l - //
) y/ h3 u1 I+ }. m0 c - $my_post = array();
: c6 R+ }3 N" v9 s - $my_post['post_title'] = $title;
E) o3 b% g- X2 Q" W7 j7 v - $my_post['post_content'] = $content;
& q R' E1 [! A7 q) q. O - $my_post['post_status'] = 'publish';& I) s2 ?. q" e! g. |6 l
- $my_post['post_author'] = 1;9 @3 [: @. U9 H6 A3 s( t$ ?
- $my_post['post_category'] = array($cate);
; U4 }3 y6 G2 G4 s) E2 ~ - $my_post['tags_input'] = $tags;0 K- k) D9 K# F/ e' j
- //$my_post['tags_input'] = array('tag1', 'tag2');) p5 O4 w( h- r7 X. H# z5 x3 ~
- //$my_post['post_status'] = 'future';
5 s$ a' ], ~/ o8 K - ////$my_post['post_date'] = '2010-07-04 16:20:03';
/ e9 g0 {* F/ |+ Y - 3 C0 n! K- E; X y
- // Insert the post into the database
& X* @5 Z/ W: B1 s% R, _' c# @ - $ret = wp_insert_post( $my_post );
# {1 G4 h+ k* F7 R7 n- e - echo $ret;+ _" r) N. c: [( ]6 `9 L. i
- ?>/ U- Y1 x @7 | E# _, p" g
复制代码 我自己写的' Q& G( }6 B9 N8 G* h, o# {
插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|