|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑
% o* v0 \6 }; v0 X, ^* d3 e
5 j- B0 t/ x7 A5 y直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持
4 B3 c: X: W, ?0 s1 n) A. h* G1 ^
( @9 ]% @: M, E# m- L我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了- i' q6 B) F" [) r. O' O
4 F9 S- t' T1 J n, g) P
方便实用 简单 何乐而不用呢- % l0 k F v6 s% ]- j2 I
- <?php
8 L. J' k8 T' {& e; [. N" b- | - 0 Z% X E8 B$ Z: G: w) z
- define( 'ABSPATH', dirname(__FILE__) . '/' );
7 p; \9 Q7 b# r# X* e - require_once( ABSPATH . 'wp-config.php' );# n2 ^9 \3 V3 x8 a9 z
- require_once( ABSPATH . 'wp-settings.php' );, r" K. x; J2 G- z3 v: O- y
- require_once( ABSPATH . 'wp-includes/class-wp.php' );
# d# a: Y [5 ^ - require_once( ABSPATH . 'wp-includes/functions.php' );
' x$ D- q! x0 c$ f/ b - require_once( ABSPATH . 'wp-includes/plugin.php' );
: @- S% Q* {* } - $title = $_POST["title"];! i$ ^7 d+ r( |6 E! g+ j5 |
- $content = $_POST["content"];0 w9 b" g: q6 [( a2 n* ?; Z) P
- $tags = explode("_",$_POST["tags"]);, L7 g m5 |* N" r* @5 i
- $cate = $_POST["cate"];: J+ w! v/ m1 w2 r* a
- //print_r $tags;
. @- O* B- o: G - //( f5 |. p1 I9 U/ q! j
- //
0 e- r. M6 o$ n - $wp = new WP();- n/ v; r6 i- V: T* O) n
- $wp->main();
1 ~4 V! D) c3 y) ^+ N& p% g+ ] - //
4 H0 [8 e& {! N0 E - $my_post = array();
. k$ }4 ]: u6 f' i: l! O - $my_post['post_title'] = $title;
, V/ x/ C0 V) n/ w - $my_post['post_content'] = $content;
% Z$ d1 T9 A6 y( Q. d - $my_post['post_status'] = 'publish';
+ g3 b$ X) g* l3 k" w% a - $my_post['post_author'] = 1;
" _# [ p! c+ |4 b - $my_post['post_category'] = array($cate);( T4 H6 ~( w, q- D1 [
- $my_post['tags_input'] = $tags;
7 A1 c; @7 D3 f& p+ d3 T - //$my_post['tags_input'] = array('tag1', 'tag2');' V& p3 C1 r9 f
- //$my_post['post_status'] = 'future';
, Z9 ?1 K$ v! k/ [9 x% p0 o - ////$my_post['post_date'] = '2010-07-04 16:20:03';
1 v& Z% v1 l- I! H4 g/ k
+ ]2 A7 Z1 |: |- ~3 `- // Insert the post into the database
\# U. m6 ?, b: `; H5 G2 L - $ret = wp_insert_post( $my_post );
S- x9 B! ~5 k1 R" _- v - echo $ret;
) \ X) V0 N/ f K - ?>7 W- I0 Z# G2 M* x9 N
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气
1 o9 d" g& l, X: H8 U! t, U- <?php
' d" |/ H) x4 v; J - define( 'ABSPATH', dirname(__FILE__) . '/' );
( _& W4 Z- I' ^+ o6 U) y- k3 {/ J - require_once( ABSPATH . 'wp-config.php' );2 K4 m' r# B8 _
- require_once( ABSPATH . 'wp-settings.php' );* K3 i8 G! m7 ?) B
- require_once( ABSPATH . 'wp-includes/class-wp.php' );
5 i: @: }: R7 W - require_once( ABSPATH . 'wp-includes/functions.php' );+ D0 M2 Q ?+ `4 g& l0 X
- require_once( ABSPATH . 'wp-includes/plugin.php' );
b& x# ?, ?5 a( r) P, T - $pic_name = $_POST["pic_name"];. S# n1 [5 N0 y/ |& l: O
- $pic = $_POST["pic"];
! K% k( D) @. u2 j - if($pic != "" && $pic_name != "")! j3 z5 ^/ p) V: r0 e: x; y1 L
- {
( ~2 h- Q2 j Q Q7 T - $path = "images/";9 I% ^ B) t+ S/ p& q H
- $pic_name = $path.$pic_name;
1 Q' X5 K6 ^$ D5 M, O - echo $pic_name;
" Y" X2 z; I F# n( M - echo "||";
* I- W3 T8 Y. l1 o3 n - error_reporting(0);
6 f* [7 J% @+ z% \2 h2 Z - mkdir("images");; r, V7 y5 T0 }3 L& h
- $handle = fopen($pic_name, 'w');
+ k/ P5 s' H" Q) \3 }7 c/ ^3 D - echo fwrite($handle,file_get_contents($pic));2 }& y# I0 r3 n+ A% }9 j! o8 ?
- }
2 N& ~) \! ^8 o$ s A' C7 ^
4 j, T. f! p9 a2 D3 Y9 o- $title = $_POST["title"];4 s3 Y* ?/ V: P' Q- Q
- $content = $_POST["content"];
. a/ a; d* R0 s# U% x/ a/ B% k - $tags = explode("_",$_POST["tags"]);
' {4 ^ [( V& I0 j - $cate = $_POST["cate"];& U( ]4 D+ S* D- `3 T0 [0 p
- //print_r $tags;
. @" ^7 X' }8 @. Q7 L2 Q) A - // k {: K- e( V3 t) W0 D- f. R1 N
- //: D4 z# ]) x0 D' S/ F2 z4 y
- $wp = new WP();
( c& p' z. W' J. S* }5 a7 m - $wp->main();, P2 E# T5 Y/ }% x/ O
- //1 `+ C! g* O& p+ L4 x# N, A
- $my_post = array();* a. R7 V2 X- H" }
- $my_post['post_title'] = $title;0 ` L: }8 b& q. \4 M) t5 m2 s
- $my_post['post_content'] = $content;
' d% y: s+ T0 B2 ~) A - $my_post['post_status'] = 'publish';
+ w1 x5 H6 l2 d2 f - $my_post['post_author'] = 1;. d, H9 M+ u0 V1 ^6 P5 K6 ?$ G2 m
- $my_post['post_category'] = array($cate);0 x2 \* R6 \9 T, _ t
- $my_post['tags_input'] = $tags;
" G: x9 K+ [ g% k; q - //$my_post['tags_input'] = array('tag1', 'tag2');7 q+ |; N* b# I! x
- //$my_post['post_status'] = 'future';
/ ?7 \1 X& a# F6 _( X, x - ////$my_post['post_date'] = '2010-07-04 16:20:03';& Q0 D* K0 U4 I- H# k
/ v. _. H% W9 V4 f- // Insert the post into the database
/ F- _. {, R2 s/ k: s! I6 Q, j - $ret = wp_insert_post( $my_post );
3 G4 e8 c$ c/ }1 l$ O, L - echo $ret;# g- t. H1 t9 `" v0 d* h
- ?>
6 w: u8 {! `1 ?! N3 t
复制代码 我自己写的
! s* i1 W6 n: N: G3 f6 V% o插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|