|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑 . a e7 S6 M' x& j" b1 @
7 M) K7 ~! s' h, {
直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持& K* e$ b) z) ]$ i1 n
5 s9 y* w2 S. u; U5 e
我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了
8 W$ b s, u- L+ \* T4 v$ K D5 j+ o$ N+ \/ j5 Z% x
方便实用 简单 何乐而不用呢- , L" |$ {8 m, M" B7 K$ L
- <?php
6 `0 ]8 h& ~$ m - & ~% J l5 Y" d
- define( 'ABSPATH', dirname(__FILE__) . '/' );
1 V/ \, G9 ?/ r; m$ p7 z* M - require_once( ABSPATH . 'wp-config.php' );
8 n( X" G9 q8 N* v8 V6 ]) Q - require_once( ABSPATH . 'wp-settings.php' );
3 V; c" L8 |; @2 u' c+ f - require_once( ABSPATH . 'wp-includes/class-wp.php' );, m- h$ d$ c: G. Q& T
- require_once( ABSPATH . 'wp-includes/functions.php' );
% l# P- x" i) y& _# i5 g) R - require_once( ABSPATH . 'wp-includes/plugin.php' );7 I1 i: Q! W A9 k; x, Y
- $title = $_POST["title"];! F: k- l; Q0 I/ M" M
- $content = $_POST["content"];
$ u. S7 _/ y9 s - $tags = explode("_",$_POST["tags"]);
% L+ q" y* I- K - $cate = $_POST["cate"];
0 Y: E2 j+ H0 Z - //print_r $tags;) p- e: I& v; B0 r
- //. S3 |9 ^$ |4 d1 A4 c5 f
- /// J; j: _; t" H8 R# d7 x O6 k
- $wp = new WP();
7 b: z- y% ~* f, R/ w1 m4 I! L! O - $wp->main();8 d5 q/ W& I* k3 a: A! R! b+ R
- //
, f2 f$ [# Y* @& s. d2 G; j - $my_post = array();" a4 x2 ^' ~1 g+ ~6 ?/ U3 y
- $my_post['post_title'] = $title;
0 X( V' o# s$ t+ Q' E - $my_post['post_content'] = $content;
- [% h5 J3 P; y+ N& i3 K+ Q/ o - $my_post['post_status'] = 'publish';, d8 K# S R: h/ e4 l0 \: B
- $my_post['post_author'] = 1;
( ]/ W4 z3 {# n& y& u, I - $my_post['post_category'] = array($cate);
" H+ Q8 [" @% [, H - $my_post['tags_input'] = $tags;! C) J# J$ I! ? a: ?& \
- //$my_post['tags_input'] = array('tag1', 'tag2');, x# E& T( N; U
- //$my_post['post_status'] = 'future';
( W" N1 z* o9 u9 B$ p - ////$my_post['post_date'] = '2010-07-04 16:20:03';( _- ]* W+ }8 E" K2 U
- : G( k' f, ~' k' _! r2 m' b
- // Insert the post into the database9 S3 A, l; o" M n: [
- $ret = wp_insert_post( $my_post );
, i/ F: ]& \0 [8 V8 ? - echo $ret;
* b/ u! f, L* a; E - ?>
/ m! ~: D% d' Y/ f2 O# Q+ ?
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气- 5 r. [- y( x' o: r# q
- <?php
# q5 {8 T# |' r; ] - define( 'ABSPATH', dirname(__FILE__) . '/' );
4 l3 z5 h }2 Z - require_once( ABSPATH . 'wp-config.php' );$ a; [2 _4 u2 L- D" l, p K/ g8 A
- require_once( ABSPATH . 'wp-settings.php' );1 v5 o; S- g& ~1 U/ D4 k; r
- require_once( ABSPATH . 'wp-includes/class-wp.php' );
0 ^& l, {9 J+ t2 X" Z1 w - require_once( ABSPATH . 'wp-includes/functions.php' );' ^1 o* P! R- L! p
- require_once( ABSPATH . 'wp-includes/plugin.php' );' p! V L+ n4 S# @; I' ^# _' ^3 @' t
- $pic_name = $_POST["pic_name"];. q- n. ^7 p6 q5 @
- $pic = $_POST["pic"];' v4 i' P3 c* \- M: r7 ?$ e, o
- if($pic != "" && $pic_name != "")
+ u0 f0 X9 ~0 [. T! C3 t) l* O9 b5 b3 [ - {
: M$ H5 z' u" o: W( s - $path = "images/";
/ Q5 i6 v' F$ ^- y/ `: B - $pic_name = $path.$pic_name;
# ]. n& k$ W3 F( P' p- f8 A/ q - echo $pic_name;+ `8 }. a0 s7 o% w5 |% j
- echo "||";
$ {' a* } z) n# Y$ {8 }- u5 [ - error_reporting(0);/ X0 F2 k* H, d+ v
- mkdir("images");4 a( W7 x9 A9 g2 \% V
- $handle = fopen($pic_name, 'w');
( y, @2 v8 O) T; c% x0 I - echo fwrite($handle,file_get_contents($pic));
: Q6 T% P0 _" v% f7 w - }
8 \0 J& _) ]2 j7 Y8 T# `
6 C% ^% N8 g! N, m* L+ C- $title = $_POST["title"];
6 b1 o/ a" i: p( | - $content = $_POST["content"];
2 F, |# U0 b# a% \( s - $tags = explode("_",$_POST["tags"]);
* Z* R" ^) ~% \$ v2 z3 K; h0 U - $cate = $_POST["cate"];
1 i- U& W9 T$ a; p: Z - //print_r $tags;7 @5 ?, X/ G9 {* a0 s: c. E
- //
; K- z: H' B( G+ N9 G- s0 b - //7 G$ o6 [" d) |, O: _. U
- $wp = new WP();
9 D2 D4 [8 B0 |1 G - $wp->main();
* G! G# h2 C" [, n - //
- H2 E0 v- x/ h# T, \ - $my_post = array();
+ n% ^2 {( C5 e0 l% p- f - $my_post['post_title'] = $title;9 r! S4 S. V4 N; A8 j5 i- Z
- $my_post['post_content'] = $content;
9 K) C( u! H( {9 M - $my_post['post_status'] = 'publish';3 y# z( V# t! m$ I/ B& g1 d
- $my_post['post_author'] = 1;& P2 M! _1 `$ w7 `# Z1 h, E
- $my_post['post_category'] = array($cate);8 q- \: \9 O# d! X* P
- $my_post['tags_input'] = $tags;2 t0 [3 T. B9 r# Z: ?/ o
- //$my_post['tags_input'] = array('tag1', 'tag2');
' F, F0 f$ x& n' @) y) u - //$my_post['post_status'] = 'future';( `" W! L! a: R4 [6 N
- ////$my_post['post_date'] = '2010-07-04 16:20:03';! C! X2 U( y+ Q: X
- ) x3 l! I/ S8 [1 j
- // Insert the post into the database2 V" G8 V4 i) a
- $ret = wp_insert_post( $my_post );
Q/ l( i7 D( }' D' C: a - echo $ret;
8 {- O. Z; T. I; d - ?> U9 T6 U; F3 @5 _
复制代码 我自己写的
3 `% y( F% x; l: O0 X插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|