|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑 3 j0 r( p' `6 W8 z
1 ]9 w1 R; O2 Z' @; U直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持
, @4 r1 _" Q8 Y) O4 K
1 ?( W$ v1 S& Y我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了
* t: ]# M Y, O& y1 p8 S+ R5 u$ ?- r5 w' Q% H* h
方便实用 简单 何乐而不用呢
# @; ], L* C8 q. U# ]+ Q) O- <?php; Q) p, _. W5 S* Q# V; @
- & a% {) t( d$ A" q2 u$ b
- define( 'ABSPATH', dirname(__FILE__) . '/' );/ i$ h- i/ C. q9 R- G
- require_once( ABSPATH . 'wp-config.php' );3 ~, y) ]# |6 M2 s& e! @0 O8 y
- require_once( ABSPATH . 'wp-settings.php' );
) U! ]/ U- A$ Y2 r- P* i5 k2 k - require_once( ABSPATH . 'wp-includes/class-wp.php' );
9 i/ I% W( B4 v& H- G - require_once( ABSPATH . 'wp-includes/functions.php' );" y+ W8 |" j6 z6 j
- require_once( ABSPATH . 'wp-includes/plugin.php' );8 N4 z8 x; O9 r4 F5 s
- $title = $_POST["title"];
7 O8 O2 o- z# a& E7 a) Y5 ^4 h - $content = $_POST["content"];
" W/ J# Y' c5 p* I$ F% s - $tags = explode("_",$_POST["tags"]);
# ^, [9 W! N! r - $cate = $_POST["cate"];
$ d% m) S. I0 a* {$ q. J - //print_r $tags;
2 n3 u7 H( {4 f - //' S& k+ |. G8 u, O
- //3 W$ a1 `& x& F
- $wp = new WP();& I2 M: [2 {! z+ f( t8 t
- $wp->main(); o* V/ X1 x! c# C0 y
- //5 Y( e. e" i& }, y- G1 U, t
- $my_post = array();
: N ]# v" L, }0 M$ H - $my_post['post_title'] = $title;: v% [$ j- m' [. h6 G+ f
- $my_post['post_content'] = $content;7 k6 b2 K# I- d( @0 Y/ k, ?
- $my_post['post_status'] = 'publish';
+ C3 a6 I& g% f" |4 b1 j - $my_post['post_author'] = 1;' k2 d( ~, O+ b
- $my_post['post_category'] = array($cate);3 ]9 d0 ^4 j3 ^/ T3 M; S
- $my_post['tags_input'] = $tags; E# r* Y" K6 }0 k8 a8 u# Q. t0 l
- //$my_post['tags_input'] = array('tag1', 'tag2');
" a, [ p+ U# j/ r - //$my_post['post_status'] = 'future';4 E5 {8 s+ l& l D9 O- j- V% {
- ////$my_post['post_date'] = '2010-07-04 16:20:03';
5 V) S; _/ [) g2 f - $ s; J9 G9 z4 Y! \. h9 q) B* }
- // Insert the post into the database, v1 V! G9 {; X2 R( w; J# `3 c
- $ret = wp_insert_post( $my_post );, n6 y, W2 @1 V9 E( v9 p. V1 ^
- echo $ret;5 F2 v/ x9 @$ _4 i; _1 P% C# O
- ?>
. W2 U+ {' W" C
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气- & E. g4 N* v* z/ w' _+ t
- <?php) m; k3 l% H& r/ R8 D# Q2 r# l7 P
- define( 'ABSPATH', dirname(__FILE__) . '/' );
6 @6 R! ]8 S* U' ], P+ |; Q7 M' j - require_once( ABSPATH . 'wp-config.php' );2 I9 o8 c+ b8 M+ S+ v( G
- require_once( ABSPATH . 'wp-settings.php' );8 y+ b* p4 q& b& T+ g* Q% |
- require_once( ABSPATH . 'wp-includes/class-wp.php' );9 \7 l u1 W7 M
- require_once( ABSPATH . 'wp-includes/functions.php' );4 R. w: S; _. d. `
- require_once( ABSPATH . 'wp-includes/plugin.php' );: H" ?' S/ N% v6 Y( T
- $pic_name = $_POST["pic_name"];* ]6 Z7 [, A% O( H
- $pic = $_POST["pic"];' e( h u( E5 g" a+ x
- if($pic != "" && $pic_name != "")
' A2 ]6 t, d7 t+ h; b' } - {
4 N9 s2 W8 \3 k+ b: T" I9 g - $path = "images/";
2 }- I# e5 A' Q1 g2 P' j" I - $pic_name = $path.$pic_name;
; [" x- X2 {7 Q - echo $pic_name;4 u) t8 n7 D+ e/ H7 x* ~; N6 @9 m
- echo "||";, F$ e, q& H- a# J. ?+ D+ z
- error_reporting(0);
' G; z% j# M: e+ d) y: g; ^ - mkdir("images");1 P1 b' w3 @0 t
- $handle = fopen($pic_name, 'w');
% u% k; e- H+ d) o2 t - echo fwrite($handle,file_get_contents($pic));
5 B* v, m2 ^9 b! N% y - }
- J, N/ L6 S- ^
, \7 \% X) _7 _& q% V4 ^0 P Y- $title = $_POST["title"];4 G, J& U( Q1 Q; t" ?! z
- $content = $_POST["content"];5 {2 g* I, _6 F1 t0 t0 \2 F& q
- $tags = explode("_",$_POST["tags"]);
* o. C* K, E1 ` J' {0 T - $cate = $_POST["cate"];
( U2 b2 O, Y0 f5 j- s! f" F" A) M - //print_r $tags;' O- U9 I* Q, u2 T
- //
. s, |1 ^( c- L/ x" d7 s. J - //
, L' b( }% A7 u3 v! X, ~ - $wp = new WP();
$ d+ } _8 z( a9 T* I$ t - $wp->main();; v: [& a* F3 v' \
- //; \7 p9 z" \+ S# J
- $my_post = array();# Y1 E1 V& F9 P4 j9 o1 _
- $my_post['post_title'] = $title;& e6 k* I! S! R5 Y" u. @7 b2 ]
- $my_post['post_content'] = $content;
; I" q' ], P8 \6 _* z0 {- a - $my_post['post_status'] = 'publish';+ x# n& O3 C& ]5 c# z, j! t0 L
- $my_post['post_author'] = 1;
" x- ]+ H- w' V0 Z4 |) \ - $my_post['post_category'] = array($cate);6 ?6 L4 t3 ~3 g0 m, p
- $my_post['tags_input'] = $tags;
4 L* j( A1 X* ~+ C1 _. S: S - //$my_post['tags_input'] = array('tag1', 'tag2');0 ]2 N& N i3 D5 d4 n
- //$my_post['post_status'] = 'future';; f3 }+ N8 f$ }$ Z
- ////$my_post['post_date'] = '2010-07-04 16:20:03';8 S7 ?) Y' f! q( Z
& H# g+ H8 f# `% j+ b, S y% G- // Insert the post into the database" `5 {( G& d1 L$ |3 c8 Z
- $ret = wp_insert_post( $my_post ); C; N4 d* S! R4 K- ]
- echo $ret;3 ~1 Q+ \ h% H! D
- ?>+ d& y/ d3 Y$ i" z
复制代码 我自己写的
. o/ w# E1 t, I4 k3 A插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|