|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑
9 O/ U. b; ?4 [+ G% f+ z7 S1 A3 u2 `+ Z+ {! i
直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持
. K& @1 @+ z) u \0 G! c8 P2 D1 I J. r* a5 G+ B$ Y5 V: z
我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了6 T! \7 l' @ |
# q7 n2 T. J7 Z9 L5 ~1 [; X4 N方便实用 简单 何乐而不用呢
9 ^8 j! ]4 b: Y4 W. C- <?php6 L9 e" l4 s. I
v2 X1 U( h. X: d2 [9 G- define( 'ABSPATH', dirname(__FILE__) . '/' );
8 a# f' u8 t0 o0 U - require_once( ABSPATH . 'wp-config.php' );
! @* `( s. E+ C( Q9 `! |; \4 G - require_once( ABSPATH . 'wp-settings.php' );
/ Z$ W9 h7 d. p) n- L8 {$ J - require_once( ABSPATH . 'wp-includes/class-wp.php' );
G) [5 Z& Q9 b - require_once( ABSPATH . 'wp-includes/functions.php' );
5 ~& N" a$ X8 N - require_once( ABSPATH . 'wp-includes/plugin.php' );
: p: y, p5 A: k$ ?" ?. y+ k# q- u - $title = $_POST["title"];
# m; z0 X* J" u4 J5 Z - $content = $_POST["content"];3 k" M" [" A6 F7 ]8 s1 C
- $tags = explode("_",$_POST["tags"]);
% T% r# I3 p0 e, C, O - $cate = $_POST["cate"];; X h; |3 m* w
- //print_r $tags;
# C3 i; o, R) i4 P A4 D- V3 j: h - //, ~- w! K, Z( U( z( m
- //
7 r$ B4 R7 l2 i5 p0 g, g& S - $wp = new WP();
3 K' t4 B- {+ C0 f+ M% }0 S - $wp->main();) R' z% i- a2 u/ D* E$ q+ @/ V$ k
- //
3 Q9 K" e5 D2 Z% a6 Q7 W - $my_post = array();
$ C) X7 Q3 |: ?7 S& V - $my_post['post_title'] = $title;. j+ B# W8 I+ m& M
- $my_post['post_content'] = $content;
2 r8 Y! O3 f' k- D2 y6 R0 J2 c - $my_post['post_status'] = 'publish';
" f" |" q9 C6 s1 D - $my_post['post_author'] = 1;
# G6 x8 y( G/ _7 d0 C# y' a - $my_post['post_category'] = array($cate);
- i6 N K, U1 V6 k8 X I - $my_post['tags_input'] = $tags;% d# t/ f* z) r
- //$my_post['tags_input'] = array('tag1', 'tag2');' f* l5 E# P- b0 ^. c% `
- //$my_post['post_status'] = 'future';
4 d: d& J0 S% U, f - ////$my_post['post_date'] = '2010-07-04 16:20:03';! O: v1 V) V5 o
- 0 r# S _( ~% T4 M( F& a' M7 x# p
- // Insert the post into the database3 f4 x0 d) K0 p0 |# r& v( n
- $ret = wp_insert_post( $my_post );' ?4 E& E+ o( _' k D
- echo $ret;
& O3 z4 J1 l. |$ b3 C# W - ?>
3 J$ }3 z& z3 Z K5 B4 k
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气- ) g8 ]! H, a" ?* ]8 e0 H
- <?php
+ J0 S; l5 T8 S - define( 'ABSPATH', dirname(__FILE__) . '/' );- K" U1 z$ u1 z3 b
- require_once( ABSPATH . 'wp-config.php' );2 k5 O2 q3 }: L: v4 Y2 h& A# @: q, _
- require_once( ABSPATH . 'wp-settings.php' );
9 t z% T+ h, D+ r) q/ i8 ^: X - require_once( ABSPATH . 'wp-includes/class-wp.php' );
9 d% d8 @& c* B3 n @! K - require_once( ABSPATH . 'wp-includes/functions.php' );3 T; m$ Q0 b7 Y" H+ }
- require_once( ABSPATH . 'wp-includes/plugin.php' );
' e2 C" e4 R: S& m0 C$ { - $pic_name = $_POST["pic_name"];, ^; E1 C$ K8 z2 u# r
- $pic = $_POST["pic"];" S. m- _8 p' e, E% t
- if($pic != "" && $pic_name != "")
3 V, n. w7 V# ~# A - {" }: k* L. a/ C4 r5 w0 o0 V
- $path = "images/";+ z6 h0 B0 R. X! M+ u8 y
- $pic_name = $path.$pic_name;
* m: g0 a1 A- R \3 l7 J* _ - echo $pic_name;2 `( o' w, C( u. `' \
- echo "||";
5 z; z; W" ~( R+ D - error_reporting(0);
3 P6 E8 [% n6 @. {* S' x- E - mkdir("images");
: B z$ O5 ^: n6 J+ z; C3 p) F - $handle = fopen($pic_name, 'w');
! G# s% x+ J( C3 P8 [ - echo fwrite($handle,file_get_contents($pic));
, S# _- M! J$ t9 ?6 w8 B - }& ?" g, X m& E9 a4 k# f
5 B1 s5 d n' h4 ?5 w1 w2 u. ~9 Y- $title = $_POST["title"];& P7 R* _6 \* G/ Y/ G6 _7 H) J' |
- $content = $_POST["content"];
. _$ C x& R: a - $tags = explode("_",$_POST["tags"]);
$ V, }6 ?9 Z, u - $cate = $_POST["cate"];
1 ^& {. u3 ?3 b# V' y - //print_r $tags;
{- H+ W1 C3 R- Y+ d- W8 y - //
& J9 e( m( {" k7 H; S - //
; F$ t& u8 C8 L* c0 q% u9 y - $wp = new WP();0 T2 {& T/ m4 {1 B
- $wp->main();
0 I3 J* `% G/ T0 T: N; G& F - //7 [! f/ Y$ ~ o0 X
- $my_post = array();+ k, `+ C# t1 k( v' b
- $my_post['post_title'] = $title;
6 ^! d: |9 m1 N1 N( P# k - $my_post['post_content'] = $content;
% e9 g z2 H& r$ z9 e S9 P6 o - $my_post['post_status'] = 'publish';
' q/ L$ j* ^5 z& L/ c - $my_post['post_author'] = 1;' g( S& l& |) c0 G, R
- $my_post['post_category'] = array($cate);- J: z, u0 k- Q4 n* g& n4 x# C
- $my_post['tags_input'] = $tags;
0 l' y$ n- ]& O: i - //$my_post['tags_input'] = array('tag1', 'tag2');5 s& v) a3 S3 @# a9 o+ B0 r
- //$my_post['post_status'] = 'future';8 y7 q; N9 F: L- O5 F/ a, B
- ////$my_post['post_date'] = '2010-07-04 16:20:03';+ J3 z- @$ k8 Z S2 s$ [9 o4 n+ I
- ' _% T% Q- t9 O- M& S/ s6 a1 L
- // Insert the post into the database
% y% W$ S/ r- a J4 Q6 U - $ret = wp_insert_post( $my_post );; y2 p$ e* X( o8 {
- echo $ret;
, w3 ~! e3 V; g: F7 s - ?>3 Y5 ?4 H: j. P- C; R
复制代码 我自己写的
2 Z4 r# G2 ~4 L7 f) y; ~. M插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|