|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑
8 H0 A! o# ]2 o) ^+ _( b
+ f" }$ w, g+ t7 b# L直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持5 U$ z$ p, R" _+ g- H2 K2 K
) L6 O2 M9 Z) d# a& Q. S
我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了
* C1 h% G* S/ o" ?4 Y! J
, a2 d" u9 g% ~方便实用 简单 何乐而不用呢
6 y7 o3 U+ m, R6 i/ F- o* Q- <?php5 U. Q/ u, b- D" h- p9 q: e- d
- ) J; z& k6 u0 J7 c. b# I( t4 O
- define( 'ABSPATH', dirname(__FILE__) . '/' );# { c: z: W( U; w' u, f4 E8 G
- require_once( ABSPATH . 'wp-config.php' );
* w# {+ ?; D7 K% F+ S( O4 I - require_once( ABSPATH . 'wp-settings.php' );
7 W4 W- R1 ?9 k: F$ M - require_once( ABSPATH . 'wp-includes/class-wp.php' );3 ^5 G; R0 t/ {: i7 r
- require_once( ABSPATH . 'wp-includes/functions.php' );3 w r3 U+ @2 _4 v
- require_once( ABSPATH . 'wp-includes/plugin.php' );0 c- A* S& Y5 d1 e
- $title = $_POST["title"];
9 P9 s9 M8 ~, ? - $content = $_POST["content"];
0 C: q# Q z# C( v* C4 w - $tags = explode("_",$_POST["tags"]);4 x: M& W0 H/ x
- $cate = $_POST["cate"];
) X a/ @+ G( N$ y/ p - //print_r $tags;
; E' X) X! m z - //! i3 [) N7 L0 h
- //3 _) [1 l' M( q0 A* E
- $wp = new WP();
& U/ J1 n- t7 d& c8 f/ _. v( X - $wp->main();' T; {) k2 n7 {
- //$ X, t9 k! T- B# ~- |( H8 ?$ E2 @
- $my_post = array();
8 m* U5 `; Z* h - $my_post['post_title'] = $title;- e# T+ Q- g, `! H( d* x
- $my_post['post_content'] = $content;
6 u( t1 `# G3 G3 T - $my_post['post_status'] = 'publish';2 l# l! M$ Y) H3 ]' h
- $my_post['post_author'] = 1;
0 e; G) P5 b! j+ G3 o - $my_post['post_category'] = array($cate);
; ]/ z& l$ W' I0 {+ r E2 }" }$ D% w - $my_post['tags_input'] = $tags;
3 P: C' q: e& j2 s4 t: N - //$my_post['tags_input'] = array('tag1', 'tag2');
3 g! E+ }5 Q" [/ b; S - //$my_post['post_status'] = 'future';( n; ?, e. C- d# |3 v% i* r, M ?. {
- ////$my_post['post_date'] = '2010-07-04 16:20:03';
' G8 V8 D) A2 Q7 e4 P. X9 b - ! Z2 M2 i; ]6 h6 C
- // Insert the post into the database
) a, l, O0 J* H3 s. { - $ret = wp_insert_post( $my_post );' D* E) N1 ~5 C$ ?
- echo $ret;
- j4 y4 u5 F4 _6 m8 m - ?>
" E. {4 P9 C9 W; y. y
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气
+ N8 m4 {" ^7 d- <?php
7 P. L7 E) l" v3 E) D - define( 'ABSPATH', dirname(__FILE__) . '/' );/ O1 B) F3 j; `+ K, q3 B W
- require_once( ABSPATH . 'wp-config.php' );/ Y e8 }' E8 m
- require_once( ABSPATH . 'wp-settings.php' );% x7 i) V, Y) D, U' ^
- require_once( ABSPATH . 'wp-includes/class-wp.php' );
4 m" v; k% G, z - require_once( ABSPATH . 'wp-includes/functions.php' );
: D2 b$ U* w9 N- n - require_once( ABSPATH . 'wp-includes/plugin.php' );
( n- r8 y/ r9 t j2 \8 L - $pic_name = $_POST["pic_name"];1 w: N9 k/ c: N2 K: L
- $pic = $_POST["pic"];
2 a S1 ^8 P m5 z# ^& z9 q - if($pic != "" && $pic_name != "")
7 D. K' w3 [! {- [ - {2 m4 Z+ c3 @" Y( U2 ?" Q
- $path = "images/";
( E5 s! @% A7 y; D7 p - $pic_name = $path.$pic_name;
9 h' r2 V4 K! c/ g `% I" w5 A - echo $pic_name;: j4 ^5 F) {: @/ f# _1 X) f- `, P5 _
- echo "||";/ U, `$ }+ j1 a& }3 S
- error_reporting(0);
! ]+ u0 l X: L$ o% x/ E2 ^! T - mkdir("images");
/ F) ~1 g) R) B" h+ h - $handle = fopen($pic_name, 'w');
4 P0 B9 Z3 g2 e: k - echo fwrite($handle,file_get_contents($pic));; i+ F; G& Z+ z' ~" L
- }
; L7 x8 {0 U& T! m2 A8 s0 T
0 c4 l' f' `, ]: k4 _- $title = $_POST["title"];/ C2 V. J1 H! A! N7 {5 J$ p
- $content = $_POST["content"];
7 b e4 G* I% r$ } - $tags = explode("_",$_POST["tags"]);
, i1 b$ d9 g2 q# A: _ - $cate = $_POST["cate"];% ]# V# T% v9 ]0 }2 ]0 }3 b
- //print_r $tags;& \; Z" y- b9 X
- //2 w2 i) L5 [& W. |* Q( t" M
- //, e* s+ W/ {% q2 l% D, N( c
- $wp = new WP();
; Q% A4 Y! E% E2 d9 \" \7 P1 L3 C! q - $wp->main();. X! B, d7 {3 I: }; u
- //" `5 ]6 M8 S% k# N0 M; k+ A/ b
- $my_post = array();
4 B! P! \) x1 @9 U( Q" Y - $my_post['post_title'] = $title;
0 x' J3 U" }1 R" e0 O) h% P3 r - $my_post['post_content'] = $content;) w$ f) A8 A8 ?- l. i. j" o5 [
- $my_post['post_status'] = 'publish';
' B" ^: |# P' k8 p( O - $my_post['post_author'] = 1;
4 \; m8 L8 I4 j; k( N - $my_post['post_category'] = array($cate);
0 P; X+ k: S; u2 {' C - $my_post['tags_input'] = $tags;
! I' |7 J M$ x% T" i: S5 N - //$my_post['tags_input'] = array('tag1', 'tag2');1 d1 }8 Y. k( [$ X% e7 A x
- //$my_post['post_status'] = 'future';3 d9 F3 d+ m8 D1 W( [
- ////$my_post['post_date'] = '2010-07-04 16:20:03';6 {; W# D: {- B" Y* U( j
- # V6 c5 U9 g2 Z9 Q
- // Insert the post into the database
- U1 `6 e u& g6 S. \$ u - $ret = wp_insert_post( $my_post );( p) |5 D& U3 n: @) h
- echo $ret;
1 u, f/ D9 ^/ F1 b6 R: f - ?>
, F( [1 ?+ C! P8 m& O5 M+ J6 u: a
复制代码 我自己写的3 a ?! s7 g! ^
插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|