|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑
: a Y3 g# j# ?( ^
1 v! O) w- i% w1 s( K7 {直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持
& [0 h! W1 R) i: C1 U0 K- \5 d2 z% ~9 w# h' g$ l
我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了 C( P5 n6 Q% T- M4 X; a/ ]
5 ~2 I. w8 w& b! O7 Z; ?方便实用 简单 何乐而不用呢- 3 ~3 p7 L; l- z5 [
- <?php! `6 d6 q5 Y6 J
" _9 z- T% s: F9 a. R! s. ?- define( 'ABSPATH', dirname(__FILE__) . '/' );
4 j- m" X6 [; p7 L - require_once( ABSPATH . 'wp-config.php' );9 c- o @: W* Z. @( U
- require_once( ABSPATH . 'wp-settings.php' );
u3 o1 ?: V5 r: q. ?& W2 O - require_once( ABSPATH . 'wp-includes/class-wp.php' );
% a: K" Z$ D( A( t2 |5 A* C - require_once( ABSPATH . 'wp-includes/functions.php' );2 n! C/ b, g' q* M1 W+ n5 G
- require_once( ABSPATH . 'wp-includes/plugin.php' );
4 m) a% Y2 u% |: R - $title = $_POST["title"];. T, A. ^! a; E9 }' P! ?1 N3 F9 J
- $content = $_POST["content"];: ?+ X! T0 U8 n, { D+ B
- $tags = explode("_",$_POST["tags"]);, ^* C+ b9 r) W/ s, U
- $cate = $_POST["cate"];! b' b( l8 z, \! Z
- //print_r $tags;( q3 s! }& r6 _' L8 u2 z% E$ F6 l' ^
- //
" J4 P" u" x& ]8 M; N - //2 `! I+ @* T( s8 w! H: p: G% \
- $wp = new WP();" e& S5 u& B% M" m5 ~
- $wp->main();
/ C" U/ S/ b# a1 n8 I% H - //
' Q. ?) l8 |, g% B$ E1 c - $my_post = array();9 L& i% ?& t* V2 e8 u+ Y
- $my_post['post_title'] = $title;
! N U! e7 Z: U. p' q - $my_post['post_content'] = $content;
1 v7 ?: O# g7 ~ s4 m - $my_post['post_status'] = 'publish';( S9 O+ l! \ Z! a% E5 g9 @
- $my_post['post_author'] = 1;" R; E: r6 ]" E o% ?, b' m
- $my_post['post_category'] = array($cate);
) g. r( o/ a# p3 {' C4 c - $my_post['tags_input'] = $tags;+ O1 \' N- e/ f. t
- //$my_post['tags_input'] = array('tag1', 'tag2');
8 I& | O3 n9 t/ B( q- F$ G - //$my_post['post_status'] = 'future';
2 {, o' p8 Y3 Q- X6 j. T - ////$my_post['post_date'] = '2010-07-04 16:20:03';
; b0 V+ V3 f% ^4 b0 q' t9 K
- f( ` H: Y4 y3 @; s! t0 Q' U- // Insert the post into the database0 h1 f$ r0 x, ?( R
- $ret = wp_insert_post( $my_post );
& U4 ? v" q& n0 e, f - echo $ret;3 w# N1 x8 |/ y9 x- a
- ?>
" i b! m' Z# ]% W( g8 ~; F4 d
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气
) N" ~0 X' s6 f( D- <?php
' q6 T- z* q, {. V2 e - define( 'ABSPATH', dirname(__FILE__) . '/' );
1 @; b8 K2 v8 I z6 H0 w. p4 [+ ] - require_once( ABSPATH . 'wp-config.php' );
5 [& Z5 A0 b0 h; G) c - require_once( ABSPATH . 'wp-settings.php' );! {8 U. h5 b8 I% W+ a) ?
- require_once( ABSPATH . 'wp-includes/class-wp.php' );
. b( I: R. g4 K1 L. w. ]" V - require_once( ABSPATH . 'wp-includes/functions.php' );
9 `$ B N# f4 }) [: H - require_once( ABSPATH . 'wp-includes/plugin.php' );, H( Y! o8 t7 x* h) N2 S# ]9 }7 x6 q; Z
- $pic_name = $_POST["pic_name"];% W4 C9 k, z/ T S( a8 w. X
- $pic = $_POST["pic"];! \" a* _$ a* g
- if($pic != "" && $pic_name != "")+ C5 K) U+ b& k+ q/ ^7 k$ E
- {( i Q9 N# ^+ _+ D% r
- $path = "images/";
4 k# @! o2 W+ w# ?9 n# g - $pic_name = $path.$pic_name;
( N2 M S( u/ n2 M& X1 E& Z. { - echo $pic_name;( ]' }7 C$ ^& B& S* w. ~
- echo "||";* z5 G! w7 h, m2 l0 m
- error_reporting(0);
" E# [$ F+ h+ I - mkdir("images");5 e. E% A! M1 C% {+ v! B
- $handle = fopen($pic_name, 'w');
]8 h! s# J9 J# r7 r. f2 x G% @8 | - echo fwrite($handle,file_get_contents($pic));
2 ?# h4 \4 m% K2 i9 z - }
* U- J! r' b% ` j - 1 F o/ ]1 P9 \; J, Z
- $title = $_POST["title"];' `) H: C& I* {/ {0 }
- $content = $_POST["content"];: v" z4 ]9 m: i9 w5 V( D; x
- $tags = explode("_",$_POST["tags"]);
% ^* i [) ~! V, @; D - $cate = $_POST["cate"];0 r7 I Z- h' k8 n
- //print_r $tags;
& a# b% L; W2 \: J/ b0 h - //
% }3 f/ G2 h# D) K% h7 y. Q - //& y; g/ z. l ~# w- x3 Z
- $wp = new WP();1 @. k1 r l8 c
- $wp->main();
# j2 S; {6 A' y: c - //
( ?2 j2 A6 R0 D - $my_post = array();
2 x( |$ U, j) D1 A - $my_post['post_title'] = $title;, ?; E4 T- M7 k& a& ]2 S( ~( O
- $my_post['post_content'] = $content;' u! L6 ^# K+ g$ z
- $my_post['post_status'] = 'publish';9 m3 b- {0 F4 V
- $my_post['post_author'] = 1;
4 |; i; {- U3 A ~8 g1 k" \ - $my_post['post_category'] = array($cate);2 j* A. w5 h( d
- $my_post['tags_input'] = $tags;
! I O+ c+ d1 I* |% N# M& I: h8 Z - //$my_post['tags_input'] = array('tag1', 'tag2');/ d# d) R$ S. u+ |# h$ E
- //$my_post['post_status'] = 'future';2 q3 ^8 y E/ Y2 q& X3 p
- ////$my_post['post_date'] = '2010-07-04 16:20:03';2 I U b% J; M. H6 @. W, [1 T
- ! \% b& ?% c6 B" r
- // Insert the post into the database# B% |' I; _. o- t
- $ret = wp_insert_post( $my_post );4 b8 U5 b E% {5 I7 k
- echo $ret;( \. z, E6 c( Y2 b% S$ h/ v8 T
- ?>& [' s1 u _& f1 P+ k
复制代码 我自己写的. [$ g' }" o5 q* R% N/ j, R
插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|