|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑 3 D5 x$ a8 X" T4 H0 K
4 K4 q% l% K g4 F+ ?: j8 @8 }$ X+ [直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持# z0 z" j9 o* }- y; ]6 w7 J v& [1 q
* Z9 f& o: l/ U n9 y. _ ?我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了3 R+ i2 I5 b+ G- S8 c. ^" r
5 \0 ? X& J- X( p
方便实用 简单 何乐而不用呢
' B5 E% r* p4 m- <?php
! K1 }5 L/ t' r+ X' H - - B2 C! s# M! ?9 d4 s1 X, X r+ r3 Q
- define( 'ABSPATH', dirname(__FILE__) . '/' );5 {* `- G# V I! C
- require_once( ABSPATH . 'wp-config.php' );
2 X7 H# q7 t" E3 x - require_once( ABSPATH . 'wp-settings.php' );
8 R- W( i i4 u5 z0 P - require_once( ABSPATH . 'wp-includes/class-wp.php' );* w* j% y, `. ^! n
- require_once( ABSPATH . 'wp-includes/functions.php' );
) ~% {7 a0 e) s - require_once( ABSPATH . 'wp-includes/plugin.php' );5 z$ o& o# |- q1 Y- b! Q
- $title = $_POST["title"];
; f p5 O' h( O! C2 e" c - $content = $_POST["content"];8 d9 q d) S/ q' s
- $tags = explode("_",$_POST["tags"]);+ ~0 x" m6 n5 {" \) u l3 R) P
- $cate = $_POST["cate"];9 R* u# x5 F% D3 z; L' b
- //print_r $tags;
7 b9 @ F A/ F; U* h - //
- y) I$ z" ?% Y+ k! A% u5 p - //; _* Q) m4 Y4 h3 C( r9 K% a
- $wp = new WP();/ f) }. F$ J+ L
- $wp->main();
2 y( X0 L0 | o! b6 d - //
# S' M: {3 x% U0 Y - $my_post = array();' X7 @- D) l; p; q/ }/ t4 k
- $my_post['post_title'] = $title;; A; @5 h s% X* x
- $my_post['post_content'] = $content;
1 O9 w3 }6 y8 Q, C- @% x( i1 i: f - $my_post['post_status'] = 'publish';: n2 V( |! S, y& k) G, w2 C
- $my_post['post_author'] = 1;% u0 \% F% B, J
- $my_post['post_category'] = array($cate);) e# l. [4 `. m- C T
- $my_post['tags_input'] = $tags;* [7 l8 O( n/ U
- //$my_post['tags_input'] = array('tag1', 'tag2');* b9 m+ a/ I( A( h
- //$my_post['post_status'] = 'future';
( G, {( M' W7 w4 p- m - ////$my_post['post_date'] = '2010-07-04 16:20:03';$ M+ U' \) v L" _% ^2 y% J+ x: }1 E* O
; C6 T5 ?6 g4 O8 {& x4 H- // Insert the post into the database
8 ~! Y& m4 y1 P% z/ {- I - $ret = wp_insert_post( $my_post );0 q6 m, W# e; x7 m% [
- echo $ret;
/ H9 b, R- o+ [4 f: q" V8 C& q, i- s8 s - ?>
) ~4 ^4 O/ H% j$ \+ d$ M& f p
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气- ' `% |) ]6 S$ \; n8 m. B
- <?php* T( h; S; O W0 E: Z3 Y0 K
- define( 'ABSPATH', dirname(__FILE__) . '/' );$ y. M" J# E8 h7 [- @0 ~* @
- require_once( ABSPATH . 'wp-config.php' );( w& `9 p! `' z4 B* ?% V$ Y* ~
- require_once( ABSPATH . 'wp-settings.php' );
+ w, X! ]; R- H7 a0 Y - require_once( ABSPATH . 'wp-includes/class-wp.php' );4 x: Q. s* U9 S. o6 G
- require_once( ABSPATH . 'wp-includes/functions.php' );8 b. p Z$ I" b! `
- require_once( ABSPATH . 'wp-includes/plugin.php' );! E$ W7 e5 u- g9 g
- $pic_name = $_POST["pic_name"];5 _8 o- F3 l7 Z% F X( _# r
- $pic = $_POST["pic"];
- x5 N+ D }) t8 ? - if($pic != "" && $pic_name != "")
! H' d) d0 D+ c/ U0 W) T - {
- g& u: x+ h9 Z) R W% P - $path = "images/";
" d) B+ D3 s+ l3 V- L0 _) }' k - $pic_name = $path.$pic_name;
# q& ] E" l3 {6 | - echo $pic_name;2 l6 d4 p- v( l7 k" Q( [2 r7 N
- echo "||";6 D( F$ R* b4 x$ u' B
- error_reporting(0);
8 G# D6 ]9 m# d: }6 _ - mkdir("images");
* W8 w9 o/ J: Z* R- ~8 X - $handle = fopen($pic_name, 'w');
1 p; t& ^0 N( |1 q/ L/ t - echo fwrite($handle,file_get_contents($pic));
W X \# i u7 h* T; _ - }* y6 e9 q8 q5 Y8 m$ E8 u
. k' ^4 {7 M# e C+ S5 q! x5 \- $title = $_POST["title"];
4 B& G1 i: U+ A. i2 {8 \ Z- x - $content = $_POST["content"];/ T5 e6 p$ Q; b8 q0 U+ U0 N7 H
- $tags = explode("_",$_POST["tags"]);
6 ], Y8 @! `9 ~2 f; d3 ^ - $cate = $_POST["cate"];
3 H' F; G3 e, ^ a - //print_r $tags;
0 f6 C8 d {9 t. `5 B# T - //
9 p, q; \9 E3 n! v - //
* i' F9 m" n" ~8 D: J - $wp = new WP();" O7 v1 C6 D* {, \# y6 {$ v9 {0 m A8 y
- $wp->main();/ v: t) Q, C1 S( ~7 Z$ _( q0 p$ ?
- //
) ]5 Z5 [' x* b- y - $my_post = array();
8 m6 X# ~/ ^. Z3 T5 m - $my_post['post_title'] = $title;: o Z9 I Z3 {4 r+ L- u
- $my_post['post_content'] = $content;8 D$ }2 U( e$ S2 a- ?, ?( A
- $my_post['post_status'] = 'publish';1 M" k* m2 m' ]3 q9 z: }5 v
- $my_post['post_author'] = 1;
/ z$ }" X0 ^- c! T' b' t& v - $my_post['post_category'] = array($cate); M, {) z! v L, B$ Y& j- p
- $my_post['tags_input'] = $tags;
. U, P7 ~9 [2 p% r8 r2 S - //$my_post['tags_input'] = array('tag1', 'tag2');
! b$ _: h, w+ o; N( s6 I - //$my_post['post_status'] = 'future';- ]" R7 a/ ^) {& E" C- c+ q
- ////$my_post['post_date'] = '2010-07-04 16:20:03';
% a0 y' N! ?' x) e( y( x3 D
' { w) Z$ W, P4 I- // Insert the post into the database
. b! @( Z& I% C4 }) [7 B - $ret = wp_insert_post( $my_post );
' G) T( k0 N0 N5 q - echo $ret;; l! B6 H: g1 l; G" R2 d. C# a
- ?>- a3 V3 b% a2 j
复制代码 我自己写的
& J% @; ^0 s) ^5 u' v6 w: H2 ~7 j1 j插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|