|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑 ( P- i0 w$ c: c: |/ f3 X$ `/ {0 |0 P( F0 F
% D5 w$ O; g+ Q+ Q, t9 V9 q
直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持
' [% B* u- v% c; g; P/ r2 v7 s' t- V) |1 Z
我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了8 g( v: B1 K W* X+ U
+ [. y8 H0 n" m! r! k' f& P3 t ^
方便实用 简单 何乐而不用呢- " \7 y9 l) }6 p% Z# G* h
- <?php- V' }( x1 S, n l4 w
- * n; L7 Q/ h. _- t/ c- Z+ {
- define( 'ABSPATH', dirname(__FILE__) . '/' );: ~/ r# E) x& v
- require_once( ABSPATH . 'wp-config.php' );
, O9 m* n" P3 [, T! ]# [! E - require_once( ABSPATH . 'wp-settings.php' );
" q8 j0 v# X4 W' W3 `( g ~ - require_once( ABSPATH . 'wp-includes/class-wp.php' );
3 N' P1 ~+ J. r6 x R - require_once( ABSPATH . 'wp-includes/functions.php' );
, W+ G& ]' \9 e - require_once( ABSPATH . 'wp-includes/plugin.php' );- `" l0 D4 q6 d
- $title = $_POST["title"];
7 M$ b. Q& B5 n6 X - $content = $_POST["content"];0 i. C# M( F. T3 Z. H
- $tags = explode("_",$_POST["tags"]);: [6 a( z" D0 d+ y2 q
- $cate = $_POST["cate"];+ x# h* Z+ g$ T" r8 a1 @6 ~7 B
- //print_r $tags;2 \- |. G; w8 n, L' E2 i& B# }
- //% ~5 k8 n+ o6 l# t" a0 n b
- //7 ?' b$ R- i. p \( k7 b
- $wp = new WP();7 E# r& t: Q( ~% N& T
- $wp->main();
, k& S" K- o, E3 S9 O: G) N* D5 K - //* y! B8 b, }0 q! Q/ g% X
- $my_post = array();# |" x+ h- [' c2 \+ D% F
- $my_post['post_title'] = $title;" Y2 w4 A, ^$ z" ]
- $my_post['post_content'] = $content;1 P0 ~& K% M, I) ?% e
- $my_post['post_status'] = 'publish';
3 Y+ }. l; q( z2 e, M5 y: L - $my_post['post_author'] = 1;
( t2 g6 `9 B3 z - $my_post['post_category'] = array($cate);. w1 d+ G! h5 U m& p. u; q0 R0 g! i
- $my_post['tags_input'] = $tags;
0 j: s+ C' b3 P( L+ _ - //$my_post['tags_input'] = array('tag1', 'tag2');
* ~* y9 |" i( K: s - //$my_post['post_status'] = 'future';# e3 n I, T( @$ v
- ////$my_post['post_date'] = '2010-07-04 16:20:03';
4 ?- b2 l( h* {& n
# z. J1 M. I, o8 S- // Insert the post into the database& m* I$ s1 T2 F5 V3 h, Y& n
- $ret = wp_insert_post( $my_post );+ }! _* V. \2 {) Y
- echo $ret;/ r) C# c6 S! {5 b
- ?>
0 `) Y8 i! K# S2 `
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气
' g" e% ~# t+ X `- <?php* v9 G- Y8 T3 r i- H
- define( 'ABSPATH', dirname(__FILE__) . '/' );1 X* s- T7 x3 J7 L# s1 A' @
- require_once( ABSPATH . 'wp-config.php' );; n( q5 ~( o0 q
- require_once( ABSPATH . 'wp-settings.php' );) q1 |# j' M# w) b+ u
- require_once( ABSPATH . 'wp-includes/class-wp.php' );
. V, K3 u0 a2 D5 w" ]; l7 J' K& Z - require_once( ABSPATH . 'wp-includes/functions.php' );
/ i( I5 B0 A1 a: p' j& I - require_once( ABSPATH . 'wp-includes/plugin.php' );
& |2 H+ c8 g- h% k% C - $pic_name = $_POST["pic_name"];& _. g1 b) w9 {8 K
- $pic = $_POST["pic"];
, z1 t& ~. h+ x; ? - if($pic != "" && $pic_name != "")) ?0 x9 g. c! ~! m S9 [
- {
$ O: d; V; z) H2 s - $path = "images/";
+ E" r6 D1 @2 g% H* ^: v - $pic_name = $path.$pic_name;
. M# k- `/ F0 M. U9 ^# X - echo $pic_name; u4 b: E' c( f' N# m; `! Q
- echo "||";
) ^ L+ d1 \/ o% F1 M - error_reporting(0);
* o6 P; R$ w- r4 L - mkdir("images");% l3 a: b% o% Q0 T) a+ I4 v1 y. g
- $handle = fopen($pic_name, 'w');; ~9 z4 L6 J4 f- P* Z
- echo fwrite($handle,file_get_contents($pic));
6 k0 l7 c; v' R3 W0 S5 H1 S2 n - }
: s, g2 r% G( z - * f8 ~$ O U; x2 h
- $title = $_POST["title"];
. r9 t) l. s( L$ e - $content = $_POST["content"];) Q8 W" z1 M7 {( D3 x- t" w+ d4 F
- $tags = explode("_",$_POST["tags"]);' a% ~- h# S) o
- $cate = $_POST["cate"];
! z, u9 M' |. C6 d - //print_r $tags;
) R# d6 p" v6 c5 x% y; P3 a, {4 v g - //' i, D' t f3 C2 ]/ @' e
- //
* y2 m) l" V$ K ` - $wp = new WP();
; S; B% K1 n6 b3 g4 ~+ t - $wp->main();' }) s7 F p/ c; ~
- //
6 M( `: b5 ~; [* p4 \. X4 E - $my_post = array(); \. f9 L$ f; o- }
- $my_post['post_title'] = $title;
9 v* Q& i" L# x- f4 k - $my_post['post_content'] = $content;, Y4 U o: g) c% U: g3 [
- $my_post['post_status'] = 'publish';* a! O+ h/ x/ u" k4 U
- $my_post['post_author'] = 1;
b2 p% ]0 \3 c - $my_post['post_category'] = array($cate);3 F( J" f/ q% g9 U* `
- $my_post['tags_input'] = $tags;
! |8 ]* G6 Y# H! M q! N" V - //$my_post['tags_input'] = array('tag1', 'tag2');
! u% W/ n6 D6 E9 c$ G - //$my_post['post_status'] = 'future';5 {* B, y; G+ b- o3 {
- ////$my_post['post_date'] = '2010-07-04 16:20:03';2 b Q" u% A' R, M6 V# Q
: p4 R. E, @7 N- // Insert the post into the database; x3 |. M( W) Z, V9 \
- $ret = wp_insert_post( $my_post );( x. \6 u$ f: Q- u; H, I' |9 _
- echo $ret;' i2 ?! Q, q) \, a
- ?>
6 p! t+ c$ V2 u7 ^! M
复制代码 我自己写的6 T% r, |" p" ^- T' [
插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|