|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑 * K* a5 k& r' N
5 J9 z) O4 c3 u* R
直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持
6 q4 {0 H1 `" B' Y J
+ a0 ~# K- j: Q1 `9 I我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了
+ i4 l) P F3 Q' c: G) C! B* X4 b8 ~% W: |0 ?! R3 O
方便实用 简单 何乐而不用呢
8 O% }/ G: P! ]& l+ f( _- <?php. {7 }, U2 ~4 {# R0 r1 A6 f
- % B4 y( e+ v) Z
- define( 'ABSPATH', dirname(__FILE__) . '/' );
( h& x4 q/ t' w4 j. { - require_once( ABSPATH . 'wp-config.php' );5 M- p7 g1 V% p+ V" Y
- require_once( ABSPATH . 'wp-settings.php' );! J ^! E: I, D% z {
- require_once( ABSPATH . 'wp-includes/class-wp.php' );& I, K x% B% J1 u4 h
- require_once( ABSPATH . 'wp-includes/functions.php' );9 S3 ^8 t1 I& w+ C4 i/ U
- require_once( ABSPATH . 'wp-includes/plugin.php' );
7 H7 @9 D9 U7 S9 b9 ? - $title = $_POST["title"];2 |- e5 c7 o% r% R9 U
- $content = $_POST["content"];& r7 }1 z8 b$ m4 t. ~
- $tags = explode("_",$_POST["tags"]);+ B" s" f: s4 c+ Q5 o# W
- $cate = $_POST["cate"];' }. v. O8 f: v+ Q5 W5 X
- //print_r $tags;0 B; @( ]- l: O+ A3 z, b/ ]
- //
) G. G2 }4 V3 t4 y$ ~/ T - //
4 m! G8 m( Y. |/ O - $wp = new WP();
7 B# B( G, w% `+ J - $wp->main();5 \; Z8 y! m4 S- S
- //
# l7 H1 z0 Z5 |( A" J - $my_post = array();
" w5 ` g: K1 j9 n7 P6 v% \4 I - $my_post['post_title'] = $title;& ^$ z$ T8 {% S
- $my_post['post_content'] = $content;" C. j4 A+ }; W( t) R' y! ?$ N8 a, |
- $my_post['post_status'] = 'publish';# y; @: Q0 w* X$ A5 o/ p
- $my_post['post_author'] = 1;% O' v6 g+ a: w6 c3 F+ C6 }: n6 i5 W
- $my_post['post_category'] = array($cate);$ q- K# D- r9 @3 d6 O
- $my_post['tags_input'] = $tags;. ` v9 t$ m5 r! w3 q
- //$my_post['tags_input'] = array('tag1', 'tag2');
6 h. `+ i! ] _ - //$my_post['post_status'] = 'future';
- `8 s8 N* K$ @. _( ` - ////$my_post['post_date'] = '2010-07-04 16:20:03';% _" f* I4 o; ~6 L) G6 r! P& m
: Y- Q! ?- I! S+ | V- // Insert the post into the database2 O* j* A' B9 z, ]6 h! T
- $ret = wp_insert_post( $my_post );
, t" s* f/ D* d8 ` - echo $ret;' Y6 K& u* C" N' Q
- ?>
# S/ z) ]2 L" j: h
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气
2 r1 ^' t' T$ z- <?php
; |; B! F6 ^9 E* o - define( 'ABSPATH', dirname(__FILE__) . '/' );$ E4 Z% W$ h, v1 w' ]
- require_once( ABSPATH . 'wp-config.php' );
$ _3 q: ?! F" w, c% d: ]- ^ m1 L8 m. W - require_once( ABSPATH . 'wp-settings.php' );
+ s. ^# a( V) r7 s - require_once( ABSPATH . 'wp-includes/class-wp.php' );
* Q! I: Z/ a ]0 o$ {. y* ?3 i/ b - require_once( ABSPATH . 'wp-includes/functions.php' );+ S& @. c3 s/ d" q1 R
- require_once( ABSPATH . 'wp-includes/plugin.php' );
! e) p" w5 ^" z* e - $pic_name = $_POST["pic_name"];
4 U4 D) t4 F0 J) W* @ - $pic = $_POST["pic"];+ K9 ]3 }* f, k+ h* Z, P
- if($pic != "" && $pic_name != "")9 T: n ?5 j _) D+ u4 t
- {. r; n; E) f' E
- $path = "images/";
5 R& O, m# e" Q4 l/ z - $pic_name = $path.$pic_name;
( X3 o( H1 F9 z, d& w - echo $pic_name;
/ @ q3 a5 J, D1 y8 a: M - echo "||";% [+ V/ n0 l1 n4 Z- `
- error_reporting(0);
+ `" z& H' e" |- r# m- \ b - mkdir("images");) r% d2 d4 {! v, b# @
- $handle = fopen($pic_name, 'w');: q, e% _3 ~$ Y7 [! ~( {) G
- echo fwrite($handle,file_get_contents($pic));7 n) v. _0 o- s& T# X! |
- }+ l/ y1 E0 z1 p9 A6 P8 V) k
- . Q+ e7 n4 t- |# M
- $title = $_POST["title"];
4 K% Q2 d: d. X5 o - $content = $_POST["content"];
- r' {: n# p% Q$ K- f0 G7 d - $tags = explode("_",$_POST["tags"]);3 m& H; h( [2 X Y- x( I
- $cate = $_POST["cate"];$ m% P n0 E5 z8 c
- //print_r $tags;
& N R0 q9 W* E6 y - //
+ ^% T4 x; q% T% b1 s) z/ b5 x - //3 m4 u# v+ S) i5 k4 N, [: X
- $wp = new WP();" Y6 Q- o9 {$ T! m
- $wp->main();' b# ]" }# z$ f8 h" n3 u
- //
A2 F1 p' n" |; u5 q! |* H - $my_post = array();5 W# c0 P; w' G
- $my_post['post_title'] = $title;% [) a+ o1 _8 `) U8 H
- $my_post['post_content'] = $content; t9 t' H. V! c$ ~5 P4 |- c. ]2 X
- $my_post['post_status'] = 'publish';
; b' `$ {3 O; r+ P - $my_post['post_author'] = 1;
& s( T E9 I, }( ]# L; i - $my_post['post_category'] = array($cate);0 k! p1 N( ]( u# F: t
- $my_post['tags_input'] = $tags;/ S9 Z( p( C5 L" a
- //$my_post['tags_input'] = array('tag1', 'tag2');# D# g9 o) G$ t0 V
- //$my_post['post_status'] = 'future';$ M ?) b7 d# l; b9 \
- ////$my_post['post_date'] = '2010-07-04 16:20:03';
- _% i+ F7 z' ~* ]- n- J& c
1 a+ j7 C. v+ M- ]- k+ @8 N- // Insert the post into the database
& o( e5 ? Z' d - $ret = wp_insert_post( $my_post );3 U3 c% ?. ]$ h
- echo $ret;
1 E2 v0 ~ R, T V0 B- e* D" v - ?>
! R) J+ n7 U3 b. ^8 Y
复制代码 我自己写的0 A, Q) |! t |1 c
插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|