|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑 0 R6 Z, w4 \; L
Y' Y6 ^* @+ `- k1 B" _
直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持
- S* N( b8 i6 j: m3 t7 K% A8 J t" P3 D; \- L0 z; g
我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了( Z$ E7 t6 V8 G t1 Z0 a; H7 M
0 M$ z- M; Z6 [ h- I# T方便实用 简单 何乐而不用呢- 1 L" Q( V* E5 g9 }, }
- <?php
5 i( Y0 V0 h) ~: j$ _ - , z& a4 _8 m% E% l V8 r5 {1 p
- define( 'ABSPATH', dirname(__FILE__) . '/' );
* Q" g2 S! ] ]3 I6 N - require_once( ABSPATH . 'wp-config.php' );8 [5 y; p' H" Y
- require_once( ABSPATH . 'wp-settings.php' );
5 F+ {( k; }6 h5 a- g0 l7 \8 H4 o - require_once( ABSPATH . 'wp-includes/class-wp.php' );3 z. }0 x1 A4 s/ Q0 l2 F) ~
- require_once( ABSPATH . 'wp-includes/functions.php' );# x5 }8 o# z/ s4 O/ `* ]
- require_once( ABSPATH . 'wp-includes/plugin.php' );8 \7 M& @ g5 c2 e2 j' ^) M
- $title = $_POST["title"];8 K( g. K! V: C6 _5 O/ D8 s
- $content = $_POST["content"];
" C1 u k" u& _7 l# q7 ]9 H - $tags = explode("_",$_POST["tags"]);
e4 Y. H; q0 g3 w - $cate = $_POST["cate"];
l4 o; P/ n {0 t2 G$ a - //print_r $tags; }9 y: o P5 h
- //
6 U) m/ I4 r4 j, q/ C, M - //7 ~7 Y# x$ B/ b
- $wp = new WP();
1 x0 e( H; o5 Y% X3 U- ? - $wp->main();
+ ~1 u3 |3 u* {7 \ - //
8 G# R1 T7 f0 c2 D; b - $my_post = array();+ _0 w" K3 ~2 l0 _& m! j
- $my_post['post_title'] = $title;
5 ]6 O+ Z: l0 g5 A2 |" R+ |% o [ - $my_post['post_content'] = $content;
1 L2 E/ W& s6 ~2 k - $my_post['post_status'] = 'publish';
! L! r. ~2 f# A - $my_post['post_author'] = 1;2 B6 a8 G# p% M9 T3 G
- $my_post['post_category'] = array($cate);) S, H% l% H G
- $my_post['tags_input'] = $tags;
. B3 B G, A' H1 C$ U# f2 D' ]% L7 f - //$my_post['tags_input'] = array('tag1', 'tag2');7 E" m: E9 B0 G8 G
- //$my_post['post_status'] = 'future';
( @ E' ]2 N- r) ~) Q2 a _$ S2 A& q - ////$my_post['post_date'] = '2010-07-04 16:20:03';
1 c1 _& u* I8 I6 N; y - 4 E: t" x) u, L
- // Insert the post into the database
" E0 ?% b% X o) ^2 v2 R - $ret = wp_insert_post( $my_post );
9 U8 @& H+ Q$ E" P- S: L - echo $ret;! g: ^% x2 H/ Y: L
- ?>* S, o1 y* u( z6 s8 u$ u
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气
, w9 U. k1 ^" T- <?php
8 L: n1 _0 A6 \9 q; | - define( 'ABSPATH', dirname(__FILE__) . '/' );3 Q$ R. t& {* y* L8 x4 w7 Z u& R
- require_once( ABSPATH . 'wp-config.php' );& N: V5 [# G+ X; a6 P: A
- require_once( ABSPATH . 'wp-settings.php' );0 ~0 Q0 x' I+ z: U
- require_once( ABSPATH . 'wp-includes/class-wp.php' );, ^5 s9 R {0 f) Y
- require_once( ABSPATH . 'wp-includes/functions.php' );
4 O* ]' o5 G# F) L& U - require_once( ABSPATH . 'wp-includes/plugin.php' );
9 T% r6 f. g- X5 h/ g) ]# Q3 Q - $pic_name = $_POST["pic_name"];8 R" {- e# _6 e$ L, n8 G: s
- $pic = $_POST["pic"];
: |/ E" b/ k! w: y - if($pic != "" && $pic_name != ""). j! z! D# c5 p$ ?3 {) I
- {
! S) Y6 Y1 V- i( W& m9 B/ f - $path = "images/";+ w5 e+ d, ?; G; a: g9 E* A
- $pic_name = $path.$pic_name;3 W) [4 |$ X$ c& i/ t5 e8 q
- echo $pic_name;
& R4 }. L# N; R1 R5 T - echo "||";
3 f% \2 p& r! Z( b* x - error_reporting(0);3 G6 g5 z$ e! Z- m7 x. r) M6 [
- mkdir("images");
; S# x4 N4 t& K0 {- [; S8 `) x | - $handle = fopen($pic_name, 'w');# e0 h/ E5 P( \. S* o
- echo fwrite($handle,file_get_contents($pic));
9 i+ _0 ^, n6 L ^0 S - }$ V; T G7 l+ C) Z# [4 Q0 e1 k4 A# X& M
D4 c2 k; r' u$ s# c9 ^) h" f- $title = $_POST["title"];
7 ^6 t8 n( ^2 t! I - $content = $_POST["content"];
7 l; J# m5 Z+ o$ u6 G" ` - $tags = explode("_",$_POST["tags"]);1 V+ _" ~- ?% }8 K6 q
- $cate = $_POST["cate"];( T8 U, W& `; {1 ^9 ]. h
- //print_r $tags;& P" C% h! E8 G6 |. u# [$ G, K. a: a
- //- H; \& b/ a8 w8 e
- //8 l. V: Y5 B7 a4 p: N
- $wp = new WP();
# h1 X& j4 d+ e3 ^& z. X - $wp->main();
. C0 u' v9 d; k0 u7 D - //4 O/ l3 e6 ~/ ], l" e3 T$ `
- $my_post = array();
( w z9 a* X" q" O. j4 p7 b) G - $my_post['post_title'] = $title;
$ v3 V' |; n% m+ F+ ? - $my_post['post_content'] = $content;# d; j1 Q8 i9 ~& _( {; J/ X
- $my_post['post_status'] = 'publish';- R+ q) Y3 |, e1 S/ b
- $my_post['post_author'] = 1;8 U& H! P( c; w, v6 n
- $my_post['post_category'] = array($cate);
) e# A5 T' H/ ~ {0 `( @ - $my_post['tags_input'] = $tags;- `+ t+ D. M' O
- //$my_post['tags_input'] = array('tag1', 'tag2');
2 w/ R, c& F' o7 i8 [7 s% K - //$my_post['post_status'] = 'future';
- Y" X- P) L9 V d- ^ N - ////$my_post['post_date'] = '2010-07-04 16:20:03';# ~( d; T/ w0 p
' l8 F; r' l% l4 t/ c- // Insert the post into the database
" J1 v2 q+ S4 Z( L2 ^ - $ret = wp_insert_post( $my_post );
8 z+ B; k( Z8 y8 O! G - echo $ret;
) a, R1 g9 }: i. M1 {1 z - ?>
2 Q8 M$ q/ o6 g1 j( ^
复制代码 我自己写的2 A, e. O+ V P0 m2 W# ] ~6 F
插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|