|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑
& y, l4 y$ t5 G8 j. H, P/ U9 j( H* X2 K3 l
直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持
E: i+ J* x2 U* u( m+ |( D8 n* _4 o8 N$ p9 d1 I
我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了
9 b) L" S) n: y" M- R# z; X, o/ Z4 h% u( C2 S5 U) o3 j. z
方便实用 简单 何乐而不用呢- - r; f) r8 [2 o3 P
- <?php
0 c, t% _3 ^# i1 `, d+ T
; I3 g8 s" D4 B; V; |: i- define( 'ABSPATH', dirname(__FILE__) . '/' );2 h" ?7 N% O" y
- require_once( ABSPATH . 'wp-config.php' );
, m1 @+ U6 W2 L9 k) [4 o - require_once( ABSPATH . 'wp-settings.php' );
6 y3 W1 C/ j$ T- z( r K6 s - require_once( ABSPATH . 'wp-includes/class-wp.php' );
* c* T/ {" {5 w - require_once( ABSPATH . 'wp-includes/functions.php' );+ Z0 ` E, E0 F( ^2 ?" v
- require_once( ABSPATH . 'wp-includes/plugin.php' );
' [% i3 Q- R3 X6 u) c - $title = $_POST["title"];2 P% t/ k# }2 P* R9 e! c
- $content = $_POST["content"];
. `% j2 T8 v# g/ H - $tags = explode("_",$_POST["tags"]);
6 Y g, V! D7 G. E) g - $cate = $_POST["cate"];2 k0 C7 J6 W, o# ^9 j+ k$ M9 M
- //print_r $tags;2 r& A, q. R: |! \
- //
3 j$ g) W7 {0 [$ ?: C1 f - //
2 @, _& P/ k. P1 {, \ - $wp = new WP();
D# t4 d& Z; ^& i/ {, h - $wp->main();( ~: f9 _. ?, ~( \, e
- //$ n" R3 j8 x3 K
- $my_post = array();1 y8 `, T8 k, K" J
- $my_post['post_title'] = $title;
2 M( _' e* T; @6 N$ S, ?: q - $my_post['post_content'] = $content;
& p- u1 l# _; ~& V - $my_post['post_status'] = 'publish';
8 `# r8 r) I) [8 g- S+ J2 _ - $my_post['post_author'] = 1;& ]5 U% h, U/ K! g) e9 |* B
- $my_post['post_category'] = array($cate);6 `& O2 S6 A2 T: {, Y: @
- $my_post['tags_input'] = $tags;' T# n' _# ?. \7 Q
- //$my_post['tags_input'] = array('tag1', 'tag2');) Z& m2 k. T0 b. {6 F" P
- //$my_post['post_status'] = 'future';' S6 m; T: b# I0 @
- ////$my_post['post_date'] = '2010-07-04 16:20:03';: B# m, h+ i% _3 y% I: ^
; Y8 ~2 S6 L( i" N6 X: \5 l8 y6 k- // Insert the post into the database
o) X& }6 e+ a8 e - $ret = wp_insert_post( $my_post );: x% l, I: X/ X) k1 z
- echo $ret;
; V* u, A5 h5 h. U% ^. x5 o - ?>0 W8 m) A( Q, B; H
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气- + X* {5 z/ J/ |3 B8 @
- <?php1 P. ]! c( u o+ B; D- j
- define( 'ABSPATH', dirname(__FILE__) . '/' );2 b P7 e3 r; H
- require_once( ABSPATH . 'wp-config.php' );) g- Z# ^6 D* ]$ h, q' s+ ]4 I. f
- require_once( ABSPATH . 'wp-settings.php' );
# p! ]0 z, ~3 K6 D6 h0 L+ Z# w - require_once( ABSPATH . 'wp-includes/class-wp.php' );
. s9 @, V# U& Y6 Y - require_once( ABSPATH . 'wp-includes/functions.php' );
7 w5 S k) H4 Q - require_once( ABSPATH . 'wp-includes/plugin.php' );
L$ j6 r, d5 Z7 [ - $pic_name = $_POST["pic_name"];% H" L7 e. Z2 O1 I/ q: k* f
- $pic = $_POST["pic"];' A! L1 N! q6 V5 f: k9 Z# I
- if($pic != "" && $pic_name != "")
: B# m. i1 p" F6 J! B1 z5 E3 e& o - {$ r0 z9 S; L( _" t: Z! T
- $path = "images/";
9 n0 `. ^* b7 e9 j: w- n7 \, D - $pic_name = $path.$pic_name;) q4 b D2 ^. ?* |
- echo $pic_name;
3 c$ k3 G4 m* }. @$ c - echo "||";
: F' ^$ p2 M$ \" C; Y |: ` - error_reporting(0);, B! g! f% I4 u' ?
- mkdir("images");
; B& x" |% f/ c4 ~9 r - $handle = fopen($pic_name, 'w');9 R5 ^& ~! P" P3 Y
- echo fwrite($handle,file_get_contents($pic));% v5 I- R+ e" x, a e( g
- }, v% {# G b% V/ i% w z q) Q" _
- # @9 {5 v$ F8 V- |* a0 K2 x
- $title = $_POST["title"];
0 }% c [! O' V" t b5 w, P - $content = $_POST["content"];5 m" j" B9 M* [6 B f; D
- $tags = explode("_",$_POST["tags"]);
G, {3 e: S' F: _% W* e% o) G - $cate = $_POST["cate"];
" h1 N0 d H6 Q0 W - //print_r $tags;2 U; S# w: j* ~5 T/ n* v% O7 }
- //
) D2 F8 Z6 T0 `: q4 ] - /// A) Z/ L3 M. s
- $wp = new WP();6 N+ V* V% l+ O; h
- $wp->main();
& k5 L4 b, J/ M1 n8 v4 ^ - //# ^1 [0 I/ A2 r% c9 {" G
- $my_post = array();
1 @ t& `& c8 q) h- r3 |! G - $my_post['post_title'] = $title;' b# N$ A5 C) {* g# I( _6 v
- $my_post['post_content'] = $content;5 C% D9 S) D9 o/ t( f1 N' @
- $my_post['post_status'] = 'publish';- d9 O! l0 V! B1 C( L) p. y. ~
- $my_post['post_author'] = 1;
6 v2 g; x+ T$ v. p, z. ~ - $my_post['post_category'] = array($cate);2 h3 k% p+ p( H/ S
- $my_post['tags_input'] = $tags;
/ [6 f% R0 R9 p* O5 F+ I q( o6 g - //$my_post['tags_input'] = array('tag1', 'tag2');2 Z( y8 X X% `$ i& u. x7 n
- //$my_post['post_status'] = 'future';* v! R5 h+ `0 H8 q2 Z5 j/ ^$ ?
- ////$my_post['post_date'] = '2010-07-04 16:20:03';5 ~& u) J( f8 X9 j
5 ~6 d$ V; o" W3 ^- // Insert the post into the database
- v2 P" a! w1 h- c4 T! f! d2 ` - $ret = wp_insert_post( $my_post );/ G, P. A( g: V6 w1 `
- echo $ret;
4 I6 }# P" ~. p$ D( ]2 r0 T5 I5 N - ?>
3 }/ r0 X I% E- @8 ]
复制代码 我自己写的
8 i, a: g! ]- K7 V" ?1 S7 g插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|