|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑 2 d0 b$ k0 d7 m' w7 A4 N% N8 r
( B4 R$ _9 m: V' a$ ?" Z直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持5 \; J. s" j0 ~7 b8 K: I
. I, c( Z( W) Z* q4 S% ^9 a我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了
; v2 }4 I9 c7 s/ F/ v- c* a; j0 n/ a/ n' T8 V$ |- k
方便实用 简单 何乐而不用呢
2 t4 ^2 ~+ V+ f4 a% n- <?php
K5 @; `& M; J - ' G1 O9 R! s1 R# Q% I( K
- define( 'ABSPATH', dirname(__FILE__) . '/' );
: t' c% y8 M2 I+ Z* o( L3 g9 I/ S/ j3 Y9 D - require_once( ABSPATH . 'wp-config.php' );' z5 @* E" d% ], b4 Q
- require_once( ABSPATH . 'wp-settings.php' );
( s" T _$ d! _; m - require_once( ABSPATH . 'wp-includes/class-wp.php' );8 X! g( x1 C" r* N0 g; ~ e* \
- require_once( ABSPATH . 'wp-includes/functions.php' );' m s1 Y- ~7 T8 ^* ~
- require_once( ABSPATH . 'wp-includes/plugin.php' );3 p. s0 |4 ^6 V+ _, `: y$ ^
- $title = $_POST["title"];
9 m2 b4 }4 S* h* r0 q6 L- g2 U - $content = $_POST["content"];
" j# @" n5 x! Q1 w+ ]! ?; |' |' \ - $tags = explode("_",$_POST["tags"]);
: I. R* U% R( P- g: H0 h - $cate = $_POST["cate"];! i# {9 U4 Z/ m' J: v9 s- y, {
- //print_r $tags;
9 d* V, U [6 A+ [% e - //
' Q2 k- B8 ^8 v8 S0 U2 Z - //) m. Y8 R8 v2 R! Z
- $wp = new WP();: P1 I* M% _+ `9 F
- $wp->main();* n7 t( S8 v! \6 q9 f
- //
3 l4 T! Y4 M/ ^" f! @0 i- X# E - $my_post = array();' n" D$ V- B; D0 t
- $my_post['post_title'] = $title;( t3 w, @; ^/ B s3 P' V
- $my_post['post_content'] = $content;
1 H ?( S3 z4 p- Z - $my_post['post_status'] = 'publish';
# ~$ h( ^, A3 o( y" c: I! A! A- F' G - $my_post['post_author'] = 1;
7 E7 w( }) }4 U2 j! F; ? - $my_post['post_category'] = array($cate);
: N7 H6 e5 ]. ~8 {# U0 Q - $my_post['tags_input'] = $tags;" Q5 x- K7 }) {* g
- //$my_post['tags_input'] = array('tag1', 'tag2');
( f s1 K% o2 w - //$my_post['post_status'] = 'future';
) ^. o2 B0 P+ v - ////$my_post['post_date'] = '2010-07-04 16:20:03';0 Y! u/ Y6 p/ N ], u; F* g" C
; p* o6 ~% K6 z( ^& m- // Insert the post into the database
8 q: O. e9 f: W: b- u - $ret = wp_insert_post( $my_post );' Q( n' F" P( b W# I
- echo $ret;
) D, M7 m3 F* [# a6 q! L# o- Y - ?>
4 J: K( e! E- y" Z$ N
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气
; j" l) t3 `% ]5 e. {9 j- <?php$ \- n" A. ~9 ^+ {' J
- define( 'ABSPATH', dirname(__FILE__) . '/' );+ ?. ` |* {8 D* T. P* K, X
- require_once( ABSPATH . 'wp-config.php' );7 w4 T3 p, b+ o+ u, y
- require_once( ABSPATH . 'wp-settings.php' );
# f* A, y1 ?2 M1 M9 x5 F - require_once( ABSPATH . 'wp-includes/class-wp.php' );: Z( j. O4 |' {- {* _ _
- require_once( ABSPATH . 'wp-includes/functions.php' );
, X9 u5 g. I9 u2 R - require_once( ABSPATH . 'wp-includes/plugin.php' );" T" A- e6 S' R9 ]9 B# r
- $pic_name = $_POST["pic_name"];: i' K* b- u7 {& U8 H1 q7 N
- $pic = $_POST["pic"];6 D2 _& J! _8 Z( `
- if($pic != "" && $pic_name != "")4 w1 Q q+ V2 B( d' _# H! K8 M
- {# i0 ^" D& Y' L c
- $path = "images/";$ t9 V8 q+ d8 x0 L' J, p! i
- $pic_name = $path.$pic_name;8 i, s' V; A6 Q) `
- echo $pic_name;% M1 U; B" q$ p5 P) {/ o- P
- echo "||";
\/ Z* {" E8 c3 L - error_reporting(0);
! a6 N# a7 a3 k2 f1 Q7 W) } W4 d - mkdir("images");
- J& r5 D7 D2 ^/ Z# R - $handle = fopen($pic_name, 'w');
/ X4 M' J, z; D - echo fwrite($handle,file_get_contents($pic));
$ A8 j) D8 y1 n# t3 n' y - }, x+ j, J, {. A4 x" X# P4 L# |
- * y. W7 }" Q, V. t! v* f
- $title = $_POST["title"];7 X$ a$ \& w/ E) S; C8 P: K
- $content = $_POST["content"];* v5 Y" I! E& ^, @1 x
- $tags = explode("_",$_POST["tags"]);! u. Q4 L X; E% l, H, N: ?; d
- $cate = $_POST["cate"];0 h3 E) o7 n" K
- //print_r $tags;. {# ^! ?; i1 J& n( S0 D2 N e
- //
7 M0 t( N+ O! v1 t+ a8 n - //
6 L% W5 u* ^" I - $wp = new WP();9 W" h- `3 e6 h5 d# { [+ c
- $wp->main();% u: ^: u) f! P+ A4 x j, m9 ]
- //
2 i% C; v8 `* E1 y$ s - $my_post = array();
% r4 K+ g1 f3 s% s& ]! q5 y - $my_post['post_title'] = $title;7 Y! B) l7 z8 V2 V/ z
- $my_post['post_content'] = $content;. O4 m. s- `& k( B, ~& w
- $my_post['post_status'] = 'publish';# ~ i; ^& H( n0 i1 F: C* b$ d, x
- $my_post['post_author'] = 1;
" P7 r$ K! [6 r5 } d, {: z, w# D - $my_post['post_category'] = array($cate);
6 Q/ ^) R. Z; C7 }9 g$ W - $my_post['tags_input'] = $tags;
& C/ N% S( m: K9 y `% E - //$my_post['tags_input'] = array('tag1', 'tag2');
$ [& N, C; {% |9 f - //$my_post['post_status'] = 'future';
) N3 f" @2 m4 v2 k8 C' x( Y - ////$my_post['post_date'] = '2010-07-04 16:20:03';6 ]6 ]- H* { v6 V9 ?. S6 z+ T
* t: I% h- n. k/ y, K5 N, _- // Insert the post into the database
8 a9 U; C% @6 e - $ret = wp_insert_post( $my_post );- p; S1 d+ @- ~7 S9 [
- echo $ret;; T/ g3 ?* g, X! y, R! ~
- ?>
. i+ s# R2 ^8 ?, S3 e/ {
复制代码 我自己写的9 @9 l3 |/ O& Q& a$ _2 a% g$ E; w4 S
插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|