|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑
) q% V; M( N ^; [$ N0 b+ s
% n X- x; E- [8 D$ L/ M" e直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持
6 X- L% t$ K/ y* a, p- V4 ?; j+ U
我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了6 M4 n; O) z. C7 D+ }
; Y! N% Z# e8 T* L* K. | _. F& r方便实用 简单 何乐而不用呢
7 k4 _. z/ ]% Y' e s- S- <?php
3 {6 P# c7 x' V6 E$ M. Y
% F7 G2 N( E7 s& Q$ L5 _% ^# [- define( 'ABSPATH', dirname(__FILE__) . '/' );! q4 @7 y5 p% t7 [4 j1 L
- require_once( ABSPATH . 'wp-config.php' );
9 g- n4 h9 Z& v+ p7 i - require_once( ABSPATH . 'wp-settings.php' );
0 m4 P) M4 \& V" T, z0 v: r - require_once( ABSPATH . 'wp-includes/class-wp.php' );5 S+ T6 N- U) F
- require_once( ABSPATH . 'wp-includes/functions.php' );0 p, U4 v# [7 _' J, {! y' b
- require_once( ABSPATH . 'wp-includes/plugin.php' );& y/ J1 i* w, w8 y, S% y3 R
- $title = $_POST["title"];
% B( B; { V, f7 g - $content = $_POST["content"];! ?; P7 @) q& l$ ~
- $tags = explode("_",$_POST["tags"]);
$ L0 H4 E/ F- D" r( T7 u - $cate = $_POST["cate"];
5 F! c& {; U/ ~5 W! _, t5 [ - //print_r $tags;2 I% Z! q; r* |( q5 Y7 w9 [
- //
- h# l1 i9 y9 u% x# T4 ~ - //
( i4 ^, s; B4 y: h+ u- U# j - $wp = new WP();+ Y2 I5 T& V% y% j! \# q
- $wp->main();+ {. v9 d7 `: `' e% L% k
- //
0 l! X0 ^ ]3 l9 L8 V: I! i - $my_post = array();: s/ K0 c* M) C/ ~6 H6 D
- $my_post['post_title'] = $title;
8 i5 C- @ g9 U5 y, a7 E2 @' e# V" R - $my_post['post_content'] = $content;
, E2 M8 c7 H4 j! [9 I - $my_post['post_status'] = 'publish';( h$ J" p. h! Z1 D* a- K( A! w
- $my_post['post_author'] = 1;. z8 a+ k# m& E# `* p
- $my_post['post_category'] = array($cate);; M0 U0 h$ \1 }5 E/ j+ k$ R9 O
- $my_post['tags_input'] = $tags;
9 \- A1 w8 C0 _6 J5 Q6 A - //$my_post['tags_input'] = array('tag1', 'tag2');
( W' [+ [! O6 Z8 T% c5 t u9 D - //$my_post['post_status'] = 'future';& m& p! ~" P' |, B3 \+ \1 a
- ////$my_post['post_date'] = '2010-07-04 16:20:03';
5 w8 t9 }* G4 b4 Z. g* |
( ~ t0 ], N; p6 T$ a2 {% X- // Insert the post into the database
4 j1 K2 C* z w. Z2 f7 Q - $ret = wp_insert_post( $my_post );
) M; s0 p5 v" I0 U$ Y" H+ B! ^ - echo $ret;
" q: o7 m' f4 W) G - ?>( \6 z1 c3 u5 I4 J" E& A
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气- 3 y% v9 m. L5 |0 Q' @7 F. U. ?
- <?php
' A/ p" _0 m M* b - define( 'ABSPATH', dirname(__FILE__) . '/' );! j0 {. e" V2 u7 s6 d4 f# d
- require_once( ABSPATH . 'wp-config.php' );6 g" U9 P9 A# d* B. v8 F) l' F
- require_once( ABSPATH . 'wp-settings.php' );
( j; L* Y7 o7 p/ `: e - require_once( ABSPATH . 'wp-includes/class-wp.php' );+ w' T! ]. r% p; }, B, E
- require_once( ABSPATH . 'wp-includes/functions.php' );' ?/ h7 b4 ?5 ^* r- P3 g
- require_once( ABSPATH . 'wp-includes/plugin.php' );# j7 R$ x5 t8 Y$ d
- $pic_name = $_POST["pic_name"];9 R6 a# Z6 W9 k) Q6 o; _' {1 z; ~, T0 T
- $pic = $_POST["pic"];
0 [+ V; q2 g% j - if($pic != "" && $pic_name != "")% |6 `( |! T/ I
- {* J4 Z' w: ]4 V) U9 Y
- $path = "images/";. _3 @) o5 P$ R7 N x
- $pic_name = $path.$pic_name;
% G4 S' N# r. {- R( Z& C; L - echo $pic_name;. P: N: y- T4 z
- echo "||";" ~; A1 |) [+ S" C9 `
- error_reporting(0);
8 s0 o- e& g; G; K - mkdir("images");
. D+ g* f1 ?! P4 M8 b - $handle = fopen($pic_name, 'w');2 @% x; D, x4 Y9 {# a( q9 y
- echo fwrite($handle,file_get_contents($pic));
4 W. [; ]; I' j/ s: V - }
. [9 E1 w5 h' B F8 F
$ L* Y1 z, Q2 _7 X7 Q) C- $title = $_POST["title"];
7 x e8 x( @2 p. q - $content = $_POST["content"];' @2 b2 `0 ]* H. J! g$ W
- $tags = explode("_",$_POST["tags"]);
% e, V" K* M% I( ^! w - $cate = $_POST["cate"];
8 G8 ?# b- }9 u1 B" y7 j# X - //print_r $tags;
/ u: r% k; H$ }5 A: L3 D - //' W8 \- i. l* m4 s4 P3 |
- //5 s2 D2 a4 m& x* N: H
- $wp = new WP(); q$ U6 K- J* I4 T1 ]% g
- $wp->main();9 ?2 x0 K& K4 x+ _1 O
- //+ a% [% F. Y: l9 ]. `0 m
- $my_post = array();
" \4 _* }5 M- q2 |; m - $my_post['post_title'] = $title;
2 M y. q/ r$ d+ s+ z/ p6 f - $my_post['post_content'] = $content;+ o7 |( Y. V: @& K; B, H
- $my_post['post_status'] = 'publish';
* \. s; J+ ^0 E0 z) a% ` - $my_post['post_author'] = 1;
$ h8 `' }3 T, f( M7 B - $my_post['post_category'] = array($cate);5 z+ r4 o- }- W& `' }3 O$ z1 X* Q
- $my_post['tags_input'] = $tags;
3 Z' q/ m: G7 W* }! i! ^ - //$my_post['tags_input'] = array('tag1', 'tag2');
; Z6 K5 G! |; K0 U' B( }; T - //$my_post['post_status'] = 'future';/ Y+ \0 U: [" O4 w" z! I, i
- ////$my_post['post_date'] = '2010-07-04 16:20:03';
8 h4 _* s( b, k
4 G: F3 u+ W$ Q) Z- // Insert the post into the database3 E# b7 f/ d, u8 j4 o8 b
- $ret = wp_insert_post( $my_post );
# u* _0 B: L/ L! F& e) K# l- i - echo $ret;7 Q: k) ~- L6 h l* M
- ?>
. a0 O/ d( R) `4 J7 M+ }
复制代码 我自己写的& m' Y0 H. m" P( K/ g5 p
插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|