|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑
) N+ U m7 `, W% X1 Y8 e+ K1 U! w2 ^. @. c1 q! |' z! R
直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持% B/ T& X' D, n7 P9 y. W! H
3 f; E+ X- w" O我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了
$ h) h$ d+ ?; f2 n0 Y) ], V1 E+ D; x+ v, Y. ?% }
方便实用 简单 何乐而不用呢
6 `; W7 j* o$ e/ a7 P+ w" z- <?php I, y/ X+ q2 {
4 ^" _% C5 w/ i) P) l4 J3 n- define( 'ABSPATH', dirname(__FILE__) . '/' );
+ g9 o( p' l( ?: K t4 J - require_once( ABSPATH . 'wp-config.php' );: e/ Y3 `5 J3 }% v& m
- require_once( ABSPATH . 'wp-settings.php' );
: |6 V4 O' S" X8 u - require_once( ABSPATH . 'wp-includes/class-wp.php' );% q2 B* M) @" [2 e% t/ A' e0 L
- require_once( ABSPATH . 'wp-includes/functions.php' );9 h n4 P* h9 S: @
- require_once( ABSPATH . 'wp-includes/plugin.php' );
' o. e/ x* \! [" u- ]8 }/ r) O - $title = $_POST["title"];- ^7 B' z) @+ j1 N, [
- $content = $_POST["content"];( x0 m, {9 C, B
- $tags = explode("_",$_POST["tags"]);& N# I6 h/ N! r N' M
- $cate = $_POST["cate"];# m% b. n* V0 [! S; c% X5 M4 K
- //print_r $tags;
9 ?/ J5 M% z, e( w e - // m+ H' o& X' a/ u* c
- //
2 I; j6 t v; X$ n! q* c! ^ t7 p, h - $wp = new WP();
0 i# _* n4 W) ~ - $wp->main();2 Q: y0 X ]$ _( O. v
- //
" ~3 @* J- ]1 H! m# Z) U7 A( C - $my_post = array();! d& ?3 ^# G0 V: v7 D
- $my_post['post_title'] = $title;' ]4 R- X2 ?" B. p8 F
- $my_post['post_content'] = $content;) z; I$ o' P- H2 b- {; b
- $my_post['post_status'] = 'publish';* B9 T9 T' ]/ R
- $my_post['post_author'] = 1;
! I( f5 a6 ~: a% D - $my_post['post_category'] = array($cate);( u, P! H# P& k) T s
- $my_post['tags_input'] = $tags;
" V' u7 h' Y5 T# Z5 { M - //$my_post['tags_input'] = array('tag1', 'tag2');0 M, J! T6 O+ E& G, ]; i: E1 x
- //$my_post['post_status'] = 'future';: Z( s3 h2 W% \! k: _$ ]; n
- ////$my_post['post_date'] = '2010-07-04 16:20:03';5 b8 a! a$ m/ Q0 b: k0 a l! \
- 2 h1 ?' Z f. K2 F: Q' u
- // Insert the post into the database
1 I8 J$ P9 U0 z3 F" g - $ret = wp_insert_post( $my_post );
+ M9 I3 E, y7 V - echo $ret;3 o$ m! b0 M( t6 v j8 F- E. B+ a* z
- ?>
# V8 V L, B2 Y; U# x
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气- ; u) \( @1 o- g
- <?php
/ j* p* a" P; T- G - define( 'ABSPATH', dirname(__FILE__) . '/' );; h8 E4 v- R0 X- r+ k+ ~9 [, E0 Y
- require_once( ABSPATH . 'wp-config.php' );# O' \: ]+ g* \+ \; k' `
- require_once( ABSPATH . 'wp-settings.php' );
0 c7 h$ B- p: x3 {- \) i - require_once( ABSPATH . 'wp-includes/class-wp.php' );" f, C# `1 g3 O. D! v2 L9 O B
- require_once( ABSPATH . 'wp-includes/functions.php' );
0 H" w$ v6 i3 f% Z; Q - require_once( ABSPATH . 'wp-includes/plugin.php' );
4 o+ t& b3 B8 C5 a( U - $pic_name = $_POST["pic_name"];7 @( E* r# \& t S: i% |
- $pic = $_POST["pic"];
d. J8 c8 m' s - if($pic != "" && $pic_name != "")
$ p$ C. k/ x$ c( o4 f - {
9 F/ D" i" l3 @- R2 S1 }. k$ }& D - $path = "images/";4 |0 R9 b2 Y) L1 C+ B- Z
- $pic_name = $path.$pic_name;
" ~. L* I' y7 q5 i - echo $pic_name;
, d" e/ X. [+ ]& C) N5 S - echo "||";7 S% r: Z: b7 A5 F
- error_reporting(0);
4 d4 P2 T, |9 F$ H; y) K- ]) A - mkdir("images");
1 ~$ {1 r2 D [$ A - $handle = fopen($pic_name, 'w');
0 g+ G, ^. b. {" q7 ? - echo fwrite($handle,file_get_contents($pic));/ G, R H/ R _5 |
- }
% A$ h( ^! t, d# l
. s5 p3 U& x1 K6 q- $title = $_POST["title"];5 V- B; P) O0 F E2 Y
- $content = $_POST["content"];
' d$ y: r* J& B _9 q- K - $tags = explode("_",$_POST["tags"]);0 R+ C4 M! m ?: G: Q4 y! v
- $cate = $_POST["cate"];
/ ~# `; I+ O/ y$ g) {, [, S. A3 k - //print_r $tags;
& j) e8 U- s2 ~. j: \ M) D' H - // M) z L0 A# p2 T) T) n
- //
$ p; [& d; ]. c+ ?4 H - $wp = new WP();
% r2 W' Z. N7 y2 R" e0 L6 q9 } - $wp->main();
) [0 L7 ~/ {8 {! |. K3 H. T - //& \, D$ r, i; N$ A9 K, G" k. d$ q
- $my_post = array();2 d, P& J. h' S7 q1 v/ b
- $my_post['post_title'] = $title;+ L7 w6 z `4 o& _, ^! X- ^
- $my_post['post_content'] = $content;7 o z1 o$ r0 c( e( U
- $my_post['post_status'] = 'publish';' e* {/ f/ U9 c
- $my_post['post_author'] = 1;8 ?5 k0 e( g, l& k) M- B; T; \) d8 c
- $my_post['post_category'] = array($cate);
4 k. Q" w/ i& z. y - $my_post['tags_input'] = $tags;
# m: h8 f' g/ H) g* j$ X: w - //$my_post['tags_input'] = array('tag1', 'tag2');
! h. w ~" l1 R4 V7 x& m4 r - //$my_post['post_status'] = 'future'; |7 M3 G) B4 |) n* c% n& [/ M
- ////$my_post['post_date'] = '2010-07-04 16:20:03';2 }# e/ [! ]3 w4 o I
- 8 t+ z8 y" r' r, Q- }* Y
- // Insert the post into the database7 C9 q! Y: L) R3 h
- $ret = wp_insert_post( $my_post );
# b! h& q2 s* z; d E/ t - echo $ret;
% H4 [9 V! I% r Z1 |6 M3 C - ?>
% k& G/ X% K- t _. Z! z8 Z1 U
复制代码 我自己写的
- v/ v$ y) [+ M5 q插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|