|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑
$ Y' ~8 u3 q E" f8 Y, `5 C* c6 _4 ]! v8 R! W
直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持* I3 b e+ ~1 @2 g8 K
% |+ r9 o- | I F( F" F! |
我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了
- w8 s5 V' s( V& y; J8 k* \- K- J7 s1 z3 B) Q% I
方便实用 简单 何乐而不用呢
e. F# K; R( b- <?php
$ Z3 M0 B( ~2 B - : m. C6 y' D3 H3 ^6 a
- define( 'ABSPATH', dirname(__FILE__) . '/' );
+ x. q& [' F4 V* s - require_once( ABSPATH . 'wp-config.php' );. I. P' R9 I' J
- require_once( ABSPATH . 'wp-settings.php' );. _$ g/ m! v& B, B* `
- require_once( ABSPATH . 'wp-includes/class-wp.php' );; ^2 d& D3 X* H( R, O6 [
- require_once( ABSPATH . 'wp-includes/functions.php' );9 F: C; P) c4 p) N; w4 I
- require_once( ABSPATH . 'wp-includes/plugin.php' );
# D' ?- \0 D+ b: {5 ? `7 y - $title = $_POST["title"];+ k3 n- J" s9 W3 W# q# v& G
- $content = $_POST["content"];& q+ `1 @7 f7 d5 L
- $tags = explode("_",$_POST["tags"]);
; p) M) ]; D0 z - $cate = $_POST["cate"];# {+ _2 b8 E9 n- g
- //print_r $tags; W9 W0 \1 J# k3 k& R+ L/ e6 }" z
- //, L" I7 r! p" v# h; c8 N6 ]
- // A( `- r7 j$ F5 [8 f
- $wp = new WP();" b, |' R! l' p: f& H5 U
- $wp->main();
) p G& Y# w( `' Z7 p, `+ D: J - //3 k) q& g/ q/ h* ~. V: v
- $my_post = array();
* F) M- j) s2 M8 P% u - $my_post['post_title'] = $title;8 r$ _7 M+ U/ O, [1 `9 b4 P1 P. A
- $my_post['post_content'] = $content;2 u- P& j" m4 s5 h
- $my_post['post_status'] = 'publish';
" x: |/ W0 I+ M: @. M! ? - $my_post['post_author'] = 1;9 }8 H* b L' k) \. m
- $my_post['post_category'] = array($cate);1 B+ s' S2 }/ W( U6 j: y. x
- $my_post['tags_input'] = $tags;- r" P# Y4 `( h& n N
- //$my_post['tags_input'] = array('tag1', 'tag2');# C0 G. n' o4 e3 K- S
- //$my_post['post_status'] = 'future';
6 g, W2 S% g, A. w4 q4 c; v8 \+ n - ////$my_post['post_date'] = '2010-07-04 16:20:03';
9 o4 G6 y0 @; a+ X - 0 {3 g( A/ i+ h' m3 }. j
- // Insert the post into the database
' @: Q8 F+ z7 f' ~ - $ret = wp_insert_post( $my_post );: E7 _% H) x1 } @% P
- echo $ret;
* b& Y/ u4 O Q @0 m$ h1 `5 A2 H - ?>
4 }/ G8 r1 E1 F Y
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气- 6 ^% d" q" K% V' s0 i
- <?php
4 l% X& c0 J/ w6 ^0 ~ - define( 'ABSPATH', dirname(__FILE__) . '/' );2 @* i+ n% `9 n) @0 y' s
- require_once( ABSPATH . 'wp-config.php' );6 P* |# ]0 {8 F
- require_once( ABSPATH . 'wp-settings.php' );2 C! D- d: I! `5 C
- require_once( ABSPATH . 'wp-includes/class-wp.php' );4 e4 p& [2 o B& K
- require_once( ABSPATH . 'wp-includes/functions.php' );
& _; K7 P7 d* S- r- l - require_once( ABSPATH . 'wp-includes/plugin.php' );+ t( U+ p+ C b( b+ Q0 b/ H9 V% B
- $pic_name = $_POST["pic_name"];
5 [+ b4 q0 s" p2 r/ y4 i' n% m1 M - $pic = $_POST["pic"];& I% p# J" X4 C2 c8 w2 C1 O5 [* h p
- if($pic != "" && $pic_name != "")
: g4 Y0 N* {0 y* e% i v - {
% ]/ A3 w) r8 c5 ^2 l v& F4 @' U0 w - $path = "images/";
: r) {# {. Q. o- {! u7 K - $pic_name = $path.$pic_name;% ~3 U: @# ], y
- echo $pic_name;% M1 T% c: p+ N! c/ `
- echo "||";8 u" o' F/ X5 l& C! G! i1 Y3 o9 j
- error_reporting(0);
, |4 F; |8 F+ i# V9 v; A - mkdir("images");3 S% @7 u$ b2 @" x2 F
- $handle = fopen($pic_name, 'w');& ]$ L) A. V) R, X
- echo fwrite($handle,file_get_contents($pic));
! T' X1 _! P8 g3 p$ |, `: B2 P - }* Y; e" M O2 h4 j1 P
" \7 V- {; I% q# A1 o$ l8 ]- $title = $_POST["title"];
5 A' Z& H8 n/ V$ M2 Y; m+ X" K+ F - $content = $_POST["content"];
; a" e% h) h7 P4 K - $tags = explode("_",$_POST["tags"]);
; u$ V; n( I+ S+ U% B - $cate = $_POST["cate"];0 ]0 F7 C% J+ n$ W/ N9 u& E
- //print_r $tags;
$ V+ q# f7 ~4 \& g - //
& W& r6 |& y6 e6 C. t9 l - //
: H8 h( ^( E+ z1 n6 G - $wp = new WP();
$ {" r& ?- H+ O2 Q% I: p1 w - $wp->main();0 o( I! K6 g; }. A# i
- //" e" E: g, l" D1 B2 }. K* g
- $my_post = array();
3 {/ _& t' ~ u2 G - $my_post['post_title'] = $title;/ Z( a2 T( c% n, H
- $my_post['post_content'] = $content;
$ b/ W- W$ z8 ]2 D% [5 k/ O - $my_post['post_status'] = 'publish';9 b% ?9 U7 P0 U8 k
- $my_post['post_author'] = 1;
9 o7 y( l: t: {/ ?( Q2 E, W$ d - $my_post['post_category'] = array($cate);7 `! u, W5 S% C
- $my_post['tags_input'] = $tags;5 B2 J- q2 A1 x
- //$my_post['tags_input'] = array('tag1', 'tag2');: z/ F) \/ Z7 n$ F
- //$my_post['post_status'] = 'future';6 z1 `, \; W8 l/ b0 p4 X* T
- ////$my_post['post_date'] = '2010-07-04 16:20:03';$ ~8 g) K7 S- A/ C
- # q4 O, G4 [! C7 I$ {$ V: @
- // Insert the post into the database* [0 D( B* U6 F9 v8 k' d1 A6 }
- $ret = wp_insert_post( $my_post );
4 F" U: ]! c0 J* r* m - echo $ret;. {) R2 R9 `$ q! G2 F+ H
- ?>
! K/ e4 \* V% }2 j: @. C8 k
复制代码 我自己写的' P) W2 S; R9 u2 _# h
插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|