|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑 3 s# @$ I! Z+ D6 R# v
- q# h. o' k$ G4 F) m! C- P, Q( g7 r; Q
直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持
$ H. v R% N o- w! _7 I& h. { ^3 }1 N3 y3 ^
我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了3 L5 P/ Q1 g4 V
4 q4 K4 h$ L. N) d. ^方便实用 简单 何乐而不用呢- 4 h0 ~$ Q" g7 G3 X% x" }2 b
- <?php
$ @" ^$ e' S; i5 t; z - , n6 A+ b& s! a
- define( 'ABSPATH', dirname(__FILE__) . '/' );/ W$ }9 U1 ], w, o) |5 X
- require_once( ABSPATH . 'wp-config.php' );; ^9 A, d7 V# y' \0 G* ^3 K
- require_once( ABSPATH . 'wp-settings.php' );$ J: r4 P6 A( x7 o2 y1 ^
- require_once( ABSPATH . 'wp-includes/class-wp.php' );; P% K3 O, t+ X# \# f
- require_once( ABSPATH . 'wp-includes/functions.php' );) g0 I. }* Q, E& Z- a, q
- require_once( ABSPATH . 'wp-includes/plugin.php' );% h! h4 C6 K" n9 E5 M. @- i2 D) s
- $title = $_POST["title"];, u6 K! @: y3 j+ r
- $content = $_POST["content"];( Y" I2 L" e+ G, w+ Z$ I( A( H
- $tags = explode("_",$_POST["tags"]);1 n' X' {* x: c+ f& r& }
- $cate = $_POST["cate"];: s2 c5 O( w) r4 ] l. Q+ \7 X8 M* ^
- //print_r $tags;
8 K9 H6 F0 A% r, l2 t( K - //
) d' S ?& r7 v: U$ x$ C4 r6 t, G - //! k# c5 f: @) O+ z! C, v
- $wp = new WP();
' I$ V; `4 I" j g/ A - $wp->main();
' ?, C2 Y, }7 v1 l6 y& l# U4 p - //
( i' p$ t! U+ P; n' n+ F! { - $my_post = array();
, Q* Q. Y8 i/ Q4 u- D - $my_post['post_title'] = $title;$ i" K7 k) N& t3 h1 l
- $my_post['post_content'] = $content;
' ?5 w, o3 G" f G/ L - $my_post['post_status'] = 'publish';
- w- p- ? {4 L2 x# ` - $my_post['post_author'] = 1;
9 k5 {* y: }1 R5 J& c) n - $my_post['post_category'] = array($cate);
+ r, q/ A; ]9 l& U' g4 ]/ C$ T - $my_post['tags_input'] = $tags;
3 x" H3 r) p [2 ?1 b - //$my_post['tags_input'] = array('tag1', 'tag2');/ V. \! v# P; Z
- //$my_post['post_status'] = 'future';
& c+ R7 c, o) l) I - ////$my_post['post_date'] = '2010-07-04 16:20:03';! `/ o( z- E2 ~' _; p; Y. O- p
1 j# J$ n7 R- V: G- // Insert the post into the database# E* s8 ]% d) f* t0 R S8 V
- $ret = wp_insert_post( $my_post );& w4 f/ f% n3 F( Q
- echo $ret;
$ x5 J- t3 Q- b/ }6 B& e - ?>$ k: V0 T% `: s- e2 F
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气- - J0 ]6 x7 c+ }6 D" k1 w+ U! d
- <?php8 a$ Y! ~5 l, k7 W& \1 |9 V* [
- define( 'ABSPATH', dirname(__FILE__) . '/' );
/ G: ]; N! D r# [/ o Z - require_once( ABSPATH . 'wp-config.php' );
5 C) J: ], Q+ i) W$ D - require_once( ABSPATH . 'wp-settings.php' );- D) u, _$ i; p8 _, ?6 S
- require_once( ABSPATH . 'wp-includes/class-wp.php' );
7 c5 G6 ]* c8 H! ]# Z - require_once( ABSPATH . 'wp-includes/functions.php' ); w7 e6 O L9 M0 j/ _9 } Q$ E
- require_once( ABSPATH . 'wp-includes/plugin.php' );: \! g, ~+ }- Z) ^- T3 j# [
- $pic_name = $_POST["pic_name"];( S! E0 D4 c0 U }1 G
- $pic = $_POST["pic"];
4 j) h1 ]7 M/ Q: |/ P6 Z - if($pic != "" && $pic_name != "")
+ X6 l7 {% N% N" o - {: |( Q0 p& z4 Y, D3 j
- $path = "images/";. _0 r' W- q8 `2 G" ]9 w; b6 f
- $pic_name = $path.$pic_name;, v1 K3 B7 w8 q0 @2 c
- echo $pic_name;
1 y& H+ Y7 Q) W. y! ^8 y% ] - echo "||";8 C& U5 O5 T: p* v; d E9 j* V, t
- error_reporting(0);
: j6 M7 d+ X" k( z6 v - mkdir("images");
1 j* M8 u: J, `1 m' @ - $handle = fopen($pic_name, 'w');
: L- m; f0 x- @" p - echo fwrite($handle,file_get_contents($pic));
0 X9 Y: v% m) ?2 N- Z - }
5 E' R9 B1 S7 i* l [ - - f- K, _, f$ m6 C y+ t. T6 x, @
- $title = $_POST["title"];
" }, C9 h$ ?0 { y - $content = $_POST["content"];7 |' {$ k" q% Q1 g% s8 g4 c9 S
- $tags = explode("_",$_POST["tags"]);
$ e7 E, @$ y; j- ?0 z2 D- K - $cate = $_POST["cate"];
( f) q. S) y* r - //print_r $tags; B! O5 E9 q! q( n3 L m4 R! K
- //3 `# s$ e. ?6 c2 L e% g! v1 c
- //% @1 j* \7 Z( a& j
- $wp = new WP();
9 }& E" }8 {! x# ] - $wp->main();
% s, m& N" s: Z# j - //
n5 Y4 h' k+ U/ [' m3 m3 t - $my_post = array();( I+ u8 j& x+ T. P% F& H3 z
- $my_post['post_title'] = $title;% L3 d3 X) ^3 N8 H
- $my_post['post_content'] = $content;$ x" q' H3 I) ~* O+ f) G x- J8 P+ R
- $my_post['post_status'] = 'publish';( J0 @# c6 }. v3 t8 f8 g
- $my_post['post_author'] = 1;
$ V' s, B2 M! @& m - $my_post['post_category'] = array($cate);5 ^) `1 x, T6 z4 \4 k
- $my_post['tags_input'] = $tags;
/ G; k. a; u4 ]( _3 g+ f" ~ - //$my_post['tags_input'] = array('tag1', 'tag2');
9 L! h! p. t/ t8 ^: D5 W - //$my_post['post_status'] = 'future';' X* a+ q+ W( J& R: v( X9 H7 b
- ////$my_post['post_date'] = '2010-07-04 16:20:03';
; B0 H" n3 m. w5 c9 w5 \ - % Q( t' W3 a) H* m
- // Insert the post into the database
7 k& J- }- ]4 E! }8 j - $ret = wp_insert_post( $my_post ); `# }# o q8 t" G
- echo $ret;; k/ ]4 U% T2 \2 F6 Z5 H% V
- ?>- k* m, j8 \) H3 Y' \% _5 g
复制代码 我自己写的
Z4 J+ k$ |4 S, q插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|