|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑 ; \/ k) Y2 \; U. K. ?, z9 x
) ?7 G u1 P5 @" t! K9 z
直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持1 `: p$ C% A! H. j( X% [9 q
# s+ F/ `$ c0 x$ C1 y9 p: c
我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了' O$ S+ J; m6 [! W# d& q
, X; Z& A4 G0 Z" ?方便实用 简单 何乐而不用呢
0 N4 U- k6 V; o+ B( ?: `- <?php) v$ C. R6 t; m. `
- / B G4 o Z* X4 _5 F5 q; j- Q0 s$ W
- define( 'ABSPATH', dirname(__FILE__) . '/' );
' F: I) O8 x: E! J% _ - require_once( ABSPATH . 'wp-config.php' );# Y7 c% X# Z3 D, I, [1 {: b+ }
- require_once( ABSPATH . 'wp-settings.php' );& F% E: q6 X* D5 F
- require_once( ABSPATH . 'wp-includes/class-wp.php' );
& H, P% a) z2 H0 K2 `) y" { - require_once( ABSPATH . 'wp-includes/functions.php' );
* Q. O1 L6 ?7 L; @- Y a - require_once( ABSPATH . 'wp-includes/plugin.php' );4 W8 ~- V" z. [$ C" F; F) o
- $title = $_POST["title"];8 I$ i+ e/ {6 D2 l' S
- $content = $_POST["content"];2 H; \2 `+ ^5 {/ r( V* N
- $tags = explode("_",$_POST["tags"]);
# g2 H2 _7 b) S- R6 F) ^0 g - $cate = $_POST["cate"];
& [7 K5 n# t' x( Q) _ R* ]# ~5 L - //print_r $tags;! F- L8 A$ U, w+ b
- //
: O8 C3 n' h5 e2 A9 H( ` - //
3 o# n6 U5 M& {$ z - $wp = new WP();
% t' M4 O: k; Z6 ?& Q, P - $wp->main();
7 M, X) h8 l# y; [' v - //4 [- V$ r8 e- p! _
- $my_post = array();
, t& B# a" g4 m) X% E1 i* a8 j - $my_post['post_title'] = $title;% O: P& n) ?# N. i
- $my_post['post_content'] = $content;; K A6 c6 b( {8 C9 P% m
- $my_post['post_status'] = 'publish';
# ~9 f' u) m* z* r* G4 i+ Z - $my_post['post_author'] = 1;* x& P+ G$ l8 U$ Y% T6 G
- $my_post['post_category'] = array($cate);
; z8 J+ x" A' G; P - $my_post['tags_input'] = $tags;* W0 O1 K( Q0 U5 Q! U4 c3 t! V
- //$my_post['tags_input'] = array('tag1', 'tag2');
' e; w4 L0 D: c+ g; ~( ~2 Q+ { - //$my_post['post_status'] = 'future';
, c! u8 z9 o& S$ a- S \ - ////$my_post['post_date'] = '2010-07-04 16:20:03';; R# G! L G+ J5 N- w
$ ~$ t+ T8 Y4 L; H9 T8 V- // Insert the post into the database
1 x% E b1 I5 n2 A - $ret = wp_insert_post( $my_post );1 i8 x9 c3 b, C/ b) X
- echo $ret;
. p7 E1 t. X. }8 U4 p - ?>
$ z9 n3 C% c3 ?# f$ P b
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气
' Z: u- [, p1 J8 @- <?php
+ Y' Z# D5 r/ q5 h# r - define( 'ABSPATH', dirname(__FILE__) . '/' );
: e: N# \: _' a. c8 e - require_once( ABSPATH . 'wp-config.php' );
0 s! o; z1 _1 z9 j. h! F - require_once( ABSPATH . 'wp-settings.php' );
7 W7 h3 E/ j& g& y( @& v _ - require_once( ABSPATH . 'wp-includes/class-wp.php' );8 L. v7 l$ Z7 W. a3 {
- require_once( ABSPATH . 'wp-includes/functions.php' );
5 I3 }; V9 C% X6 G, I - require_once( ABSPATH . 'wp-includes/plugin.php' );
( Y X) |1 X* L - $pic_name = $_POST["pic_name"];
! |% Z7 c+ d, T - $pic = $_POST["pic"];+ U+ n8 _; V- }% Y9 o3 \
- if($pic != "" && $pic_name != "")6 Z2 c% x- H. ]* e$ e
- {3 \' x) O0 \! ?0 W2 J
- $path = "images/";
- j" S0 i5 l6 v) b( s* @& w1 ] - $pic_name = $path.$pic_name;
! w0 I5 b# ?+ L+ e! I3 ?: F& | - echo $pic_name;* J7 C& s: F# D
- echo "||";
) j. u8 a% l& B# K5 ~ - error_reporting(0);! ]" h l% ~) n; G1 J/ X5 j) z K
- mkdir("images");
3 r1 B6 V2 u/ A$ s3 R( e4 ] - $handle = fopen($pic_name, 'w');
1 N: m) c( {, y! } - echo fwrite($handle,file_get_contents($pic));
. c6 p: A& n/ p' [- o/ ] ^ - }
! N$ \" x4 j8 \. N
) s& w. w: n! |0 W" O8 F4 G; C- $title = $_POST["title"];
7 z, m: e; g( N, H5 c: _9 r - $content = $_POST["content"];
0 y* `8 g& j; B! J' U" R! t* L - $tags = explode("_",$_POST["tags"]);5 N& k# ?& H9 ^: n5 p4 x8 i* |' z
- $cate = $_POST["cate"];0 b4 @$ v& D( k8 V* H/ I
- //print_r $tags;+ U; L- O* d# s% ]8 Q5 t" c
- //
- c6 G& A1 |2 `0 x" { w: C7 h - //" J$ `7 ~/ M1 u3 |, }+ z
- $wp = new WP();
* f3 w9 c* J( K8 u. C& q) { - $wp->main();, u+ P2 Y" W3 d: ]* R: h
- //: w0 r. B9 {- ^% ^) Z' l5 V& q
- $my_post = array();7 O1 W" K% M2 |
- $my_post['post_title'] = $title;" `1 ]2 E7 W0 [" Z* d
- $my_post['post_content'] = $content;
$ d' L, b8 ] l - $my_post['post_status'] = 'publish';9 b# N) H+ u! P3 T9 L
- $my_post['post_author'] = 1;
6 Z5 j8 r" w7 A! q) j2 z: { - $my_post['post_category'] = array($cate);, ?3 Y( z; x5 s" E1 d& P9 E$ D) v
- $my_post['tags_input'] = $tags;( r5 G! e! B" z2 y; j( E
- //$my_post['tags_input'] = array('tag1', 'tag2');, h* u0 t8 k0 q0 b: `
- //$my_post['post_status'] = 'future';: B5 w9 f W4 O; z* o- E) E
- ////$my_post['post_date'] = '2010-07-04 16:20:03';
: h; G) k- M n8 f* t$ x, O# e& v
6 Q: [" e8 n2 Q7 l. t- // Insert the post into the database
5 f% E1 T5 w' \: p/ H7 H - $ret = wp_insert_post( $my_post );* N9 w, L% {; _2 X, Y% x
- echo $ret;
4 b+ R: m4 R* l: }2 E% P% q. z. w - ?>
) `+ v3 _3 P8 ^& Y5 L) H5 B
复制代码 我自己写的
p1 J e: j- i- _ |: @5 V插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|