|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑 # z( o+ I! x. u- U- q8 J
' |& V d' c3 o直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持& c. M3 Q0 L* R( J
5 a1 y( S9 ?5 Q1 Y) x2 g. y, _
我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了& i: k9 Y: \- J
( G/ M* b! H$ F& s
方便实用 简单 何乐而不用呢- ; @5 }- z- X |7 s& r& t
- <?php
; |. t; k) Y5 d' h! C1 Y6 O( t
1 O1 |6 U) b! ~9 [7 B6 S9 j- define( 'ABSPATH', dirname(__FILE__) . '/' );
- }2 \+ s2 a$ K5 z. m - require_once( ABSPATH . 'wp-config.php' );
' }) y# K: q N$ x. w! J5 ]. k( B4 P - require_once( ABSPATH . 'wp-settings.php' );, c5 M% \- q- `: A0 [9 r% b
- require_once( ABSPATH . 'wp-includes/class-wp.php' );6 f! r+ V6 `4 d( g
- require_once( ABSPATH . 'wp-includes/functions.php' );
( A5 ]' Y x+ P$ x0 ?. n - require_once( ABSPATH . 'wp-includes/plugin.php' );
0 r* j. O3 |% [ - $title = $_POST["title"];5 h0 Y, ~% `( @: r+ O; y3 W
- $content = $_POST["content"];
# x+ T# l& E" u' z1 z, j j - $tags = explode("_",$_POST["tags"]);% _7 L- b- S) s/ L
- $cate = $_POST["cate"];) v% w( ?3 ?5 q' @$ S9 n0 v* [/ V
- //print_r $tags;+ c& D3 c/ \" H9 S$ q* z: ~0 I, F$ Z& w
- //
2 m( B( N- K/ [6 N* Z. J( g+ {4 ` - //) T$ G' ~9 `7 R4 L1 U8 J: R
- $wp = new WP();
) ^/ e) P- [, M; K; G1 O+ B% p - $wp->main();
{0 x- M; X. [8 j' ^( b+ | - //
" h5 q& L: l6 @8 s - $my_post = array();
6 b7 T b- G5 M - $my_post['post_title'] = $title;
& v- f/ P) C/ _3 ]8 m/ G$ W% i! P - $my_post['post_content'] = $content;
/ |8 o G0 l8 p$ l0 G. K2 X# ?% N: X - $my_post['post_status'] = 'publish';0 r2 S& h; C1 Z: z& U0 I3 L
- $my_post['post_author'] = 1;" F$ g% B; E# X. Z* u$ _
- $my_post['post_category'] = array($cate);
% S0 K* a2 t% R4 n6 U- g7 E( x7 r# T - $my_post['tags_input'] = $tags;
. G3 u% X* B/ d; }0 g6 K4 X - //$my_post['tags_input'] = array('tag1', 'tag2');
" y1 h0 c0 }+ n" n - //$my_post['post_status'] = 'future';& k, u* i7 t9 ^2 S8 Q
- ////$my_post['post_date'] = '2010-07-04 16:20:03';
/ J- s; }. ?$ q - $ _! g' I& ]$ _. x0 y7 G1 }
- // Insert the post into the database
5 E9 G0 l' p- k3 d& G2 s - $ret = wp_insert_post( $my_post );
/ w5 B) A8 l; b- S4 C' J; X/ ] - echo $ret;
! V, g R6 p' d; l - ?>. V2 ?3 R [0 d( e6 s* {
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气- ) t" x5 k2 w/ S
- <?php
7 r* n" N; i) c6 a" m - define( 'ABSPATH', dirname(__FILE__) . '/' );
" w) p4 F, s1 Z6 W: M - require_once( ABSPATH . 'wp-config.php' );
2 l4 x' S8 w( W: G2 F - require_once( ABSPATH . 'wp-settings.php' );
* Z* Y4 l% B/ @8 H+ A+ R7 t - require_once( ABSPATH . 'wp-includes/class-wp.php' );6 F! s& n' H: g# ]6 y9 @
- require_once( ABSPATH . 'wp-includes/functions.php' );( m$ s' a0 f2 j, ]2 t
- require_once( ABSPATH . 'wp-includes/plugin.php' );
1 T; N' u7 I+ ^: R/ G5 E - $pic_name = $_POST["pic_name"];
& A {# R+ S/ u# l s - $pic = $_POST["pic"];
' ?0 P% w; [/ V% H - if($pic != "" && $pic_name != "")
5 U% O# Q4 A9 T - {2 ]) q- }1 P Z: S" g! K
- $path = "images/";* K, o6 }5 H" `% F4 x& L8 J, D. T2 N
- $pic_name = $path.$pic_name;* I% O: |6 m% T% Q! P. ~9 M
- echo $pic_name;9 h& ~0 n! d: p0 `8 d& [7 @
- echo "||";; b, O# |( ]' D8 x2 X
- error_reporting(0);
$ y4 E; V3 j( n8 M' `5 ] - mkdir("images");
3 u2 ^; B( o U! d) ` ~ - $handle = fopen($pic_name, 'w');
4 @" a% q! v9 w e - echo fwrite($handle,file_get_contents($pic));
- `- W1 a2 I9 V - }
# Z2 d8 l2 O9 M$ q) y
8 R: R X1 R8 f! z" z3 n* n1 z- $title = $_POST["title"];" _+ D" d& w3 _/ A0 ?
- $content = $_POST["content"];0 D g1 D6 _1 p
- $tags = explode("_",$_POST["tags"]);" r. B3 s5 Y# p5 l+ N
- $cate = $_POST["cate"];3 e" i. D8 P( t, O- y8 K
- //print_r $tags;
5 [6 R) p! r- Z$ P) S! G - //
- r1 c* e- M' I* ^% K q - //
- L. k- r7 D. b4 T: ~ - $wp = new WP();
" G9 f1 u% C/ F/ x6 j - $wp->main();
5 W1 O$ ]8 W8 b, O2 M( y' ^/ A7 _; V - //
, M( i: x6 m8 N - $my_post = array();- R# v: o5 ]) f
- $my_post['post_title'] = $title;
. c( M. d5 n% H' Z - $my_post['post_content'] = $content;3 n; B. E3 D, T8 N
- $my_post['post_status'] = 'publish';. y g: e; x; n, K* A
- $my_post['post_author'] = 1;
. p7 {: K. n: g1 j( X' {. Y6 g - $my_post['post_category'] = array($cate);
" O; @( J" `5 b+ j! U c - $my_post['tags_input'] = $tags;
& ?7 l# ?5 q6 ^. {+ `( j0 E/ W - //$my_post['tags_input'] = array('tag1', 'tag2');
3 s- @* n `! o t B2 Z2 p0 w2 X - //$my_post['post_status'] = 'future';$ V: ~4 V, T" j) z; k7 p3 C
- ////$my_post['post_date'] = '2010-07-04 16:20:03';
0 P, u* f" V8 y4 @5 H {7 r
/ f; {7 l. W `8 r; a) Y( Y6 Z- // Insert the post into the database
$ x, d f* c% U" d5 r - $ret = wp_insert_post( $my_post );( H" M1 R- G( T! K
- echo $ret;
$ I: s7 N8 Z- d8 ]3 i1 ?; E, M - ?>
7 w$ y# b' I6 g5 ?' R: B+ u+ g( I
复制代码 我自己写的
% Z9 T% ]' u! \& h! m" V3 v插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|