|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑
3 K! F- N# n, f9 R- W) P$ M
5 L2 q4 J7 y2 J2 Y9 b0 q直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持. s% n/ L. b* W# Q8 q
C0 ~& V, }2 n% n0 G我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了
1 L. h" V7 ]* Y5 H2 j( Q/ i! u1 J+ p9 {
方便实用 简单 何乐而不用呢
6 f3 Y- ]( z* f; H6 W# P- <?php, r {$ ]* O$ _1 E6 L ~+ k
- 3 O! _! \, X# E$ |# U* S7 \; [
- define( 'ABSPATH', dirname(__FILE__) . '/' );
# U3 x+ n8 Y2 U/ ^4 F; A; h0 D' \ - require_once( ABSPATH . 'wp-config.php' ); A: x7 H0 c$ c) f% u
- require_once( ABSPATH . 'wp-settings.php' );3 W% `6 S' ]- L1 o' h" o9 ]$ ~3 k
- require_once( ABSPATH . 'wp-includes/class-wp.php' );% \: x' Y" ]/ T# B; L
- require_once( ABSPATH . 'wp-includes/functions.php' );
! L+ f4 m+ J C O - require_once( ABSPATH . 'wp-includes/plugin.php' );9 V! K% z: D2 i1 F. u: E
- $title = $_POST["title"];
9 v- c4 u" V# E& P5 _; [ - $content = $_POST["content"];$ L9 p; q, g: p+ `1 ~
- $tags = explode("_",$_POST["tags"]);$ t2 M/ ^2 Z$ @& W$ ?5 [3 j8 f
- $cate = $_POST["cate"];0 }- X: Y. X! e; }
- //print_r $tags;
; d& u- H- z- h, q - //
- p' Q* R# ~4 D: `7 S - //
0 V1 z6 D6 K" N- H: {* ^! X - $wp = new WP();2 n) a& ^# k' q- B& c, U
- $wp->main();
- y- p, r* \% _1 s! } - //
: Z0 ~& h6 [; A7 ?2 @! l( f# u! u - $my_post = array();
3 X( n* V1 W) k' T+ {( k2 L6 G - $my_post['post_title'] = $title;9 K1 u5 ~1 s/ u0 _% ]( Z% |
- $my_post['post_content'] = $content;& m, d3 v1 ]4 u$ ^
- $my_post['post_status'] = 'publish';
5 m6 }1 V9 P! Q- F3 k8 O8 z - $my_post['post_author'] = 1;- [ f3 l/ V# X) V2 V. r
- $my_post['post_category'] = array($cate);
3 F" y d3 q0 v) O - $my_post['tags_input'] = $tags;6 B7 Y' r, g! c, p5 K* e2 ]- M
- //$my_post['tags_input'] = array('tag1', 'tag2');
6 b& ^4 I; A' d4 V) K - //$my_post['post_status'] = 'future';: E" Q( [! S" V, J
- ////$my_post['post_date'] = '2010-07-04 16:20:03';1 ^. y1 a. T1 [4 J( q( ^
, `. S4 J/ R4 |; y4 ]4 m- M- // Insert the post into the database
; |. ?5 T1 \3 l5 z* D. i1 M* Y - $ret = wp_insert_post( $my_post ); ]# W- N- w8 ?5 w6 b
- echo $ret;9 a0 I/ ]* A/ `9 f0 V! c# o
- ?>
' C; H! I. Z$ m- d7 M3 x
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气- 4 S5 E9 ?1 W/ X* a6 R1 u) a4 [, A
- <?php
1 n5 }5 B( q; i- A, O: o - define( 'ABSPATH', dirname(__FILE__) . '/' );
; C. g6 F3 ]! s$ y" y& d - require_once( ABSPATH . 'wp-config.php' );
9 T, p9 R/ F1 |7 L0 f - require_once( ABSPATH . 'wp-settings.php' );. w' |2 w# r* h* S
- require_once( ABSPATH . 'wp-includes/class-wp.php' );$ [ T4 l3 y7 H7 A
- require_once( ABSPATH . 'wp-includes/functions.php' );
' e* j# u. f7 ?+ P - require_once( ABSPATH . 'wp-includes/plugin.php' );/ A3 r2 A$ q9 |- i$ V
- $pic_name = $_POST["pic_name"];
0 H7 q W. k0 k; x; s3 K - $pic = $_POST["pic"];5 v; O4 x! ]& c4 w# s. W' C0 k
- if($pic != "" && $pic_name != "")
7 ]. j6 d/ t, {, s8 t: [ - {
8 H7 \5 k1 L* l' P - $path = "images/";
9 N( H" p2 u' { - $pic_name = $path.$pic_name;
( n' Q( d( L c# Z$ ~ - echo $pic_name; K7 z' n/ h6 Y# _" }
- echo "||";
. t, _: x1 H. w+ j) x1 |2 d. S5 _1 \2 T - error_reporting(0);
- F; f O p8 |9 s1 \" B - mkdir("images");
5 m- i1 I i+ ` D: z8 N- t0 t; I) } - $handle = fopen($pic_name, 'w');
7 A7 z& k+ O' z. O - echo fwrite($handle,file_get_contents($pic));" l5 z; a" l5 l) s& H: ~
- }$ Q% Y5 l, h) X. v
- $ c9 Y: Z+ i8 G- g
- $title = $_POST["title"];% a( B8 }; V |( V Y& q% N
- $content = $_POST["content"];! e- R. h4 d* [3 A0 |6 Z4 [
- $tags = explode("_",$_POST["tags"]);9 F( S8 m" V1 Q. ~: ^
- $cate = $_POST["cate"];! @7 I( l, J: D+ O2 m
- //print_r $tags;# \' E& e5 Z: s7 k/ n6 w
- //$ a) f d6 w- T& [5 p
- //
' q* y0 T F4 j3 y" E7 r - $wp = new WP();9 u8 F- K$ t# E/ E
- $wp->main();
4 V/ s5 J6 i& F( k- e: H7 k - //
$ G# d. n2 x; e ~5 ^0 k8 g2 e( m - $my_post = array();
- F+ a+ H0 y4 W! O# e# ?/ K - $my_post['post_title'] = $title;
# Z/ T1 ]) x; U- {# T - $my_post['post_content'] = $content;
8 L( t7 W. c8 y: E% l; ^. [ - $my_post['post_status'] = 'publish';
- j5 E# M! K" R. N& m% O - $my_post['post_author'] = 1;
5 C8 ^" l" S Q Z* @ - $my_post['post_category'] = array($cate);- J; S4 C3 H; m8 f# s
- $my_post['tags_input'] = $tags;
: }" x0 K4 R; }* w* l- M6 p - //$my_post['tags_input'] = array('tag1', 'tag2');
. Z+ J/ G9 \' c$ [ - //$my_post['post_status'] = 'future';
) u3 F" _& W: {! E& Z - ////$my_post['post_date'] = '2010-07-04 16:20:03';
# q' b& R# r5 z- W
6 q# }1 A/ J- ~' E% R; P- K- // Insert the post into the database
' Z) b6 [5 I. Z - $ret = wp_insert_post( $my_post );
0 T9 |" w3 ~. b5 h1 x" o6 }) U, r - echo $ret;
' y8 |1 A% s& J7 v - ?>2 U5 c3 W( k( [/ q' B* h4 C2 k
复制代码 我自己写的) t5 V; \" \1 t4 }
插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|