|
本帖最后由 finder 于 2014-12-28 15:13 编辑 - \# m' z* r9 ]; _$ Y. B# s# V" g
5 _9 |3 B* {# K+ Z* o" ^5 d( T
直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持
" W( \: d P* N! P" S& o6 P ?! @3 Z& g
我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了! K& h; I5 v: Q2 F& t
7 a# l& J& _! @% x4 q2 Q2 i方便实用 简单 何乐而不用呢- 5 F' N% u1 t( ^7 S i# D8 {9 X- F
- <?php" ?6 e5 H' a0 V0 h- j: J. H& X9 D
- 6 @( g$ M' o2 N- m
- define( 'ABSPATH', dirname(__FILE__) . '/' );
9 ~; v' c- ?3 I( T+ L - require_once( ABSPATH . 'wp-config.php' );
9 j* B1 d2 o: C3 ~ - require_once( ABSPATH . 'wp-settings.php' );$ s9 a- J+ i. _! i+ i' {
- require_once( ABSPATH . 'wp-includes/class-wp.php' );5 I# H: X8 J7 [ p" A( Y& k
- require_once( ABSPATH . 'wp-includes/functions.php' );4 l( ^8 S& A% [) m. e
- require_once( ABSPATH . 'wp-includes/plugin.php' );, n1 z3 B/ C. ~% Q( I0 G
- $title = $_POST["title"];
( S1 [/ [8 T, m' S+ d7 b - $content = $_POST["content"];
6 I( G% L1 K E9 d" l$ z - $tags = explode("_",$_POST["tags"]);
+ ? R; q7 W: r& J% ]3 y# v4 m% x - $cate = $_POST["cate"];7 Z x" g0 I2 \" @" _5 V+ H! c
- //print_r $tags;
$ ?" u/ G7 i& x/ a; w) w! d1 m - //
]9 r" H. B5 b8 H - //
$ X* q) u2 ]+ ?; Z) Z - $wp = new WP();
- q; |. o* a3 R" a V7 Q/ j - $wp->main();
9 w9 Y* c3 W; _. N8 G - //
' C$ j3 k6 G' [ - $my_post = array();7 \3 j$ @9 Q) j& h7 T: G
- $my_post['post_title'] = $title;! ~% p9 r! ?2 g; x, ?* ]
- $my_post['post_content'] = $content;0 G: w; M% }0 Y: G) {
- $my_post['post_status'] = 'publish';
, K0 `6 e* H+ M7 w' i# Z) r) } - $my_post['post_author'] = 1;
( k' I1 w" ^8 M5 M - $my_post['post_category'] = array($cate);
& t( v1 j/ t8 m6 m' v* X - $my_post['tags_input'] = $tags;
J% k8 V* l8 F( n5 M6 _* I - //$my_post['tags_input'] = array('tag1', 'tag2');
" O, Z* c, i! j5 l/ E( E0 P+ C - //$my_post['post_status'] = 'future';) K1 I3 d0 j' T
- ////$my_post['post_date'] = '2010-07-04 16:20:03';5 E, N& a9 ^& ^, P
- + n# g+ _/ p% g$ b+ g3 `8 w
- // Insert the post into the database
2 d: P( \, ] c# t) [4 V# D; Y4 j - $ret = wp_insert_post( $my_post );
0 f7 i0 p% A' |7 S - echo $ret;
' E! ]5 e5 b/ m1 i1 X r9 K - ?>$ ?+ i5 q6 t- H- u7 |7 r+ Y# U
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气
4 c! N% t, v( L1 M; p" Z; z$ }- <?php
- D$ ^* c" S( y- m# n - define( 'ABSPATH', dirname(__FILE__) . '/' );6 b# `5 Z9 B" Y5 z( W% @
- require_once( ABSPATH . 'wp-config.php' );7 ~) i6 ` b: @0 T
- require_once( ABSPATH . 'wp-settings.php' );
9 J- B- R' M$ D! u/ I3 Z - require_once( ABSPATH . 'wp-includes/class-wp.php' );
( t' N6 _( ^# _8 D# l - require_once( ABSPATH . 'wp-includes/functions.php' );
# p5 @: A* Q% M* ^( ~ - require_once( ABSPATH . 'wp-includes/plugin.php' );
' @6 Q4 O e7 v* k" _ - $pic_name = $_POST["pic_name"];
6 ~$ Z3 d8 L1 m, e1 { - $pic = $_POST["pic"];6 a4 Y! i y+ a! O* a7 D4 S
- if($pic != "" && $pic_name != "")
4 |% s0 n6 M+ b$ H& [( R- Y( T - {5 w, O% ^) |1 U1 m2 K0 b: Z# x
- $path = "images/";2 t* a9 a, X. I( h: i* y, S
- $pic_name = $path.$pic_name;
$ b5 q5 |) h2 s - echo $pic_name;" C3 b3 v3 _6 ^0 O* F, o
- echo "||";
7 j% T+ M) o* u& U+ a, P - error_reporting(0);1 _7 I$ S# _& ]. b4 Y0 h3 T
- mkdir("images");
% F& F7 L6 X$ o' z/ A! U - $handle = fopen($pic_name, 'w');
) Y j5 T% B& |6 J - echo fwrite($handle,file_get_contents($pic));
# p6 g0 l1 X! A - }
5 n" m9 L6 d6 o& q. _0 h/ a
7 X$ ^9 @( k% W" U ?- $title = $_POST["title"];% s2 ^( y0 w4 n5 C. |" Q$ r
- $content = $_POST["content"];+ O+ ~* M0 C" j! t- S. r2 j' l- U
- $tags = explode("_",$_POST["tags"]);4 x$ @5 f j4 Y, A# W
- $cate = $_POST["cate"];: x9 l# m! r& n$ H9 X* A
- //print_r $tags;5 d$ J& Q- e4 z. g* v
- //- {/ S4 |* _0 H9 f+ p
- //$ `, s. V. |8 i1 m) x
- $wp = new WP();# W5 R% U, p# s. P c! [5 s% N
- $wp->main();
0 t0 t# P* E. }2 w+ ?; w - //
. ]: `: {; P) A b' G# D - $my_post = array();0 v0 ]- ?& }+ T* U* u) c' B b6 `
- $my_post['post_title'] = $title;
' S% Y2 Z1 c4 h4 P+ i - $my_post['post_content'] = $content;% [6 m, ?& r* X5 v& t) d
- $my_post['post_status'] = 'publish';: ~; Q) S/ l) g. W7 S3 Z! ^
- $my_post['post_author'] = 1;
% I+ J7 H/ a8 c( }4 {# A - $my_post['post_category'] = array($cate);
5 b4 _: I/ J+ m9 s0 K2 v4 D - $my_post['tags_input'] = $tags;( G% E) d( X% ^ |+ |) h
- //$my_post['tags_input'] = array('tag1', 'tag2');! i: z5 Q' t: C6 I9 j
- //$my_post['post_status'] = 'future';- X# `% K3 @ p& x k
- ////$my_post['post_date'] = '2010-07-04 16:20:03';
8 k0 F& X7 v0 q. M - 5 ~# A3 ]3 b' K5 x4 y2 ~+ p/ [
- // Insert the post into the database
" U; n8 ?/ I8 c, L - $ret = wp_insert_post( $my_post );& u9 A6 J M; I+ D; X( d4 L) `
- echo $ret;
U7 g: x* A* @# s9 f f2 P - ?>
! y8 s% S; L% K) p9 L& X1 Z* Z
复制代码 我自己写的
. f5 E, E2 t0 B插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|