|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑
1 R( j. ~ _$ X5 D8 `' }& L! V# t- k* p% q( ^, W2 l
直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持2 }6 ]2 ^% B0 Z1 c# B
6 J5 N5 S" z! Y4 U" O I. \
我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了5 x# ^2 Q3 P. l( r. Y$ I6 k7 J
+ U2 Y# m( ]/ Z9 N- [- ?- s& G% a方便实用 简单 何乐而不用呢- $ A& n7 J4 |0 q5 t8 j7 d) K, T
- <?php( O$ O: T' @ x9 |- A( r6 F
0 k8 Z' f- Y, {6 D# Z- Q- define( 'ABSPATH', dirname(__FILE__) . '/' );% ~# e% q" N( @ `# w1 |0 z% S
- require_once( ABSPATH . 'wp-config.php' );9 Y2 V2 ]; c) U# l2 w
- require_once( ABSPATH . 'wp-settings.php' );( K3 n% y- ]7 D
- require_once( ABSPATH . 'wp-includes/class-wp.php' );
# l" H" z5 [, O# b& t$ l - require_once( ABSPATH . 'wp-includes/functions.php' );' A# d: O% j" w2 h- ~# {2 I3 v
- require_once( ABSPATH . 'wp-includes/plugin.php' );
5 U- _1 F' G" E/ M l, _6 _( ~8 s+ J - $title = $_POST["title"];7 _+ K3 \: V* x
- $content = $_POST["content"];4 V, M, \: i7 y
- $tags = explode("_",$_POST["tags"]);0 W h0 t0 ~ U* }! y* E8 l
- $cate = $_POST["cate"];. {3 s: l9 j0 u0 @$ z" l3 ]
- //print_r $tags;2 F( u; f( Q) D: O
- //
! |# `' I; D% J+ f7 Z - //7 N% P K6 c" s7 @* b4 s; D
- $wp = new WP();
# Y5 V. O* V% a$ r - $wp->main();4 Y1 s, t7 l: g. g' K( v
- //
3 \6 p& K3 s6 c9 S$ k - $my_post = array();. X* o+ I/ }4 Y+ W- ]. U, v8 i
- $my_post['post_title'] = $title;
# i7 R6 `8 |+ h" c - $my_post['post_content'] = $content;, W& W9 I, d: P+ @8 m1 w2 }& Z H
- $my_post['post_status'] = 'publish';' i; t+ R4 {( ?- v+ h' c
- $my_post['post_author'] = 1;) q/ y P" d V3 ~
- $my_post['post_category'] = array($cate);
; `5 P9 b; o& B8 y+ C - $my_post['tags_input'] = $tags;% l8 b; k. Q7 ~) V7 o- d
- //$my_post['tags_input'] = array('tag1', 'tag2');
, t7 e0 o5 a& N$ b/ C8 w - //$my_post['post_status'] = 'future';
+ P" Z/ A6 |* i }; D3 E - ////$my_post['post_date'] = '2010-07-04 16:20:03';
4 y; {/ G" F! O0 ?$ Y+ V& x. K; c - 9 ~3 Z7 s& X& Q) n0 v. j
- // Insert the post into the database5 x) n4 _; b' j: C$ T
- $ret = wp_insert_post( $my_post );
" J# `. r, F# F$ n. E6 P7 h - echo $ret;
5 g# R# o$ w3 T. G - ?>
3 [; x; a( \; u3 i* W B
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气
& c8 H5 b4 _" L* O- <?php6 \9 d' O! T3 H3 d
- define( 'ABSPATH', dirname(__FILE__) . '/' );4 N2 W/ k+ q0 F- ~# A5 B( j" j
- require_once( ABSPATH . 'wp-config.php' );
8 d+ v( C8 L3 g" E9 C - require_once( ABSPATH . 'wp-settings.php' );, R( `% d# `2 T- k+ N
- require_once( ABSPATH . 'wp-includes/class-wp.php' );
3 q7 V2 M5 w1 M ^ - require_once( ABSPATH . 'wp-includes/functions.php' );* w/ G0 k; d0 c: A4 ^
- require_once( ABSPATH . 'wp-includes/plugin.php' );
% G" t1 a% Q+ Z, k - $pic_name = $_POST["pic_name"];% Z9 f& O( Z3 J2 }
- $pic = $_POST["pic"];
/ i* Y0 I. r; H7 x6 f7 U - if($pic != "" && $pic_name != "")$ t4 w- s( |; i4 e) z
- {$ t( {: H& ~' j4 t
- $path = "images/";( ?4 _, s+ W, E q
- $pic_name = $path.$pic_name;1 v; g% L9 |* i, u* [( J% v5 b
- echo $pic_name;5 O) R) K+ p' w9 z( x% i9 t0 R
- echo "||";0 R* a& w! a* \" s5 M( n0 d- s' s' F
- error_reporting(0);
; H5 M0 H# k/ v# X, B* f8 } - mkdir("images");3 e- Z5 v# D' |; `
- $handle = fopen($pic_name, 'w');9 E, x( c z+ t8 F7 l2 k' r( ~
- echo fwrite($handle,file_get_contents($pic));
( W- u5 x, z- y- M) }; t7 @ - }
$ a. f2 U% m6 [; p - % A* g& C" d+ k: X u. U) b
- $title = $_POST["title"];
% r9 g! G& \2 } - $content = $_POST["content"];6 R" H) \& N/ j; g5 z( d
- $tags = explode("_",$_POST["tags"]);3 @8 j8 t" l$ `
- $cate = $_POST["cate"];7 s! {' u3 h( F3 h4 k
- //print_r $tags;
0 G) H6 a# ]% ~ - //
% g/ `: g' K, @6 b% h - //
0 f r0 O1 n( s% `. d: N: N - $wp = new WP();5 g6 z2 ?/ \6 T
- $wp->main();# @' Z6 U C$ B7 h$ m9 C4 i# o
- //
% p& _0 S" H3 D# E2 C' ` - $my_post = array();
. U/ {8 \% g6 ?- M! {& }' P - $my_post['post_title'] = $title;
) m4 T9 a2 G: K* T" b - $my_post['post_content'] = $content;
% t/ s; N& b0 T6 O - $my_post['post_status'] = 'publish';
9 e4 `* e: n- C! w0 M - $my_post['post_author'] = 1;
2 }/ I* s' l" M9 i( O; B: e8 d0 X - $my_post['post_category'] = array($cate);
# ]2 w2 _9 _0 K$ b- R( e- _ - $my_post['tags_input'] = $tags;# |! Z0 H3 Y) l7 g% R7 g/ X
- //$my_post['tags_input'] = array('tag1', 'tag2');
. b' v$ W5 r: ?1 i5 k- N7 h - //$my_post['post_status'] = 'future';
, P* C$ n, R7 S. t* a/ g5 R - ////$my_post['post_date'] = '2010-07-04 16:20:03';
. X( f h ?4 W9 {7 S
! L% z: t4 ]3 h$ L5 C9 I6 L- // Insert the post into the database- P9 ^, x1 Q' P% p6 | G- ^& y2 h$ @
- $ret = wp_insert_post( $my_post );
. Z- G5 u" ~8 _$ I9 q5 @% F - echo $ret;
" V& t0 d4 q* a! S( G# S - ?>
, I" w/ h' M+ \8 m
复制代码 我自己写的
) i. p2 T7 j3 F: G/ I1 W$ B插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|