|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑
) x: q; L5 a, |3 f, ~5 j' l* o
% y# ?: u- v/ p& G+ A直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持! ^( o5 t6 m% O' b
5 C o4 X5 A% W0 z# X我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了
% C2 L0 I! n2 ~0 ~7 z. d _# h9 v' o* x# ^* K
方便实用 简单 何乐而不用呢- ' q7 A; |. u* A6 j: H0 f' r" c0 B
- <?php" O/ T1 y5 l& I& z( e
- $ c" M, [ C! \4 F2 l
- define( 'ABSPATH', dirname(__FILE__) . '/' );
" G, J$ J, {/ y: k- f* r3 L& F - require_once( ABSPATH . 'wp-config.php' );
, c$ | j; R# E c" n2 X - require_once( ABSPATH . 'wp-settings.php' );* [& D* _; h& E( K& {+ ~( B( n# Y
- require_once( ABSPATH . 'wp-includes/class-wp.php' );
( A8 G+ @2 q9 S' W - require_once( ABSPATH . 'wp-includes/functions.php' );2 j+ v" e- l+ k; g# a% p
- require_once( ABSPATH . 'wp-includes/plugin.php' );
6 z1 t2 l2 [. N$ p4 N9 ` - $title = $_POST["title"];
, s" o+ T+ c/ h( q. C) m - $content = $_POST["content"];6 ~, [/ D+ J. ?
- $tags = explode("_",$_POST["tags"]);
, `2 ~$ J0 C( M; b5 t4 V% B - $cate = $_POST["cate"];
( D# D; W" v) L8 a$ U - //print_r $tags;8 i2 g N6 M) y* ^
- //& C+ Z% s3 A4 V i j- Y% X8 ?
- //3 d1 u6 ^( r9 [ U
- $wp = new WP();7 d+ r9 }6 T4 W1 P# `8 l1 e
- $wp->main();
Z2 a; d/ B5 S$ O; m - //
! C. E. |# p& C6 O8 e - $my_post = array(); P% F' l& @: z2 i" p
- $my_post['post_title'] = $title;3 b& Y% _5 Y4 M
- $my_post['post_content'] = $content;
9 b# B9 S3 A# Z% a; _7 S - $my_post['post_status'] = 'publish';% ^( d6 P" L. t
- $my_post['post_author'] = 1;
7 e! a& {# g" u9 {% t: V* a Z - $my_post['post_category'] = array($cate);
( O# s+ O) O0 r( [- L/ u6 j - $my_post['tags_input'] = $tags;
% C! V0 k. H& r- r8 C - //$my_post['tags_input'] = array('tag1', 'tag2');8 ~& O, c7 ~; a: U/ Y
- //$my_post['post_status'] = 'future';/ C- } M" H$ G. Y4 d
- ////$my_post['post_date'] = '2010-07-04 16:20:03';
0 b7 W2 I* I+ i! p: ?' c+ ` - ' L! F+ x3 v# |! V9 b! C% }1 K: W
- // Insert the post into the database9 ]- G1 j8 t( a s' ^5 ?0 i
- $ret = wp_insert_post( $my_post );+ R' N5 x5 c& \9 M
- echo $ret;3 U6 r. W1 X+ z, t& c& z
- ?>
! @$ z: A$ p/ E# u1 Z+ W& ^9 h
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气
* `; d( ?/ V. M" L9 R7 Y& a5 H- k- <?php
# `, R B$ k9 k# ^) p9 R& y - define( 'ABSPATH', dirname(__FILE__) . '/' );
) X; w# {: |1 I6 y7 p4 d, { - require_once( ABSPATH . 'wp-config.php' );9 I! h- o1 {- T7 }/ c* c: u7 T
- require_once( ABSPATH . 'wp-settings.php' );
; |1 }2 v0 m7 g; t9 r# P$ u; z - require_once( ABSPATH . 'wp-includes/class-wp.php' );
0 x O' C0 H4 \ J2 r/ q - require_once( ABSPATH . 'wp-includes/functions.php' );9 i# |7 V' G& g8 Z R% K
- require_once( ABSPATH . 'wp-includes/plugin.php' );
4 S6 S6 I1 a+ |1 Y4 r - $pic_name = $_POST["pic_name"];1 u, m4 l `) k
- $pic = $_POST["pic"];9 k% N* A8 B* ]. H$ Q# `9 a9 Q! u
- if($pic != "" && $pic_name != "")' n1 n( Y) T+ Z, f; ]* C
- {/ E2 } T0 A( \' U( g
- $path = "images/";. `- r J" I( ]6 s$ Z# J' J
- $pic_name = $path.$pic_name;, c+ O* U/ z" i& G
- echo $pic_name;
- G( z8 @, p/ e- {7 s9 H* J - echo "||";
( {9 {$ U- s, H+ { - error_reporting(0);
2 Q4 P" m% t* {; A" S# ^ - mkdir("images");: \( y$ R- Q O j0 d5 c1 b/ x: q
- $handle = fopen($pic_name, 'w');
; S% q$ s, z- \* L( x) Q$ |9 l - echo fwrite($handle,file_get_contents($pic));. M7 F7 b5 i; H
- }
+ W) X/ j( d/ G/ e6 d( S
+ P y; y0 J' P4 F) w/ K) K2 C- $title = $_POST["title"];; Z7 h1 j& F$ ?+ I% G. j( n2 D# z
- $content = $_POST["content"];( V* n8 i' T8 P8 ]2 i
- $tags = explode("_",$_POST["tags"]);
7 ^) X/ T/ l. n: f+ X% f - $cate = $_POST["cate"];
4 h! Q, K+ D7 G - //print_r $tags;
6 v8 I( m7 ~7 w O' C$ u0 q9 J - //
- C* {. q! L6 A/ {, D8 Q) q. j! J - //
: i- ?1 u8 K0 V# A - $wp = new WP(); O3 g' N. z1 \1 ]. F$ t2 b
- $wp->main();
9 D! T, E( P1 z - //' ~; s8 n2 I& }+ [$ ]$ [1 L
- $my_post = array();3 |! l% Q! e+ J' S
- $my_post['post_title'] = $title;
6 W+ m( T/ f, t! T" e3 ^ - $my_post['post_content'] = $content;3 c! @; x/ C+ L' f3 a% s5 u; g
- $my_post['post_status'] = 'publish';
4 C1 U8 p" `( ^% l I6 A# ^ - $my_post['post_author'] = 1;
j: [7 L! O! g( r3 V; a9 X - $my_post['post_category'] = array($cate);
2 z/ W: V5 w( Y. ~ - $my_post['tags_input'] = $tags;
, V3 X3 E# {! a9 B3 N - //$my_post['tags_input'] = array('tag1', 'tag2');8 q p' y. S3 p) v9 V9 M
- //$my_post['post_status'] = 'future';
3 C6 s! t2 R" x/ I- r1 _6 R% G - ////$my_post['post_date'] = '2010-07-04 16:20:03';
- P$ w2 g% }9 ^$ x+ z - ) d" ~8 G, x, {: D' ^8 g j
- // Insert the post into the database- \; T) x& V5 K8 X% E5 Y1 l3 Z6 r
- $ret = wp_insert_post( $my_post );
, `% y0 d, j: `5 T# e - echo $ret;
5 c1 o. n2 R3 R* v3 n - ?>; m" V# Y: L- [; Z( S* ^% f
复制代码 我自己写的
' \" P7 @2 F, [" }% K' F: o插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|