|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑
2 j8 P H% Z3 T3 z, ?$ o$ n" E! P
4 E: T% j8 P5 e9 I直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持
- f, Z# U, j+ ^8 `: U4 P3 m
8 d, v& P( F) C: e+ A; [$ l6 O我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了
/ @! W& v I9 ]- N* \3 p- C7 N$ D+ {1 P+ Y, F* p
方便实用 简单 何乐而不用呢- ' ]' G! m3 G: O' ~
- <?php
! }, @0 @5 p% ^4 {0 L9 g+ a
6 c ?& N3 i" x- define( 'ABSPATH', dirname(__FILE__) . '/' );
) F) L8 c6 q4 `& p - require_once( ABSPATH . 'wp-config.php' );
4 p1 ^" _. j( U* m) R8 j - require_once( ABSPATH . 'wp-settings.php' );& |( e8 ~% e* [1 n
- require_once( ABSPATH . 'wp-includes/class-wp.php' );4 i5 x: ^- L9 p7 A; |
- require_once( ABSPATH . 'wp-includes/functions.php' );/ l+ q+ L) o- V
- require_once( ABSPATH . 'wp-includes/plugin.php' );
0 B6 S6 g4 m7 T1 W! K3 v3 b - $title = $_POST["title"];
0 o0 R# [* t) U - $content = $_POST["content"];3 [# G3 `( w! H
- $tags = explode("_",$_POST["tags"]);
4 Q! C2 E6 `1 ? - $cate = $_POST["cate"];
) @& ]6 X* v0 H. V* ^! ] - //print_r $tags;
& e- z: k% B$ s! E0 v - //; Q) s- \) f, V- d$ v$ @
- //
0 e0 `/ l' V1 L" |- A- U) B7 p5 a - $wp = new WP();, [: |: n8 y+ ?6 c O
- $wp->main();5 E0 T: j" Y/ T5 i8 P& A: q
- //
( v3 i7 n% o! v& S |) Q - $my_post = array();
$ f9 a; y6 F9 _) Z, d! p6 o - $my_post['post_title'] = $title;9 Z9 Z. ^3 q6 z ]1 }# ~
- $my_post['post_content'] = $content;
+ m5 j. d% `; S; S) N! | - $my_post['post_status'] = 'publish';6 W- M9 E- l" v3 v$ `/ o
- $my_post['post_author'] = 1;
8 P/ l7 o0 \ x1 r7 Y( F - $my_post['post_category'] = array($cate);
5 l. M# ^, J3 r" n0 M9 o - $my_post['tags_input'] = $tags;4 g: i' I# e Q$ c4 L* ~: e( Y& w
- //$my_post['tags_input'] = array('tag1', 'tag2'); D0 C& F! C) j! P( C
- //$my_post['post_status'] = 'future';" H( C% f& V) A, E6 G
- ////$my_post['post_date'] = '2010-07-04 16:20:03';& q8 N) I/ \. X7 o
- % t8 }% V7 r! q: A- F
- // Insert the post into the database
% i1 y! O5 ~7 T+ r: M# k. ^) Y - $ret = wp_insert_post( $my_post );* B3 v& U4 [' B9 v
- echo $ret;1 E0 e4 i0 V2 P
- ?>
$ H2 L6 {% ^& ^- i- y8 P5 \% v
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气
5 j0 s3 W1 e* `. o) o% z3 I- <?php8 W/ r5 C4 |9 z; l' c3 s
- define( 'ABSPATH', dirname(__FILE__) . '/' );
$ p) s7 E L9 E - require_once( ABSPATH . 'wp-config.php' );: j0 W! b) m$ j3 s7 |/ s+ W. f$ J
- require_once( ABSPATH . 'wp-settings.php' );
9 D M, N9 {1 Z, W0 H: I - require_once( ABSPATH . 'wp-includes/class-wp.php' );$ y7 W9 r+ _; Z7 ^9 I9 u {
- require_once( ABSPATH . 'wp-includes/functions.php' );7 j. ?$ g; s5 V1 p& V
- require_once( ABSPATH . 'wp-includes/plugin.php' );* O" k4 U0 S) E- ?
- $pic_name = $_POST["pic_name"];& U/ [/ L8 D& c! T
- $pic = $_POST["pic"];
- k3 h3 i5 o+ X! b - if($pic != "" && $pic_name != "")4 R/ p* z* s1 Z
- {- B0 g' H( a$ B6 v
- $path = "images/";
+ W& v5 `& J5 t, v0 r, v- q: k2 w: { - $pic_name = $path.$pic_name;
9 Z. X: Z3 c8 I' l - echo $pic_name;; M2 j! M$ l2 [5 z1 R
- echo "||";
% O' _$ B9 J& g - error_reporting(0);* H0 |$ ]8 p |$ q+ ~2 B. K
- mkdir("images");
) K2 z# X/ a3 N/ o9 t# K/ h - $handle = fopen($pic_name, 'w');
$ [" a! \8 U I - echo fwrite($handle,file_get_contents($pic));% ?# k5 {% M1 R1 k4 D
- }
% S3 o; X+ z9 z - 3 a4 V# H- s' ?
- $title = $_POST["title"];
5 `" x* r# S% t) {, ` - $content = $_POST["content"];1 G, ~. A4 J6 ^7 _- V, i
- $tags = explode("_",$_POST["tags"]);, F4 S5 A: B. \2 `
- $cate = $_POST["cate"];
1 q. u2 ` {# ]3 G. v - //print_r $tags;8 g. V! k" H+ j2 t" g+ Y3 a
- //
0 Q3 A. O# M$ E$ ^4 D - //
$ {6 M" W% g" W: W8 ^# \7 t) j. @9 W - $wp = new WP();8 [3 J* a0 v6 x% B
- $wp->main();8 E; C$ f# r# H/ Y. E' H1 r
- //
T& t$ A0 n2 k K8 L9 i; K - $my_post = array();6 G7 S( Q; S/ G1 G! T1 h
- $my_post['post_title'] = $title;
+ |1 C+ z1 I; {6 Y2 r$ N% x4 b0 F - $my_post['post_content'] = $content;
[1 H/ l- n5 b! Y6 s, P - $my_post['post_status'] = 'publish';
7 O- L5 T% x& r - $my_post['post_author'] = 1;9 S/ b5 `9 W4 I7 k1 [ u
- $my_post['post_category'] = array($cate);
b. R' L, a% F U: f( G8 {: b2 N, F - $my_post['tags_input'] = $tags;% d& j- |; I4 U, }4 P5 k5 V
- //$my_post['tags_input'] = array('tag1', 'tag2');# [% j( S7 b {- q" \& n6 [( j. p
- //$my_post['post_status'] = 'future';
/ w4 {( f% L4 J6 K8 W - ////$my_post['post_date'] = '2010-07-04 16:20:03';
# v. V7 b1 Y8 h1 R( t0 p* r# e, f
' z' x* L3 M) X- // Insert the post into the database
* L: ?) m. p. Q. s7 D5 h3 ^ - $ret = wp_insert_post( $my_post );
$ u5 r6 U% m/ ?: K - echo $ret;% V' h ~; T' a% }9 u K' U2 ?
- ?>
% W% l+ H- n- g3 j1 [$ a
复制代码 我自己写的
8 b/ D5 K i2 [$ Z# R, s% K% L& M插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|