|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑
6 C1 k% |2 l, Z4 y6 k8 _
$ ]9 ^& H2 h7 F. G: S0 b2 b直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持, p; y1 u. S$ P0 p( A. C( O
4 ~' z+ Q) G: h( y! {3 N! F- L我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了
8 j& a) I- z# S
% O; y3 r( I1 C7 o, W. k方便实用 简单 何乐而不用呢- % p' \5 S0 a6 @) L! G
- <?php
. C) k8 Q+ W6 o3 i. G - 5 ~# \: r' u- F$ r# r: ~
- define( 'ABSPATH', dirname(__FILE__) . '/' );
+ Y/ S8 x% _% ^0 w4 n1 ] - require_once( ABSPATH . 'wp-config.php' );
8 b) O6 M' Q2 v- _; O, v, w) x - require_once( ABSPATH . 'wp-settings.php' );
6 }7 H* m; H& t; f - require_once( ABSPATH . 'wp-includes/class-wp.php' );
0 N: s. E4 L# ?7 m5 R- T& t - require_once( ABSPATH . 'wp-includes/functions.php' );
6 o! Q/ i1 w* O% K - require_once( ABSPATH . 'wp-includes/plugin.php' );
) X7 X ]8 K; }- c - $title = $_POST["title"];
. a0 {9 l) M0 D% s$ E - $content = $_POST["content"];
0 S- I' j1 q' O/ i. N8 R( f" ^ - $tags = explode("_",$_POST["tags"]);* c" V3 R- ~/ |- y1 l9 ^3 W: I9 N
- $cate = $_POST["cate"];8 j, ] F8 x X& r, N4 u
- //print_r $tags;
6 V& m, c" p6 A" o+ ^5 Y% W& b - //4 K: v2 |4 \8 S1 K# S4 d4 b
- //
% R6 f; @. Y9 e - $wp = new WP();' ?5 S) B/ r8 U! ^" ~5 {
- $wp->main();
8 x- g: R. Z" x1 |# M - //( Q2 v: [/ y6 l' P% V
- $my_post = array();6 t+ [; I _; {( L) @: s6 y
- $my_post['post_title'] = $title;
$ J$ O" B; K' ` - $my_post['post_content'] = $content;9 P1 F( \/ k2 p1 n5 u
- $my_post['post_status'] = 'publish';
( b- H7 c( r, E: W1 j6 U - $my_post['post_author'] = 1; ^0 U7 [" Q+ N
- $my_post['post_category'] = array($cate);1 H3 r& A: |7 c# W, }" {
- $my_post['tags_input'] = $tags;
2 z1 A! J4 Q% U: S - //$my_post['tags_input'] = array('tag1', 'tag2');3 f# L: K" E6 p! h6 b: _
- //$my_post['post_status'] = 'future';( I% e5 Z- P1 X* c6 W- ^
- ////$my_post['post_date'] = '2010-07-04 16:20:03';
8 v& s8 q# Y: q" S/ i - 3 i. |: ?0 m* B: C
- // Insert the post into the database
% f2 o" |( ^$ M, q& }2 ^ - $ret = wp_insert_post( $my_post );
0 f0 a& t8 S6 J8 @' c% u5 P1 H" _ - echo $ret;
, a$ i: V/ d8 B- O2 y3 Q* _9 ] - ?>1 N; ]6 m! ~5 @4 }# o9 e
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气
; R* n- }# ?! `5 S- I; ?1 S- <?php# _4 Q8 L6 z6 M: M/ A
- define( 'ABSPATH', dirname(__FILE__) . '/' );
4 o: o' r' c' {4 ~! ^) y- q3 f; _" R - require_once( ABSPATH . 'wp-config.php' );& z, q" H; D+ P- _3 O; `
- require_once( ABSPATH . 'wp-settings.php' );
: Q3 V' B5 x, w9 a$ G7 E# ]& I - require_once( ABSPATH . 'wp-includes/class-wp.php' );) Z1 T9 ?. v. k6 l6 t7 P* U1 o6 I8 g
- require_once( ABSPATH . 'wp-includes/functions.php' );
; N4 v8 ^3 g: U' E$ h% @ - require_once( ABSPATH . 'wp-includes/plugin.php' );/ V5 X! B0 L- y& f1 o3 ~
- $pic_name = $_POST["pic_name"];
# N6 p8 ]! ~ O. S8 I, {+ k7 E - $pic = $_POST["pic"];& f5 i; \* A2 X
- if($pic != "" && $pic_name != "")
# W5 l5 u+ `6 N6 J - {4 s% A- L" y( |" `0 y- N \
- $path = "images/";
$ ]. Y) O8 Z/ B( V9 B6 n - $pic_name = $path.$pic_name;- o9 N& X8 q9 J! L
- echo $pic_name;( W6 K) {0 ]/ \: l8 }( W0 v
- echo "||";8 u+ j4 F; X' h
- error_reporting(0);
7 r3 x, B' {& p" I! k0 e" v! W' W - mkdir("images");
: O1 B. b6 l6 h, P6 K- i - $handle = fopen($pic_name, 'w');
, M; ?/ ]- W1 {8 r1 ?; n, T - echo fwrite($handle,file_get_contents($pic));
' S A* b" c$ N4 [& y; i - }
1 J7 k5 p9 t; |4 k [8 H7 v+ [ - c! z" ?0 y- o5 `. Y2 K
- $title = $_POST["title"];
. J% U% ~9 \9 C: G3 Z2 \ - $content = $_POST["content"];& U5 r1 d- A3 Y8 c I! ?" t
- $tags = explode("_",$_POST["tags"]);
6 l3 H0 J$ S( D/ P% B - $cate = $_POST["cate"];
0 U- u& b, ]$ L3 S' B - //print_r $tags;; c% G$ h" d6 d0 o% \, z8 a
- //
& _) X2 J' p% z ]4 N6 A - //# _- N/ P9 `4 \& Y4 n) }( X
- $wp = new WP();
+ N8 N, [8 m2 y/ B, I) K - $wp->main();1 l' ~- i2 J' @5 \0 b
- //, w6 z y6 G" Q: X, u& H2 d" \
- $my_post = array();- B! Y+ o4 T/ M! P3 d6 H; R% i
- $my_post['post_title'] = $title;
0 A# A1 F3 g9 a8 i: Z, I0 x1 s - $my_post['post_content'] = $content;: N# w! b3 q; E
- $my_post['post_status'] = 'publish';
+ k M& y5 k8 |* d - $my_post['post_author'] = 1;0 j- w* ~# s" z9 j3 K& O d
- $my_post['post_category'] = array($cate);
: l: d- y, H4 l! R9 G N+ L - $my_post['tags_input'] = $tags;
( M9 w: [8 v2 F7 l6 K& `7 J' S - //$my_post['tags_input'] = array('tag1', 'tag2');! y9 h, J4 e2 a# H$ b/ m8 ~" g
- //$my_post['post_status'] = 'future';
3 X7 v' u) v R/ [5 y - ////$my_post['post_date'] = '2010-07-04 16:20:03';5 m/ t9 o/ @% l6 H
- . ~3 g3 S g2 ~; ^5 E0 A0 B
- // Insert the post into the database
. J3 N+ ]- O2 s& _' d - $ret = wp_insert_post( $my_post );# Y% X. _* F3 J" {7 D+ A
- echo $ret;0 h2 k( }% c5 U7 e- ]8 c; [
- ?>
& ]% U$ I1 I- S- l
复制代码 我自己写的8 |; v% Q1 Q! ^! B# C2 w( K
插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|