|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑 5 C) T! E+ T4 F6 s$ m3 [3 n% r
- q# T) B$ g1 ?' _3 V( ^直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持* c V) n2 J' J& w, q% u; E
3 \3 }. B( m- U! t( A
我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了
- k4 m. {! a- f; _8 }" B/ o( g/ G
* w) Y/ I9 [% D8 i% |方便实用 简单 何乐而不用呢- + `: G( n: X R) q* I
- <?php" A; z+ X/ J9 b( N5 C
0 @+ N. x: @ I% {& B6 h6 h- define( 'ABSPATH', dirname(__FILE__) . '/' );2 l f& H* {% i* r% d
- require_once( ABSPATH . 'wp-config.php' );, p. U2 A1 e1 t+ P O; r2 e
- require_once( ABSPATH . 'wp-settings.php' );
7 `% \* j4 i9 n. }4 e - require_once( ABSPATH . 'wp-includes/class-wp.php' );2 V& Q1 Q4 y/ i/ C! S
- require_once( ABSPATH . 'wp-includes/functions.php' );8 }8 V* s4 v, Z# o7 B' `3 I
- require_once( ABSPATH . 'wp-includes/plugin.php' );
* r. f, T2 \$ G1 x - $title = $_POST["title"];8 L1 ?! B* @( h( M2 Y: |& K+ U+ N
- $content = $_POST["content"];
# D# L$ E; g& c9 G: w - $tags = explode("_",$_POST["tags"]);
; R6 { j$ f- X$ R( g - $cate = $_POST["cate"];6 O x# t) E) V- M0 H
- //print_r $tags;+ e: ~+ e& S( M2 v$ _# T2 p, h
- //
, L3 W. ~: ]+ ^3 U0 q* G - //+ z9 }# Z$ b5 t2 L$ s7 Y
- $wp = new WP();7 J1 U o/ H" o6 e3 I2 {. j
- $wp->main();1 t3 t. c7 d: _ m2 M' @
- //$ b# c2 y, G4 u/ t, o
- $my_post = array();
7 u: L; u, C5 k. V* b f/ q - $my_post['post_title'] = $title;
! {: n! J* s& w5 P; L - $my_post['post_content'] = $content;" C" G7 c8 B2 Q. E: J
- $my_post['post_status'] = 'publish';
- |& r3 U6 y! x& j; w7 R# J0 j; I$ j - $my_post['post_author'] = 1;
" B: D) W! c5 U, w4 C) O+ Q - $my_post['post_category'] = array($cate);- a* Z# \" o0 {- Q2 A& `
- $my_post['tags_input'] = $tags;
0 { K2 I3 ^& d5 n9 z% x. V - //$my_post['tags_input'] = array('tag1', 'tag2');9 i0 l/ h: Q) j0 }6 n
- //$my_post['post_status'] = 'future';3 L5 y2 l5 G0 k9 Y
- ////$my_post['post_date'] = '2010-07-04 16:20:03';
& ]0 \' s' M9 n- S8 U' L - 6 N( K& T. h8 M9 H4 ~* ~+ R/ J) i
- // Insert the post into the database
+ a$ l3 u! s4 W) `8 q1 I4 v - $ret = wp_insert_post( $my_post );- E# k0 D3 v* o5 X; D" Z/ i7 _+ ]
- echo $ret;8 l, m/ [" m9 w5 G' r
- ?>4 z+ a+ N( f- l* I
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气- 9 ~, K3 A" ~/ @6 \; T1 X" m
- <?php
, k, F# g2 r _: _+ G0 w - define( 'ABSPATH', dirname(__FILE__) . '/' );+ v1 U# y9 ]7 d
- require_once( ABSPATH . 'wp-config.php' );
8 z* g6 X3 l7 y8 L# a! z) \4 n - require_once( ABSPATH . 'wp-settings.php' );
6 ~- i1 h4 y+ \ - require_once( ABSPATH . 'wp-includes/class-wp.php' );
( F% t+ v/ j4 i, [( U5 Q1 U - require_once( ABSPATH . 'wp-includes/functions.php' );- k+ S$ C8 I4 {4 h
- require_once( ABSPATH . 'wp-includes/plugin.php' );/ J. |4 r0 `5 ]) p' e, q
- $pic_name = $_POST["pic_name"];8 F* c" b! _1 p# R
- $pic = $_POST["pic"];
' X @3 i1 \7 C' Z* s8 o2 C* H5 | - if($pic != "" && $pic_name != "")
# Y; Y! {/ D: Q# u - {- P2 v3 @5 l0 S1 Q% t" R. J
- $path = "images/";
( K& A6 x; G$ D9 \! F - $pic_name = $path.$pic_name;
5 `. Y) ^! n6 _0 x% ^ - echo $pic_name;/ M/ g& w( `0 k
- echo "||";
1 J) u( b9 j$ g6 @/ z3 G9 [& n - error_reporting(0);
" L; I6 R/ w m2 o/ r& s% I - mkdir("images");2 Y" Y, p2 S6 I2 A
- $handle = fopen($pic_name, 'w');* ~8 b/ m+ d- _, B2 m8 ]6 M
- echo fwrite($handle,file_get_contents($pic));
% h6 r5 ` Z6 V$ s/ G - }) u( _7 g2 q0 n( y
% @1 R4 ~% @4 M- $title = $_POST["title"];* K* y7 d/ G W
- $content = $_POST["content"];6 ~0 F& V0 h* u
- $tags = explode("_",$_POST["tags"]);
1 H2 [6 B# a. S. V9 }/ `5 ]9 f - $cate = $_POST["cate"];
* ^+ u9 N( V v3 X" C% y' O# B$ `7 H+ r - //print_r $tags;, A+ S9 I' p( P* f" d) S+ `; C& t d
- //1 \8 h# z2 t6 o [4 H
- //! W3 M& T0 m6 ^! w
- $wp = new WP();' ~$ |0 r5 k; o0 |/ N& ]
- $wp->main();$ [$ r( y4 ~# V* R
- //
. U8 L6 x! H6 t/ q9 P - $my_post = array();
! A! n+ m4 M; O, x8 l5 f8 y4 _' ] - $my_post['post_title'] = $title;8 _+ |2 x5 N1 B$ p; G
- $my_post['post_content'] = $content;
& |+ [7 k# ^" S, a - $my_post['post_status'] = 'publish';
- s- ^/ g0 J) x; q6 z - $my_post['post_author'] = 1;
" b/ R2 s. S& d, v( j - $my_post['post_category'] = array($cate);- k4 }) y5 J7 j% u
- $my_post['tags_input'] = $tags;) H) M: O3 L! u
- //$my_post['tags_input'] = array('tag1', 'tag2');
' F1 n: L2 P6 B+ a) q& `. U7 J5 b - //$my_post['post_status'] = 'future';5 g4 b, c' m* L9 b4 l2 I
- ////$my_post['post_date'] = '2010-07-04 16:20:03';
- @' @- i. q0 @
1 t5 m/ b4 O( L- H) l1 q- // Insert the post into the database. [- d5 c( K R
- $ret = wp_insert_post( $my_post ); v' U1 E% i- ]. J
- echo $ret;
- M6 X- m/ O$ ]- j - ?>( j1 {, R. Z) {3 M; y
复制代码 我自己写的
. B- n+ C# p$ a, Q2 ]7 u) H# j2 E插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|