|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑 , {) V: _& \! b$ \9 ^: T
0 s y& s+ `! b直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持
2 I8 n0 A2 w& @1 g7 L# v" d. q) z/ q! |. ~2 J7 ^3 C
我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了
+ |5 \' z: O- |* w, g; o# n+ j6 B+ X# T
方便实用 简单 何乐而不用呢- 9 q9 T8 ]3 m3 o, L; G8 n, v
- <?php
" g% m: m+ b; G" O - ( y& S$ g$ z+ s$ O m# |
- define( 'ABSPATH', dirname(__FILE__) . '/' );& q8 A6 C3 j9 T* o3 K8 e& o
- require_once( ABSPATH . 'wp-config.php' );
/ J# v9 V; H) M! b$ z( A - require_once( ABSPATH . 'wp-settings.php' ); B. L7 |" j( c5 \; B h" }3 h
- require_once( ABSPATH . 'wp-includes/class-wp.php' );
/ B6 {6 W7 P. b% f. ^( a \" Y% Z4 C4 X - require_once( ABSPATH . 'wp-includes/functions.php' );
# I# N2 I8 \8 Z$ Z' e - require_once( ABSPATH . 'wp-includes/plugin.php' );
# ~5 w! g. b% H4 E% N& x1 w6 X - $title = $_POST["title"];/ o q6 O8 z0 i+ a) E
- $content = $_POST["content"];
, B o# Y5 u% x- \ - $tags = explode("_",$_POST["tags"]);& C) V& M1 V# t4 `- {- l/ _
- $cate = $_POST["cate"];
. N+ z" }, j$ Q" ~2 O - //print_r $tags;
! k' p6 m! ?! ?5 R - //$ C& [# @9 _8 V
- //
) i' H4 U5 A4 Z- y+ ^; s - $wp = new WP();( j% }; q) ^3 S0 J
- $wp->main();
0 {$ x# G5 G$ P$ F: o: \7 U - //
/ p% x5 a p( R% {7 r' C1 l3 k+ ] - $my_post = array();! l, C5 i4 [+ {) ]6 x- }
- $my_post['post_title'] = $title;
1 c$ R3 X, f3 l) Y - $my_post['post_content'] = $content;& F% E; |+ l& E( S' t7 f6 f
- $my_post['post_status'] = 'publish';6 p5 a5 G( O h3 [) y$ L
- $my_post['post_author'] = 1;5 ]0 j6 i A, a# ?# n x
- $my_post['post_category'] = array($cate);) L7 ]+ p! Q4 h" |& j4 a3 n
- $my_post['tags_input'] = $tags;
# q T2 e u7 i1 |, C% Y) N# B - //$my_post['tags_input'] = array('tag1', 'tag2');! R! |+ E, O e$ l
- //$my_post['post_status'] = 'future';
0 i( u/ w0 ^( U4 P7 y - ////$my_post['post_date'] = '2010-07-04 16:20:03';
v9 y& m! D) y; w- |1 s
7 J. f; Y8 j9 ]* Z, a+ R- // Insert the post into the database3 f! n- {: {0 I4 O3 v J: u
- $ret = wp_insert_post( $my_post );2 s' |$ b y7 W+ C+ |4 }- c( \
- echo $ret;5 E1 O/ j. A" j5 U5 z4 Q
- ?>
. b1 k* S% s1 l( o
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气- - a% |% |0 c. q. w
- <?php s1 t$ ~ ?% Q, Q# o. K6 f. w
- define( 'ABSPATH', dirname(__FILE__) . '/' );" Q, P8 M5 J: u# i9 c
- require_once( ABSPATH . 'wp-config.php' );/ X' O4 S. W8 G, v" K7 @; o
- require_once( ABSPATH . 'wp-settings.php' );9 I2 a9 a, _/ g* N9 H5 e
- require_once( ABSPATH . 'wp-includes/class-wp.php' );# i9 {% I) `# ^3 I0 S, z9 O2 c' f
- require_once( ABSPATH . 'wp-includes/functions.php' );
% e: u0 }0 v. P" w Q - require_once( ABSPATH . 'wp-includes/plugin.php' );
0 m2 m. W3 t5 Y - $pic_name = $_POST["pic_name"];
- s1 d; B9 m9 o - $pic = $_POST["pic"];7 z8 \8 [- G/ [
- if($pic != "" && $pic_name != "")
: d" B5 n' Q# C5 m$ \ S; T! S* X( r - {/ ~0 G$ z$ Y! ^0 z1 g
- $path = "images/";% ^6 F- k* K- D7 H
- $pic_name = $path.$pic_name;
3 _; x7 k0 @9 A - echo $pic_name;4 K" R% v0 P& \) y
- echo "||";
; w$ i' P- E4 j. g. y \- ~, A' } - error_reporting(0);$ V' j1 J v5 n" L1 ?3 f
- mkdir("images");, g7 B/ J; {& T$ U1 o) K5 d9 m$ g
- $handle = fopen($pic_name, 'w');
! {1 d* a# q5 \, s1 b - echo fwrite($handle,file_get_contents($pic));5 H! x( R% q, K7 s8 z' T) F
- }1 E" O$ l3 o# J7 l* ^
9 \9 ^1 W8 x [7 \" B5 i c- $title = $_POST["title"];
# \; Z6 ] T' s8 X1 C - $content = $_POST["content"];
- p+ _! }: y+ ?) l2 w - $tags = explode("_",$_POST["tags"]);4 {6 F% w4 a! E6 _
- $cate = $_POST["cate"];
- k! n! r+ Q; t& \! Q - //print_r $tags;. x9 k. l5 Z0 z3 T. t
- //
( M) s! y+ ~5 @& c - //
- l; c( F7 z3 i7 R - $wp = new WP();
7 k4 U( X. f( L7 Q - $wp->main();8 E. m/ m/ ?8 D- I+ h% E
- //' l! w( |$ n( z& | H
- $my_post = array();
8 r( g! q2 P Y# o: e# f - $my_post['post_title'] = $title;
7 \+ d6 |4 Q# b1 [ - $my_post['post_content'] = $content;" r$ Z% K) f# d4 \7 {* W
- $my_post['post_status'] = 'publish';
/ ^3 {+ G4 |/ h2 z' f( N+ Q2 i - $my_post['post_author'] = 1;
. i* F6 L9 k' a2 z- {% [ - $my_post['post_category'] = array($cate);1 h0 [# a" ]3 [+ x) |" @+ c
- $my_post['tags_input'] = $tags;
- j7 ?# H; u6 s. D9 h - //$my_post['tags_input'] = array('tag1', 'tag2');
: V) M! V6 G6 h: {, m - //$my_post['post_status'] = 'future';
. L( d) d7 S# v9 E W - ////$my_post['post_date'] = '2010-07-04 16:20:03';$ P; k0 U3 l. E! E' U. V
- L0 v" g2 b. U6 S9 M
- // Insert the post into the database% |+ }1 e- Q( d1 U! S) f! a
- $ret = wp_insert_post( $my_post );0 c% P0 L' }; \' H8 A: L
- echo $ret;9 d+ W5 ^; }8 I4 q/ o2 F' I9 b
- ?>* E3 _" r( Q; X+ y) S6 Q
复制代码 我自己写的/ J4 V; h- I" }& B; y5 a6 g C+ j' q
插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|