|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑
) k/ e+ z! p6 O- ]2 D" r" ?. W# o, P9 C& e# ?- B3 W. q
直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持$ D5 C& Q% D4 O5 p" K! t) k
9 y$ s Q0 g$ h
我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了
7 R0 v% C, E" c6 R
8 J6 Q& L1 \7 s$ M0 l方便实用 简单 何乐而不用呢
2 {; ] f+ A8 w# D. K3 x; {- <?php
5 C" R* n. |! a6 F- G4 x
8 S, d C0 b' F3 T- define( 'ABSPATH', dirname(__FILE__) . '/' );
. W9 S w1 h8 ] - require_once( ABSPATH . 'wp-config.php' );
9 t! V: X0 g' G2 |2 P$ O# u - require_once( ABSPATH . 'wp-settings.php' );/ c+ \: L f- D- N
- require_once( ABSPATH . 'wp-includes/class-wp.php' );
" z: H, K: Q5 Z# z! E: U1 T - require_once( ABSPATH . 'wp-includes/functions.php' ); A( T4 x) n3 L0 U" S n7 S- [- I6 ^6 L
- require_once( ABSPATH . 'wp-includes/plugin.php' );
+ i9 K8 c' l' w% F% T - $title = $_POST["title"];- T' X- k R" O; U8 l% `/ P
- $content = $_POST["content"];
4 d% T; T8 n' e3 M, q - $tags = explode("_",$_POST["tags"]);
, N# S; i d3 o/ w1 A0 [9 [ - $cate = $_POST["cate"];1 N5 B! s( i3 W( \" p( F
- //print_r $tags;
) j L2 p1 O, [/ f - //
; d T L2 M' Z& N6 U0 P5 S - //
4 P6 J# N, g0 V - $wp = new WP();
. K# [, T6 _6 L- j1 T - $wp->main();
4 P' k* d; g$ Z. [( w2 M" D- s. \ - //
# Y4 ]" [1 v X! N" L - $my_post = array();
" \: \" S1 E( J - $my_post['post_title'] = $title;+ o9 Z) t; p. k+ ]: P0 J
- $my_post['post_content'] = $content;
! }- y9 f9 }- K - $my_post['post_status'] = 'publish';
5 {9 M1 D1 ~2 t5 D) d4 t- ~( [ - $my_post['post_author'] = 1;
1 ?1 {6 c8 A$ g- n/ c- D - $my_post['post_category'] = array($cate);
! O$ V& u" B) M. j9 | - $my_post['tags_input'] = $tags;
$ Y( f& _, e, E& L. W - //$my_post['tags_input'] = array('tag1', 'tag2');0 K$ S. ^+ {. I$ w$ B
- //$my_post['post_status'] = 'future';8 J+ F4 s# Y, u7 Z& R5 [7 \7 c2 I" e
- ////$my_post['post_date'] = '2010-07-04 16:20:03';
k1 H& l# n; O- E( b0 z( V
5 o5 G$ r; j9 u3 p6 G- // Insert the post into the database1 {2 Z% Y; Q! [" U" T
- $ret = wp_insert_post( $my_post );
% e2 S$ h! |1 T0 x5 m @! s - echo $ret;
7 t4 U1 ~( Z, h - ?>: Q! Z- ?) i x1 d
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气- % ~9 ]* w) U0 i; k2 M" b0 a
- <?php
! x2 p- e3 b: _. c - define( 'ABSPATH', dirname(__FILE__) . '/' );" \# @& I3 u2 Z# W- v
- require_once( ABSPATH . 'wp-config.php' );' C1 A/ K4 P: _ _
- require_once( ABSPATH . 'wp-settings.php' );
$ I- u- f7 d7 s% K- w1 F - require_once( ABSPATH . 'wp-includes/class-wp.php' );! r6 i8 ], B. }. c: J( `( ^' ~$ S
- require_once( ABSPATH . 'wp-includes/functions.php' );
0 t8 e% { g# r9 m - require_once( ABSPATH . 'wp-includes/plugin.php' );
" y& r7 r5 _* w - $pic_name = $_POST["pic_name"];1 P6 [! N* q+ m4 Y
- $pic = $_POST["pic"];
$ s+ u4 H$ N6 V( O1 b2 s) C9 e5 J - if($pic != "" && $pic_name != "")* r9 i( m* M: U& \
- {
) v! _% S+ {+ ~% H- o" c& B - $path = "images/";
4 A/ ~+ r0 b2 c% ^ - $pic_name = $path.$pic_name;
* L. Z* E% a Z% ]+ ~0 I) H - echo $pic_name;
0 O4 r9 f2 G2 T: W/ ], l3 y - echo "||";
* Z: s D j/ l9 O0 [ - error_reporting(0);/ L& B) Z% g0 R/ Z4 S# u) |* ^
- mkdir("images"); \2 J1 v9 C& T3 X8 M
- $handle = fopen($pic_name, 'w');
+ k- J, k6 S; e2 ?$ f) D - echo fwrite($handle,file_get_contents($pic));
' i* j. n2 j) m( i) K8 _ _% ~ - }
# I3 l7 u8 P( f; m% [1 P" {6 |
6 T6 P% Q T" o. v8 g- $title = $_POST["title"];
1 [* H8 r& ~7 g& J& ]2 t - $content = $_POST["content"];2 ~- S, _: ]# H/ E6 u
- $tags = explode("_",$_POST["tags"]);
- k9 P" x$ M y$ w! g+ g - $cate = $_POST["cate"];$ O) y3 O: l* Q
- //print_r $tags;* }7 }: U" w9 v& [
- //1 U; R! z8 J" N
- //
$ D! r$ d% J% p" F: E* ~3 g7 S: b - $wp = new WP();( b6 i+ o5 F3 {1 @) Z Z: V: P8 N9 F2 H
- $wp->main();
* T# Z. ?0 n n O$ y/ ~9 f - // m0 d. ]/ Y" J6 z1 X
- $my_post = array();
; W! B0 \; b( C z; ]% t - $my_post['post_title'] = $title;
7 [4 N/ p2 T- q - $my_post['post_content'] = $content;2 @; F5 w3 e6 O% m( F! Q9 k! I
- $my_post['post_status'] = 'publish';0 g! u) ?. V, }+ |2 \: S' [2 [
- $my_post['post_author'] = 1;5 Y( n" h/ T1 }4 _4 p3 q
- $my_post['post_category'] = array($cate);
" Y3 `, U* l* l3 t! ~2 T( z% E @ - $my_post['tags_input'] = $tags;' \* ]: m% l' v2 D7 `2 a: N
- //$my_post['tags_input'] = array('tag1', 'tag2');
, r( N5 D5 k# S9 P% |$ E - //$my_post['post_status'] = 'future';) u+ d- v& d3 n
- ////$my_post['post_date'] = '2010-07-04 16:20:03';( A! d y" i: ^3 A7 N' t
- ; I: l- ^8 a# r$ I8 `7 T. M
- // Insert the post into the database/ ^: M; X! \. f4 G& N
- $ret = wp_insert_post( $my_post );
* a( W1 z+ E' O/ y% {) f - echo $ret;, g+ j" l, t' I$ A2 w3 Y i: P, t
- ?>. ], b8 j! \, y+ Y) P
复制代码 我自己写的
. c9 v( n4 q) M+ q. e插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|