|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑
2 W& b- G2 x" P7 t/ e
6 e5 [4 Q. t7 w. {% H6 ^! n! {直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持/ f% k$ I' ?- F" n6 t
p% V* c: }. A% d7 ]$ {, U2 d D; C我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了6 {" l& c9 A' a" p2 {! F! Y2 c
9 d$ `/ }: P7 b6 f/ I方便实用 简单 何乐而不用呢- ! C% m/ ^4 e0 X, [
- <?php* A4 F3 D" H5 d% P" O+ E3 x- v+ `
; g: l/ a- D% |/ ?' l6 g& g5 y, T$ L- define( 'ABSPATH', dirname(__FILE__) . '/' );
, w$ r7 _4 G! ]# b - require_once( ABSPATH . 'wp-config.php' );
# z8 K1 s6 ]3 b2 ?7 t0 i% A - require_once( ABSPATH . 'wp-settings.php' );" f3 b/ B" N$ J6 p
- require_once( ABSPATH . 'wp-includes/class-wp.php' );7 e( k* U$ [4 V
- require_once( ABSPATH . 'wp-includes/functions.php' );
$ y& ]1 T' w, L9 }" ]6 I" m# C* B - require_once( ABSPATH . 'wp-includes/plugin.php' );
+ H( g5 V% l& L# G - $title = $_POST["title"];
' Y6 i1 T* c r4 p3 \- N: m% ?0 C - $content = $_POST["content"];
4 t# P1 c4 r% A$ R$ _3 k - $tags = explode("_",$_POST["tags"]);
: ~1 ?. Z: y( K+ h - $cate = $_POST["cate"];
/ ~! @) K& J8 P( S - //print_r $tags;/ f" G7 ~# s6 j9 i! d: V
- //
6 x v6 O" V# ? l. q; A% v4 ] - //4 f) D2 A! h) M5 v& }
- $wp = new WP();3 o7 H2 x0 k: \0 b/ m9 z+ j
- $wp->main();) p4 V. I% [. c8 ^" `* `! O1 e2 G
- //
' ?1 h4 |3 \; E+ I - $my_post = array();
7 O |% l6 J8 x M( T) s3 K - $my_post['post_title'] = $title;
7 [$ z% V( C" H m* S - $my_post['post_content'] = $content;
0 D! [; r+ ]( h2 R! i - $my_post['post_status'] = 'publish';1 o/ E7 p8 S- A+ ?4 C7 e
- $my_post['post_author'] = 1;1 V. [3 R- t( I) J
- $my_post['post_category'] = array($cate);% N2 M6 h4 @* ~- y
- $my_post['tags_input'] = $tags;
+ l0 T. A9 n j( d/ w0 F" R9 B - //$my_post['tags_input'] = array('tag1', 'tag2');6 D1 `' C# S/ g% V1 D# F0 r: B
- //$my_post['post_status'] = 'future';3 Q( u8 G$ D( E/ G
- ////$my_post['post_date'] = '2010-07-04 16:20:03';3 |: ]3 I4 z8 R
% k9 C5 c* e0 x' m ^9 u* G- // Insert the post into the database
6 i1 b! }" M6 N6 B" f - $ret = wp_insert_post( $my_post );
, ~, c5 E$ s6 v9 a6 d1 B% w4 Q - echo $ret;' c4 V' H$ B# m6 U' I: q: O/ H- C
- ?>1 }$ ^. N6 Z( R3 E( c8 w7 B( z/ i8 l5 V
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气- ; A- Z. J! s0 D
- <?php
4 E/ y! E, N; ~4 ]+ Y - define( 'ABSPATH', dirname(__FILE__) . '/' );/ t$ O1 [* [5 h4 f6 @0 V
- require_once( ABSPATH . 'wp-config.php' );; H- K8 N a# Z" A1 T% [0 y7 b
- require_once( ABSPATH . 'wp-settings.php' ); n/ n6 `4 u+ c; ^, @. S4 e6 C6 @
- require_once( ABSPATH . 'wp-includes/class-wp.php' );
/ `+ Y& }- _: Q - require_once( ABSPATH . 'wp-includes/functions.php' );* A+ L1 @4 X/ G- @2 i
- require_once( ABSPATH . 'wp-includes/plugin.php' );
0 ]/ ]& G& U( F7 T4 a8 w/ l - $pic_name = $_POST["pic_name"];
4 A9 y; O5 \( @6 g E - $pic = $_POST["pic"];
- K; t& ^: N ]( p5 ~* m - if($pic != "" && $pic_name != "")
8 i4 _5 @# U: \- R$ d# Q2 d9 |0 z - {
7 S% e+ X( ~" L! Y# T; E - $path = "images/"; Q' Z3 ]. X/ s. y8 r! y5 s) m
- $pic_name = $path.$pic_name;
# P4 P9 }1 E5 @2 [5 Z8 o - echo $pic_name;, y" |: \9 w/ ^0 w. ] u9 D% }
- echo "||"; l0 l7 U6 L" y1 [$ E
- error_reporting(0);: V: g) {1 X2 m( ^7 s, K
- mkdir("images");' y2 M' \( \8 v# h# d
- $handle = fopen($pic_name, 'w');
' h5 l6 p- T5 h# P4 e' o - echo fwrite($handle,file_get_contents($pic));/ s+ w: g) H3 k
- }
; l4 U1 M R/ M. A8 M2 B
8 R* V. G& B. V/ [5 ~7 @1 }& y- $title = $_POST["title"];* Z" x! ^: x0 W$ o0 t2 C
- $content = $_POST["content"]; w& O5 G3 e8 G6 m" R; B
- $tags = explode("_",$_POST["tags"]);
; F& r% H/ R0 `: v) |, u7 h - $cate = $_POST["cate"]; I$ C$ n- x& i! j V! l' p2 l
- //print_r $tags;! o- h$ }! p7 Y) M0 [+ ~; P
- //
& t" Q0 ]% t, e - //
' |, x& g1 _% e- X - $wp = new WP();9 ?- F# `8 g* O; ]- ` @
- $wp->main();/ g5 ~: r! r# J7 K( T
- //6 k# g6 @2 S8 v9 q& }
- $my_post = array();
& n3 ~$ I, F1 m- c: g' q - $my_post['post_title'] = $title; e! R" r N: i P
- $my_post['post_content'] = $content;
! n" b- e' y1 k7 z1 N+ }' z - $my_post['post_status'] = 'publish';
( Q+ p' W3 {8 Y' D% V: R. A - $my_post['post_author'] = 1;
7 j2 u2 z4 V$ n% k' | - $my_post['post_category'] = array($cate);, E# T1 a) J# n4 c2 @
- $my_post['tags_input'] = $tags;8 O, e# W0 V- A, ?' W7 B) R" |. u: o
- //$my_post['tags_input'] = array('tag1', 'tag2');, W$ D1 R. k* E1 L
- //$my_post['post_status'] = 'future';
4 W- f( D4 C& E& a9 n+ }1 R - ////$my_post['post_date'] = '2010-07-04 16:20:03';
; Y; Z9 g+ u' U. O
/ {" v# O/ \( {5 t- // Insert the post into the database
8 m7 y$ ]+ p6 E" g - $ret = wp_insert_post( $my_post );
* y0 y) k1 F9 ?& L7 y - echo $ret;
/ c$ o6 X) Y Q - ?>6 A. W' C. _; \, m, _6 w$ v
复制代码 我自己写的& ?5 b0 y- i) l4 k! K2 x
插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|