|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑 " b( {* P$ l4 j) v% R
) q3 O3 ^8 E; i直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持
+ O' T# `. N9 `- G9 A( f$ d/ r/ D- d2 m5 X7 c a5 A3 v
我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了
# I' v* p$ P8 o! d0 R1 e( o
* U/ @: |! S% p j方便实用 简单 何乐而不用呢- - M3 ?+ x8 x. C, B
- <?php
' C- u3 r1 `! l) M
& S3 h4 P$ H( G4 ?2 @ \0 J9 g. ?- define( 'ABSPATH', dirname(__FILE__) . '/' );
- V& j! t0 l; c% k1 v) \" ~# t X - require_once( ABSPATH . 'wp-config.php' );
( R6 i( K- q! f - require_once( ABSPATH . 'wp-settings.php' );6 H8 N7 Z8 K) v8 r. O+ Q9 B1 V6 x) j
- require_once( ABSPATH . 'wp-includes/class-wp.php' );! p! o! D# O/ J% z/ P
- require_once( ABSPATH . 'wp-includes/functions.php' );
. Y L, G: i6 @2 B, Z8 x/ k, \ - require_once( ABSPATH . 'wp-includes/plugin.php' );
; D& J* P n. Y: t( e - $title = $_POST["title"];/ j0 t+ W! T/ H- {9 y
- $content = $_POST["content"];
@$ S1 |0 g. k+ D5 d2 k - $tags = explode("_",$_POST["tags"]);3 T" T4 a9 ^7 K8 ~, Y1 M: H" _" x
- $cate = $_POST["cate"];
( ], { A' |& L2 l) j - //print_r $tags;
5 R( N/ L8 n4 g; W; ] - //
) R& E( }: p. x" H+ g% \5 s) ?5 E$ h - //
# | w v6 \, \0 B5 H- O - $wp = new WP();
1 ~: y' B6 h1 a+ c4 e+ n' G q - $wp->main();0 Q8 g0 D( |6 W, E5 B6 l& q
- //' M- d2 T) K0 O" V/ }$ S
- $my_post = array();
) _+ h6 ~4 o8 k - $my_post['post_title'] = $title;
) \+ f& ?2 q) k) J. p+ ] - $my_post['post_content'] = $content;
- e3 N# E }# ~0 P8 ~+ x; C - $my_post['post_status'] = 'publish';
' i9 {* _: |3 I3 L% D& h/ d - $my_post['post_author'] = 1;
6 _1 L3 T- { Z - $my_post['post_category'] = array($cate); f; q( l; v) R' I& l' R
- $my_post['tags_input'] = $tags;
5 `$ a; x/ F1 D, d - //$my_post['tags_input'] = array('tag1', 'tag2');
$ m) C0 R# j7 X7 D - //$my_post['post_status'] = 'future';
: e' i( W2 y/ Z/ _3 p' [ - ////$my_post['post_date'] = '2010-07-04 16:20:03';
% }5 t! T/ n" v8 G: l! B9 S, D" D
! q! y' G% ~; l3 p, ^- // Insert the post into the database
0 q' |- {/ O8 X) o& ~' K3 L - $ret = wp_insert_post( $my_post );
5 S" o# F8 V; B - echo $ret;
" `2 J" U# @% { - ?>
& x- k$ a$ s' o' z" V2 m9 K
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气- / ^9 F' p; @! `( | ^
- <?php; Q0 T$ B& k# U8 z* T+ b9 \9 y
- define( 'ABSPATH', dirname(__FILE__) . '/' );6 M) Z3 C: x* k
- require_once( ABSPATH . 'wp-config.php' );
! q1 R+ J8 E; m; J2 Q6 u - require_once( ABSPATH . 'wp-settings.php' );
. U; {# E" h" n6 D6 | - require_once( ABSPATH . 'wp-includes/class-wp.php' );3 E3 Z% u% j0 {$ j6 ]* \8 k
- require_once( ABSPATH . 'wp-includes/functions.php' );+ [1 @$ n$ o, T4 b
- require_once( ABSPATH . 'wp-includes/plugin.php' );
3 t8 c4 n# x/ W, }$ D - $pic_name = $_POST["pic_name"];, v6 A! ^, |0 ?- c% Y3 j8 P! @5 F- b
- $pic = $_POST["pic"];
8 E& \) F) M5 r5 ^' F9 J& n4 K0 ^ - if($pic != "" && $pic_name != "")9 H, X! V3 z! E% S" l
- {. f3 X8 A) ^: _/ ]% I
- $path = "images/";
9 t9 X2 o# r* n! O( G - $pic_name = $path.$pic_name;( s$ }/ T2 A9 d" q W( d( u+ F% J
- echo $pic_name; g9 }: _* P( j, V* x8 @
- echo "||";
# [) T# {+ W- F" Y2 Q9 } - error_reporting(0);. x: w' Q3 Z0 p8 S7 o! o6 g. {* J1 a
- mkdir("images");
; J$ e/ k. ?" z; k+ g+ j; r - $handle = fopen($pic_name, 'w');7 O) h% V( q( t' V2 N
- echo fwrite($handle,file_get_contents($pic));
4 n# c. `# \$ R" H8 E0 J4 l - }
7 `+ s0 h$ l+ H R
4 R" [0 P& U& N# }. t- $title = $_POST["title"];+ g0 g, h- j' \; n: _% v
- $content = $_POST["content"];
/ ]6 |8 T; S& o - $tags = explode("_",$_POST["tags"]);! N3 i! z0 p+ D2 ?
- $cate = $_POST["cate"];7 m8 [; A$ c7 f. f/ y
- //print_r $tags;3 W. q: ~, s. A( d
- //( G2 F: E/ X! t/ {5 o: S: D
- //
' J9 @& W7 ^5 S* a- R - $wp = new WP();2 T6 A) D4 m! ]
- $wp->main();
$ B/ k" M9 w5 x. Q" v( ?- }) H$ M# D8 Y, z - //4 C3 F. C5 D# y
- $my_post = array();* ^# Y* v: Y8 O" X
- $my_post['post_title'] = $title;
/ }7 r$ s1 s g4 A0 L, _% H - $my_post['post_content'] = $content;5 z0 q, c ?* \4 a, b7 x" B6 `
- $my_post['post_status'] = 'publish';
3 d$ @8 G' w& }& e5 T/ b; S0 L - $my_post['post_author'] = 1;
5 G( c# _, Q2 s8 r$ e. o( A - $my_post['post_category'] = array($cate);0 c8 q9 [- v0 q% a
- $my_post['tags_input'] = $tags;; C5 s* p0 h: w& _
- //$my_post['tags_input'] = array('tag1', 'tag2');/ y1 t( o# _* `. }* w5 ^
- //$my_post['post_status'] = 'future';, U+ C4 W+ j2 |, ?3 L
- ////$my_post['post_date'] = '2010-07-04 16:20:03';
. e" e# d. S& [
' ^( e; ?- \; C' K: e/ t( K6 W- // Insert the post into the database' R3 u' a. I$ A9 w( i* U
- $ret = wp_insert_post( $my_post );9 r* N# k, q& B! ~$ l
- echo $ret;
7 U# ?2 ?8 m7 b/ J3 w. v9 u, X - ?>
. z1 G R5 Y) C' W# s4 t8 _5 c
复制代码 我自己写的. m9 ]8 L- ]" J7 h8 d$ v- H! q! I
插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|