|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑
$ ]3 r: @$ H4 T/ @' _8 P2 l6 b6 @% Q. m6 N. g5 k$ o7 V" u: ~
直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持
$ s0 E/ o2 ]) ]6 z: W P9 v8 f- X2 N: [8 [7 r
我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了' Y9 W- W0 M+ `& p
4 P1 Z4 A# d. Y0 R2 ?# M3 E- T方便实用 简单 何乐而不用呢
' c6 p1 X2 e; ^) L! X" ~& T- <?php0 m4 E$ C! j% E* q
- ( e! p9 X `$ u2 g4 W8 n% l
- define( 'ABSPATH', dirname(__FILE__) . '/' );6 M9 w+ f' e+ s! h, m( z
- require_once( ABSPATH . 'wp-config.php' );5 d8 h' B- `* M/ h* d: h
- require_once( ABSPATH . 'wp-settings.php' );6 ~/ ^9 D; f3 p' u
- require_once( ABSPATH . 'wp-includes/class-wp.php' );
L E0 ^5 n0 f, I5 t. ^ - require_once( ABSPATH . 'wp-includes/functions.php' );
" ^# l9 Q, t0 I4 ?8 e - require_once( ABSPATH . 'wp-includes/plugin.php' );7 l1 r: ?. F* A6 D/ J+ r. U& l
- $title = $_POST["title"];
3 ?4 {/ R6 |; }5 y" n- H2 e# r - $content = $_POST["content"];
% {3 C* O' Q$ c - $tags = explode("_",$_POST["tags"]);( J a- t. A/ N0 M
- $cate = $_POST["cate"];
+ ?4 R( Q* e* k) l( z& I# s: {+ ~ - //print_r $tags;
$ y0 n6 W* b0 q j# a - //& \4 b( B/ S& M3 D5 M
- //1 h! G+ u5 E1 A5 W9 L
- $wp = new WP();0 G6 i# w; @/ d
- $wp->main();3 V# m8 P! l; `1 `4 v* g4 @- k% n8 f" H
- //
1 o3 j8 V& |( D+ I# m - $my_post = array();2 C4 W6 x- {! q- u
- $my_post['post_title'] = $title;* D5 Z0 `$ J9 ^- t! L
- $my_post['post_content'] = $content;
& G% K5 F* k* d2 } - $my_post['post_status'] = 'publish';0 m* C/ ]- P: t* y: Z4 q/ x
- $my_post['post_author'] = 1;: y Y. ^* x7 P1 o- K l
- $my_post['post_category'] = array($cate);7 f* m& E% I4 {- k$ a& k
- $my_post['tags_input'] = $tags;
6 o. E" m1 U: X- R# V - //$my_post['tags_input'] = array('tag1', 'tag2');0 y5 Q& l" D9 R# b( |
- //$my_post['post_status'] = 'future';7 I' h- {) O' X8 L0 c$ f
- ////$my_post['post_date'] = '2010-07-04 16:20:03';
) c5 {# F9 _0 Q
/ J) V' D0 V. B; P0 \$ I; i- // Insert the post into the database
1 x" g. F& _+ x1 k' ~' Q0 B - $ret = wp_insert_post( $my_post );
: z& h- g. g$ h5 ]5 {& D( z9 k - echo $ret;0 `* V) K" ]9 P7 T
- ?>
6 p) ]6 Z+ f6 j& ?' ]
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气
' n( O# W$ j3 D- <?php& ^) W- k7 ]. T* F$ l0 `& ^+ J4 Q
- define( 'ABSPATH', dirname(__FILE__) . '/' );
4 v8 s) Y% R1 T" M; o# j - require_once( ABSPATH . 'wp-config.php' );! L& }9 n) y$ A. `2 _$ G9 a" d2 F: J
- require_once( ABSPATH . 'wp-settings.php' );
, l6 Q4 H% E: y6 m7 v* V6 r - require_once( ABSPATH . 'wp-includes/class-wp.php' );
3 p7 x+ ]" e B1 e) z - require_once( ABSPATH . 'wp-includes/functions.php' );
+ ]& z$ j' w* a4 K6 }. E7 o - require_once( ABSPATH . 'wp-includes/plugin.php' );- {: C; l4 i8 Y+ i5 ^: ^! p
- $pic_name = $_POST["pic_name"];
# p; ] j$ r( X* D - $pic = $_POST["pic"];
! h& G+ {6 v9 E1 u- \) p+ F - if($pic != "" && $pic_name != ""); `4 L$ l- W- m0 p T9 y: `$ t
- {
& G: B+ E7 y) r: V0 }' ~ - $path = "images/";
3 |$ M& G; E7 [# i0 @' B - $pic_name = $path.$pic_name;! x5 T% O! d4 G) o' w* D" E# @. {+ M
- echo $pic_name;
8 l; }" | Q1 m4 \; u8 y* K/ ~ - echo "||";
3 B; N2 `8 E9 j0 i - error_reporting(0);
, e; d( e* E2 Z - mkdir("images");3 [8 b" @/ Z1 ?. k, E
- $handle = fopen($pic_name, 'w');2 C, h) K0 L0 T& E
- echo fwrite($handle,file_get_contents($pic));1 I' l+ B M: z9 ~& }
- }
4 W7 ]4 h1 k6 m, L+ f( X. N
$ y) V6 t3 Z3 Y3 N, d _3 T3 o% x- $title = $_POST["title"];( t* S) K! y& b9 R' q
- $content = $_POST["content"];: t7 C3 g ?" b1 b4 l
- $tags = explode("_",$_POST["tags"]);' ^9 a5 t% N- P$ n0 b" E
- $cate = $_POST["cate"];1 o5 \ m; M; |+ k
- //print_r $tags;
" m7 F0 m. H& }) ~1 C- N, i2 W' \ - //% G# i* w- d" A7 q$ B% M
- //1 v9 t" ~1 x4 i8 X
- $wp = new WP();5 M# c1 x$ R$ d" V, j5 [
- $wp->main();
7 m9 \$ N0 R4 f3 z; B" ^- b* I1 j - //
. Q- j2 u( g8 E - $my_post = array();, `9 a8 E8 H8 R$ o2 W
- $my_post['post_title'] = $title;
4 o( y' T. L; r* I8 u ~ - $my_post['post_content'] = $content;8 T: v8 u* q/ Z1 y
- $my_post['post_status'] = 'publish';
# d2 v/ j2 _# q8 C$ \/ o - $my_post['post_author'] = 1;3 w, P, f5 C- ~& A! ^8 `, A
- $my_post['post_category'] = array($cate);
v1 U' o* Z8 V1 ~2 i2 n5 E G$ w e - $my_post['tags_input'] = $tags;
6 [6 F8 x4 U X7 C - //$my_post['tags_input'] = array('tag1', 'tag2');. l; M$ d: A7 ^+ J! u& W$ h: ^
- //$my_post['post_status'] = 'future';3 `! u9 ?: R; U$ w6 v9 x, e
- ////$my_post['post_date'] = '2010-07-04 16:20:03';! H; ~) N' ^9 B, p/ l' e; F- F
- " x: T$ I0 j) a# |7 L ~
- // Insert the post into the database
. q1 s' P+ j% R7 z - $ret = wp_insert_post( $my_post );
) l& s" d* a7 @& \9 J- [ - echo $ret;
& G: K) f* S! O% Z' r% I5 P; w - ?>
+ t' K+ l7 b- |- C
复制代码 我自己写的
3 C" D G3 u# p% S) L5 q4 V插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|