|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑
- j: ?& @# @ M1 a/ G) N
' B1 D' L4 x; v7 { {4 `直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持
* b) T6 U( M% X) R+ }" ?" E4 B9 |5 l4 e6 }
我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了9 N+ J7 Q/ W" Y! j, N4 t, f# e0 D* V
1 E9 V/ q7 e M2 i: f0 a方便实用 简单 何乐而不用呢
+ G( k4 L1 }4 @6 V3 n- <?php
# |8 r- r$ N3 ^4 X
4 ~; u4 e& m, R# V1 q- define( 'ABSPATH', dirname(__FILE__) . '/' );
2 l8 F7 Y, A" i! m9 x - require_once( ABSPATH . 'wp-config.php' );6 \3 ~" z: j, a ]2 p9 T
- require_once( ABSPATH . 'wp-settings.php' );/ M1 g- U, C7 ^. n/ W! w
- require_once( ABSPATH . 'wp-includes/class-wp.php' );
8 ?" G* X/ A0 z, J+ ] Y; m - require_once( ABSPATH . 'wp-includes/functions.php' );
( g8 x" U* Y/ B# n2 ]6 M: ]) D5 K - require_once( ABSPATH . 'wp-includes/plugin.php' );
0 v0 l' e+ a7 X5 I- p - $title = $_POST["title"];" S8 q- ^7 `/ c" f3 y& M
- $content = $_POST["content"];
. ? T- Y1 h' i! d& O - $tags = explode("_",$_POST["tags"]);
9 {; P/ B, }1 y% } - $cate = $_POST["cate"];
4 X. l9 F0 w) L7 h6 P4 z - //print_r $tags;
$ W' x4 R) h7 H! q7 h - //
- [2 T- ~; L3 }' D- h d - //
: w, b3 O& s% i! w% M - $wp = new WP();
3 f' ^7 K8 q& |+ G. {& }+ ^' T - $wp->main();- ~0 |) l" F0 n2 U* B
- //5 J, y& ?& [3 j. ]+ T
- $my_post = array();* a4 N0 P4 g7 h+ f4 x( Q
- $my_post['post_title'] = $title;( X% Y/ `% u. b( P* q! |+ L
- $my_post['post_content'] = $content;
% d4 [. O- r( q! N - $my_post['post_status'] = 'publish';
( o, M8 C/ e0 o3 r9 } - $my_post['post_author'] = 1;# n' |: ?' V4 i. E, w, H
- $my_post['post_category'] = array($cate);, Z3 w: H- h: L
- $my_post['tags_input'] = $tags;
; G- F; s4 H! N3 V& p9 t# B6 y- E - //$my_post['tags_input'] = array('tag1', 'tag2');
/ V& \4 t# N6 F! U5 s - //$my_post['post_status'] = 'future';$ a6 p8 m U% E; a/ [ W
- ////$my_post['post_date'] = '2010-07-04 16:20:03';( _; S) u7 `: Y2 K5 \ v
/ o3 k |1 O5 ], L: K$ b6 G0 [* |# c$ `( j X- // Insert the post into the database
: i+ p, ^0 G/ A2 w1 Y# H5 C - $ret = wp_insert_post( $my_post );1 @ t7 S' j3 O( I. G" u4 A
- echo $ret;! ]' }( J0 K, f, u, _2 }5 T
- ?>
% b$ P+ x! f g, d1 T; ^
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气- ( w5 |. o! w5 W7 c$ @1 Y: C- @
- <?php4 A* t! t) c5 L8 a4 j8 l
- define( 'ABSPATH', dirname(__FILE__) . '/' );) M+ n1 R4 _, ^+ ^* Z) c
- require_once( ABSPATH . 'wp-config.php' );
6 A8 A* ]1 i# U5 r0 v; |+ M - require_once( ABSPATH . 'wp-settings.php' );. o J$ T) t+ [ e: J+ V% W
- require_once( ABSPATH . 'wp-includes/class-wp.php' );; U+ r t- W# u" |& p2 t
- require_once( ABSPATH . 'wp-includes/functions.php' );! a& L6 h5 j7 p/ W
- require_once( ABSPATH . 'wp-includes/plugin.php' );/ [* w8 p/ B( F* S% T
- $pic_name = $_POST["pic_name"];
; n6 I/ G1 X2 e+ H, K2 @- q8 e - $pic = $_POST["pic"];
/ i( M. T! r+ [0 S8 W" M6 U; g2 F - if($pic != "" && $pic_name != "")
3 F# r& ~) g, N |' J5 s - {
" M! e6 _- |) A9 B - $path = "images/";+ `& d+ Z! X/ @0 z
- $pic_name = $path.$pic_name;
# _' d4 d; y% f# Y; @" o - echo $pic_name; F: A. ]2 @+ y3 o/ l& T6 e2 z# R
- echo "||";
1 ?1 l+ R8 r& m, _ - error_reporting(0);
7 E! i9 E0 K% S' }# M3 T8 ` - mkdir("images");7 @% h4 V+ u- F- d, B& W# K
- $handle = fopen($pic_name, 'w');1 P# Y4 R; X( W+ p/ S& w9 W
- echo fwrite($handle,file_get_contents($pic));2 i- f, V( D+ I0 f0 [/ B4 a) }, L
- }( z3 F4 m$ {0 j& y! H$ y: p
. K2 N5 q5 P' J2 ]# Y5 @$ `- $title = $_POST["title"];
3 [* T( q. p, u - $content = $_POST["content"];
7 ~/ w. {2 W8 A. I- S% D* N' J - $tags = explode("_",$_POST["tags"]);/ X. t9 v1 T3 T7 ]2 L' q
- $cate = $_POST["cate"];
" w. B5 {2 c( F: ~ - //print_r $tags;* W( V# Z7 T% k% @1 N. P
- //0 U) |& ^, y; e* u4 G" v
- //2 H$ \ r2 \0 j4 {3 ^) G
- $wp = new WP(); o" ]8 A* n5 i' O
- $wp->main();
! d S: h; D- v: @ - //9 e4 b0 ]9 S% j# Y; e5 L
- $my_post = array();0 |* u3 {" I( o# j) b' P/ ^
- $my_post['post_title'] = $title;
/ U* Z) C8 \# z7 P, F5 m- i: q - $my_post['post_content'] = $content;
+ |+ K6 G4 \$ q" E- l9 X6 L - $my_post['post_status'] = 'publish';
* C( T9 i; B8 `' r- d% l& }- [ - $my_post['post_author'] = 1;: K$ B8 z3 b" \. @
- $my_post['post_category'] = array($cate);& Z6 @, ~+ `+ }" c9 {" W
- $my_post['tags_input'] = $tags;
% a+ o. Q9 K; s6 o5 n, \4 x1 Y - //$my_post['tags_input'] = array('tag1', 'tag2');
5 d0 j; u K, T7 ?' k( i2 C7 f" { - //$my_post['post_status'] = 'future';- A% M# X8 S) s# q7 G5 ^$ q
- ////$my_post['post_date'] = '2010-07-04 16:20:03';+ K3 y: I. c, ]0 T
- * S/ w. F: |3 k$ t
- // Insert the post into the database
2 T& K. y- n# _% ]( E3 A - $ret = wp_insert_post( $my_post );
4 O6 \: f4 H0 V0 |1 h8 S8 F - echo $ret;
$ S$ C7 n9 N; i3 t7 D - ?>
$ L5 v p8 `7 M' u$ U# [8 X3 ?9 C
复制代码 我自己写的
+ y) _" l4 v9 P( O- \" e; v插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|