|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑
. F9 {! Q+ r, D" s2 g/ x0 N: Q8 r+ @2 p
直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持% P) ]$ b. Z( v2 q6 F1 ^8 A) W
3 X8 ]2 x# V6 } ^% O8 D我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了) k2 j& N% I, F" \: I5 m6 o
! v! g h! k& ?( Q' U2 |方便实用 简单 何乐而不用呢
2 E" R% n( f& p- <?php/ f& t- p2 g4 W5 m1 ?* |9 a: N
- . n( H. k+ ] N7 w! Y
- define( 'ABSPATH', dirname(__FILE__) . '/' );
8 z/ g, b- ]9 l" ~. {& {9 S' q - require_once( ABSPATH . 'wp-config.php' );, f$ y% }- c7 p! W
- require_once( ABSPATH . 'wp-settings.php' );) j# D$ V6 M% w. F0 Q: L
- require_once( ABSPATH . 'wp-includes/class-wp.php' ); f9 O& _. E c) p1 |+ B# H
- require_once( ABSPATH . 'wp-includes/functions.php' );
# K! f% a% ?& t# d% ]. y6 a+ k - require_once( ABSPATH . 'wp-includes/plugin.php' );% g# }9 T1 M3 [4 `4 I
- $title = $_POST["title"];/ G0 n8 a& Y% ^
- $content = $_POST["content"];
( J0 g+ |1 c3 E+ |: M - $tags = explode("_",$_POST["tags"]);2 l4 b# ?3 `( ?, e) n$ |* Z( s
- $cate = $_POST["cate"];4 z- Q, w# W; x* l7 B) }
- //print_r $tags;9 Q/ s% J, \$ j) ^/ O
- //1 O6 w) Y9 Q( n N2 {2 y1 F0 \
- //
/ H+ ?. y4 z; N$ N - $wp = new WP();
3 B& {( L$ K# {/ D' b' P - $wp->main();
, N9 @+ J8 ~4 P/ S8 j K! x: ? - //: l2 Q' x. v# f7 R6 {+ ~
- $my_post = array();
7 O% o- j- {8 E, U2 w - $my_post['post_title'] = $title;
* t; z: a4 ]' A - $my_post['post_content'] = $content;* C$ {2 u+ m! P% r
- $my_post['post_status'] = 'publish';
5 _) u" I# c3 c i a; j y - $my_post['post_author'] = 1;
! o+ A; a6 i' J" z$ H3 l - $my_post['post_category'] = array($cate);( s5 Z# Q5 t" L; x
- $my_post['tags_input'] = $tags;4 Q% g2 P( i, T
- //$my_post['tags_input'] = array('tag1', 'tag2');
0 Y1 M$ ?& v0 h3 v* d* u) n - //$my_post['post_status'] = 'future';
4 l- u4 ?' E2 @9 C4 M( u0 B1 ? - ////$my_post['post_date'] = '2010-07-04 16:20:03';
' k+ T6 N4 Q5 g* j6 X: |' r
- V& l2 x i2 \7 c( e* ]" c7 W- // Insert the post into the database
/ m: t$ A2 J: ^8 x3 |' H - $ret = wp_insert_post( $my_post );2 H3 C* q+ @# O# }1 T1 J+ d
- echo $ret;
+ I6 M- E" I7 m9 M - ?>
9 `/ X" n5 T, |
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气- ' p1 a: X# ]5 G3 k5 G. [% E
- <?php3 i$ w0 l) ?1 r9 C
- define( 'ABSPATH', dirname(__FILE__) . '/' );6 z4 t! ~/ C: i( y" O" n
- require_once( ABSPATH . 'wp-config.php' );
& I0 F0 X/ G. E - require_once( ABSPATH . 'wp-settings.php' );
7 `& @/ h0 G' b5 \ - require_once( ABSPATH . 'wp-includes/class-wp.php' );
- D5 l1 o, h8 E7 S. f0 [. x - require_once( ABSPATH . 'wp-includes/functions.php' );
0 A& ^ N$ N' F0 F+ W - require_once( ABSPATH . 'wp-includes/plugin.php' );
8 I U5 t0 p! b1 @, F/ g - $pic_name = $_POST["pic_name"];
- p5 }5 ]7 g/ K( D - $pic = $_POST["pic"]; t: J- _6 u. o. k5 ^' g) I: H2 v6 W) d
- if($pic != "" && $pic_name != "")+ K1 J/ \3 d) a+ J/ h: T1 Q; H$ R8 F: Z, P
- {/ a |6 E( d$ b1 F. F4 S6 v
- $path = "images/";
, D1 }5 p9 e/ x, n - $pic_name = $path.$pic_name;
% w7 s# j# [& c% l- G. ^ - echo $pic_name;6 L2 C& [# e+ l6 S" r3 ~) G
- echo "||";
7 J/ ]& _$ R( H2 }. l1 I - error_reporting(0);
$ w" S1 z1 V [7 r2 T e z) d - mkdir("images");
; F) H0 b- D+ K U/ X8 M L. e3 x3 ` - $handle = fopen($pic_name, 'w');5 @- H/ q+ `9 @9 S7 z. v+ {" Z* p
- echo fwrite($handle,file_get_contents($pic));
# m5 ^ n$ D! `* o2 O& _! D$ V - }
' F" q: s) ~: I( K# E0 L: [5 H/ A - 7 {' ]# I( C% [8 g+ H: M
- $title = $_POST["title"];- Z3 Q* f3 U: f0 n6 Q# [
- $content = $_POST["content"];* L! I0 `! T- ^. x1 y
- $tags = explode("_",$_POST["tags"]);
! |5 N- n5 @8 {# k, O; s - $cate = $_POST["cate"];
( {# v1 u8 Q3 O) H8 q - //print_r $tags;9 P6 U5 {# V' [$ r
- //
2 A. @6 Q! U# G+ q) L7 x - //' t' M# ^% s: l7 i6 U
- $wp = new WP();" u$ {$ p: O% z0 e
- $wp->main();
) H5 P! R9 \, C7 ?, @# Y - //
# D; }. ~- H. J9 M, ?- l - $my_post = array();
7 H0 ?1 K9 f4 B7 c - $my_post['post_title'] = $title;
7 O$ O% s) x6 `3 L- N - $my_post['post_content'] = $content; S; @ B: X4 {! D* n( P; B0 i
- $my_post['post_status'] = 'publish';$ N* a0 b- A' d2 |' P, K3 w; F
- $my_post['post_author'] = 1;
$ |3 O4 B! @- R* S! b1 D - $my_post['post_category'] = array($cate);
' {: I- C2 b/ B+ y! g, w: g - $my_post['tags_input'] = $tags; C% S" D, d8 g+ v d
- //$my_post['tags_input'] = array('tag1', 'tag2');% B* r: C3 _/ g/ g0 Z
- //$my_post['post_status'] = 'future';
2 I2 J% F3 g1 P5 T; T; ^ - ////$my_post['post_date'] = '2010-07-04 16:20:03';/ Y3 G2 L+ ~- U! |8 w- l# ?2 b
- : X x1 |& F! J* t" _ f) H
- // Insert the post into the database
8 X/ S( Y5 ? q! |+ p) ~, T - $ret = wp_insert_post( $my_post );
5 l, `* A X0 L8 t- ?! Z) A( ] - echo $ret;
i3 _7 Q; S8 {/ s2 {0 A: f$ Y - ?>" s$ J& D* Z% r4 k b1 T) W9 \6 L
复制代码 我自己写的5 [- {: Y1 L$ Q9 g
插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|