|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑
$ y% Q: T8 U* r z) ?. I! d4 r! W2 p" i0 x4 O
直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持
U! K+ t' O+ P" a
- W# F9 L- h. u0 l2 U8 o我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了
" a: T- o5 X, [: l2 E& B2 J. n
* H! @3 {9 D9 A* f& l方便实用 简单 何乐而不用呢- 1 W8 E( l' e0 S& J* H
- <?php. h/ D; g! H. P& p
- 9 T( x% y9 }$ A# f# F' M
- define( 'ABSPATH', dirname(__FILE__) . '/' );
' W! D4 p! K: {5 ?/ @& w - require_once( ABSPATH . 'wp-config.php' );5 Y' ~4 E2 d) Z" T
- require_once( ABSPATH . 'wp-settings.php' );
' B( H4 `* {) {' D# z - require_once( ABSPATH . 'wp-includes/class-wp.php' );
+ d+ I' D$ ~- i6 _8 ]9 O - require_once( ABSPATH . 'wp-includes/functions.php' );
6 M& {- b" Y* c* [ - require_once( ABSPATH . 'wp-includes/plugin.php' );
8 S3 M! ]- q* n3 z' M3 m e! [ - $title = $_POST["title"];, _8 T3 t' `$ J$ N9 W
- $content = $_POST["content"];
4 U: s; `. a; m% n" _ - $tags = explode("_",$_POST["tags"]);
0 s, c' |# J& c - $cate = $_POST["cate"];' P% I1 B+ [( |# a: u3 m
- //print_r $tags;) F3 O3 M- B: Y) o
- //, Z$ p1 k5 v) U W7 Y6 d) C
- //! z$ v- G& X) h s* a
- $wp = new WP();
! Z- `. n( F9 k! C/ ^% G1 R. k - $wp->main();6 P ?# V: l' \
- //4 E& e3 b5 v4 N; ?
- $my_post = array();
. Q6 h/ Q/ V! v% [' d9 Y" X0 A f: ^ - $my_post['post_title'] = $title;
$ @. D/ x- l& Y. K9 {" v - $my_post['post_content'] = $content;
6 l3 z5 a1 J9 L& v9 W" X - $my_post['post_status'] = 'publish';4 x3 S( b9 M# C
- $my_post['post_author'] = 1;
+ @* s2 u# s6 K' c - $my_post['post_category'] = array($cate);
* \) R% W0 k6 L5 Y+ Y - $my_post['tags_input'] = $tags;
4 d1 h" F1 S- K+ N1 \* e" j0 O9 m - //$my_post['tags_input'] = array('tag1', 'tag2'); z2 L2 w) B5 o) \( G
- //$my_post['post_status'] = 'future';3 u1 L' R; ?7 @. e
- ////$my_post['post_date'] = '2010-07-04 16:20:03';
( y, f/ _- {: p% y9 G
3 l8 x) a4 Q3 p$ {6 v1 R' ]- // Insert the post into the database
" }9 I0 j3 ]& s/ S& K - $ret = wp_insert_post( $my_post );1 K' z7 O8 b0 M) ^, t4 T
- echo $ret;
% q% p+ b8 E; |# ]/ B4 h8 E - ?>/ ~% A) M, O* Q$ A
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气
- S2 N8 T3 [2 c3 [- B0 U- <?php
" [( z+ m) ~. _& a - define( 'ABSPATH', dirname(__FILE__) . '/' );! ?+ }6 c; T8 u
- require_once( ABSPATH . 'wp-config.php' );: I( B, W2 T& o
- require_once( ABSPATH . 'wp-settings.php' );
, h4 @5 k, L0 B7 B" K$ v - require_once( ABSPATH . 'wp-includes/class-wp.php' );& l9 b$ P( K7 l' O1 W2 M
- require_once( ABSPATH . 'wp-includes/functions.php' );& v) X$ W3 J5 S
- require_once( ABSPATH . 'wp-includes/plugin.php' );% S# A% _0 F% ]
- $pic_name = $_POST["pic_name"];( `& _2 [+ ]3 i/ ^8 D4 O" J
- $pic = $_POST["pic"];$ k0 k6 \' F4 G
- if($pic != "" && $pic_name != "")6 z# W0 Z) [' ` j7 r5 M7 ?8 H
- {& m% M+ Q/ J4 Z2 T9 z
- $path = "images/";
* l! j' t$ ?1 k, J - $pic_name = $path.$pic_name;' y3 B' C3 F6 a K) }+ B" u
- echo $pic_name;9 S, z( b2 ~# ?, z/ c2 k+ y: D
- echo "||";7 ?% f- M j) k$ m
- error_reporting(0);7 `, p2 R+ b9 }4 \- C
- mkdir("images");8 \6 R% m) ?* s; D& J+ M
- $handle = fopen($pic_name, 'w');
: r) Z2 g( F# ? - echo fwrite($handle,file_get_contents($pic));& a- H' B3 P% z9 N
- }5 ~! K! I9 q5 K6 B" L
$ ?4 u1 {5 E0 O: Y! Z( v3 {7 i9 I- $title = $_POST["title"];
, n! A; V$ F7 Z* h6 y: x - $content = $_POST["content"];
" _; ~% |3 ]; c; h M9 D) C2 _ - $tags = explode("_",$_POST["tags"]);
0 ] N+ u& n$ n# X8 r# Z+ B - $cate = $_POST["cate"];) z8 h/ L. x/ h6 s
- //print_r $tags;
8 T, f) x9 }4 E - //
4 V4 [6 e+ v' g9 }6 l. a' c - //
' p( C* h. t( G& d) R/ v0 J6 y - $wp = new WP();
% x) W9 B, Z* I1 i2 i( O - $wp->main();
4 Z( a, M( m: [- |: [3 @ - //
" a. Y- y P9 ] - $my_post = array();. X( b$ g, _. Z% |. K
- $my_post['post_title'] = $title;4 k9 E. G3 k* T8 H
- $my_post['post_content'] = $content;
0 B. l/ C, w# x7 O - $my_post['post_status'] = 'publish';
" g: k& k( A/ B - $my_post['post_author'] = 1;6 Z2 E& {' b0 |9 [
- $my_post['post_category'] = array($cate);# V* [) D% t0 ~+ A
- $my_post['tags_input'] = $tags;* J/ u- L6 w% d5 ]$ c1 n
- //$my_post['tags_input'] = array('tag1', 'tag2');8 F# e6 a5 i5 g& b+ g! [/ h; y
- //$my_post['post_status'] = 'future';/ ~/ R# i: K. E3 F; a7 ~
- ////$my_post['post_date'] = '2010-07-04 16:20:03';
4 B- S- Q, t: t7 w6 j - + n# y0 S% C% X
- // Insert the post into the database; [1 `. j% K9 w* {6 J
- $ret = wp_insert_post( $my_post );$ K5 C* \4 v; ?* g* [: N! ]
- echo $ret;
6 @' h0 J- k0 n" h - ?>2 k0 P ^' `9 m1 E
复制代码 我自己写的
7 k7 n* b* s) P0 T6 N9 \插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|