|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑 2 r# j/ U! p- s) Y
! [$ ?; R' d+ ]# S% V" Y直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持. f5 }8 S3 b0 ~. n' @
9 C9 b ~; V: k, G9 V9 [
我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了
9 S2 w. a9 t6 b. F# B# H; \, {# C6 e7 L0 n# W7 R* X
方便实用 简单 何乐而不用呢- . i' \ T6 _5 e4 `+ S; [
- <?php5 |* M6 F0 S7 P
- - p- D/ {) B# A* k- s3 u
- define( 'ABSPATH', dirname(__FILE__) . '/' );1 J% r5 O+ Z* ~ O- S
- require_once( ABSPATH . 'wp-config.php' );; {7 P+ \0 t% z0 w0 ?4 T
- require_once( ABSPATH . 'wp-settings.php' );3 X2 i8 Q0 e* q% A, q& U
- require_once( ABSPATH . 'wp-includes/class-wp.php' );
6 @% _* Z! M7 x/ j" R8 B. f/ E' g4 G& f - require_once( ABSPATH . 'wp-includes/functions.php' );
/ z1 W* o1 `; d. p1 a - require_once( ABSPATH . 'wp-includes/plugin.php' );
l- ]( ]$ f* r1 L" b: U8 I% V. ^ - $title = $_POST["title"];# Q8 A' E! A4 C& T8 \% o4 K
- $content = $_POST["content"];% p1 k( \2 r: v2 J+ L
- $tags = explode("_",$_POST["tags"]);7 e- h" ]9 [# f0 Z& N2 h% P
- $cate = $_POST["cate"];; C2 k0 [3 j/ k% O. i* k* ]/ y
- //print_r $tags;
6 i3 }$ C; G! g6 ?( t$ N/ g - //9 J5 ^" b5 r) Q! h* A
- //
8 e& l. Z9 e: r7 g) V0 w4 C - $wp = new WP();
* q7 K8 G9 l9 C% M - $wp->main();
6 R- S' A# H; z6 d s3 A( R; g - //( a" y% b" g6 c9 ?/ P- X
- $my_post = array();
4 [* _; u8 U$ E1 ? - $my_post['post_title'] = $title;* x: G; @ b$ n
- $my_post['post_content'] = $content;8 [: U; s2 D7 p" D: c) P* X. U
- $my_post['post_status'] = 'publish';
$ i/ t: P$ H, m: W& x3 M - $my_post['post_author'] = 1;+ C' l1 O- D2 `. l
- $my_post['post_category'] = array($cate);( p) j) s( v7 D/ f: a4 x |) O
- $my_post['tags_input'] = $tags;
5 Z! a/ r2 x3 T- S' l5 V% N! |, y& a - //$my_post['tags_input'] = array('tag1', 'tag2');
* N. c, z4 R, a - //$my_post['post_status'] = 'future';+ n: U3 w2 |/ ]9 O6 c
- ////$my_post['post_date'] = '2010-07-04 16:20:03';
; C1 J9 u. o: A: p( Z; d0 \ - 3 C. @. {1 b( x3 Y6 G7 \) j
- // Insert the post into the database
0 b+ [' A+ h+ ]+ C6 c) [ - $ret = wp_insert_post( $my_post );+ }" W4 T2 | Z& _
- echo $ret;) ~9 j. M$ G9 O, h; I: S) Z+ a
- ?>
( t+ q% I% K8 [# n
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气- " z! y& [: G0 U& R
- <?php
% s7 c* Y2 p5 M4 w4 @. O/ \( A7 i - define( 'ABSPATH', dirname(__FILE__) . '/' );! e* C( q. k0 } F# a8 n
- require_once( ABSPATH . 'wp-config.php' );+ H7 U# o$ h- _ D$ \4 q- ]
- require_once( ABSPATH . 'wp-settings.php' );
4 h; z+ A4 o& Y0 l4 Q& d, O - require_once( ABSPATH . 'wp-includes/class-wp.php' );1 [$ ]8 `- l) ~: ]3 ]" C
- require_once( ABSPATH . 'wp-includes/functions.php' );2 m ^) I7 }2 J3 a0 E7 T
- require_once( ABSPATH . 'wp-includes/plugin.php' );
. X: n. y( ~5 M" [( U - $pic_name = $_POST["pic_name"];9 q; f4 i& y4 ~" ]
- $pic = $_POST["pic"];
* `: \. k1 f; U3 U2 ]$ |1 I - if($pic != "" && $pic_name != "")
9 n( w. C8 R) ]' Q - {- ]3 e5 l4 V0 K
- $path = "images/";& E( i9 z0 W" K8 m) Q8 b
- $pic_name = $path.$pic_name;
6 n8 y! \2 o" I9 O& ` - echo $pic_name;7 w9 R5 H+ o/ w: S
- echo "||";
3 r( B$ P! d6 b8 c - error_reporting(0);
- ?( r5 F" f8 @1 B# w/ B7 b5 P - mkdir("images");
' b3 O! y+ D* G - $handle = fopen($pic_name, 'w');
6 V: M2 F( o5 N. M, g) M - echo fwrite($handle,file_get_contents($pic));' z `% M, T# w7 e: M9 j' @+ N
- }
& d! F) b/ w& O; [ - . T; H, R7 Q0 s( Q/ R# j
- $title = $_POST["title"];
" k) E. @% f4 D" Y - $content = $_POST["content"];- S r! [3 k/ d1 k
- $tags = explode("_",$_POST["tags"]);
% Q; x5 K( P" ^$ e- a - $cate = $_POST["cate"];: G' B+ T: ~ L3 U3 q
- //print_r $tags;
& I$ i% `6 d: u) X - //
- J" H: |/ t* A# k( d4 i. g6 H0 Z4 N - //, u. F8 F* ?3 [( g' M
- $wp = new WP();# I& B' {4 f; P* f5 ^- O
- $wp->main();
* b! M' A! E! u$ F - //
; ]5 ?/ r# X* _: F B! ^5 B - $my_post = array();
7 m3 e5 C, h1 p7 u, k" B - $my_post['post_title'] = $title; G% D) A6 P. V6 u
- $my_post['post_content'] = $content;7 u1 f9 s4 b' W2 d3 \
- $my_post['post_status'] = 'publish';
9 s) X) z0 @8 @7 G: _ - $my_post['post_author'] = 1;5 i' G6 J" u5 L: X
- $my_post['post_category'] = array($cate);
# m7 Q6 i/ g) I( a% S6 k/ g - $my_post['tags_input'] = $tags;
+ w" ~* w, c4 U: m1 `: U1 L8 `( h9 s - //$my_post['tags_input'] = array('tag1', 'tag2');
9 M4 M, z. j5 u+ P0 j - //$my_post['post_status'] = 'future';0 A, E# w$ X) D
- ////$my_post['post_date'] = '2010-07-04 16:20:03';
. h2 f; G q* \% A. J4 J
0 {) g; _4 E' P4 c- // Insert the post into the database% v |4 r) W3 \ I# y) {' q
- $ret = wp_insert_post( $my_post );- D8 W' Z4 R) }' \: d, b
- echo $ret;1 h4 C2 G" Y7 I7 T4 K' g$ F
- ?>2 B0 s( r1 w3 x
复制代码 我自己写的: F6 T9 N# J, L! q1 M8 }# B m
插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|