|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑
; N2 N8 F) N x( l9 I2 W& w9 O
- F7 Z& i. T+ E直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持, o/ N" A, S& T: C/ z
& @. b; _4 e. b" ~% R9 z4 T% z
我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了
9 X0 C$ S; w9 B
. q7 I: `5 i6 E J# D+ z: Z方便实用 简单 何乐而不用呢- ; W+ w- k* Z: V
- <?php3 f, i! ]7 `) T; ?# C+ Z' k# N; V/ q
- 6 ~' l ~: |# Z5 R; r
- define( 'ABSPATH', dirname(__FILE__) . '/' );
! z1 q$ F) ?9 ?* u% h8 c5 h - require_once( ABSPATH . 'wp-config.php' );
( a" R0 L. {8 a6 P* g6 W' p9 B/ Z - require_once( ABSPATH . 'wp-settings.php' );/ j! f. @! R; D6 Z4 l
- require_once( ABSPATH . 'wp-includes/class-wp.php' );
7 c. \7 T1 a3 q& g/ U! q - require_once( ABSPATH . 'wp-includes/functions.php' );
* o% i" K; o V, l - require_once( ABSPATH . 'wp-includes/plugin.php' );
4 o/ [9 v C. l* y; z - $title = $_POST["title"];* N$ [5 W" _0 F2 ?
- $content = $_POST["content"];
% z! ?1 W1 S, M P1 j3 k0 \ - $tags = explode("_",$_POST["tags"]);
3 M, I7 ]* ~" h6 C: x- ` - $cate = $_POST["cate"];. V4 m7 q( i0 v, i" Z: I
- //print_r $tags;
) t$ D5 H' T7 R$ o" ^, |2 ^! d9 W - //- { J, ~5 c# I+ x4 \! g
- //( T. v1 X% ~ y& B) i6 e
- $wp = new WP();2 r% l: i$ \7 ^# Y& E( k$ {# V8 a
- $wp->main();
. `- b4 s9 X3 ^" y9 | - //2 ~0 }2 n: E. w
- $my_post = array();
% G5 q/ f0 l: | - $my_post['post_title'] = $title;/ J2 D; Z% ?$ ?; w7 p
- $my_post['post_content'] = $content;
$ `+ l9 W2 o, q: R6 j' N - $my_post['post_status'] = 'publish';
& p; _4 q" E7 B" `% t - $my_post['post_author'] = 1;
4 n0 R% x/ j& J, Q4 S - $my_post['post_category'] = array($cate);
. b( a) Q6 c. ~0 ?# E& M - $my_post['tags_input'] = $tags;& e7 o4 m2 p* y1 Z% a
- //$my_post['tags_input'] = array('tag1', 'tag2');
6 z$ @$ q6 O- H% A - //$my_post['post_status'] = 'future';
6 |5 T8 e9 c* x; o6 V& X6 f - ////$my_post['post_date'] = '2010-07-04 16:20:03';
9 B) E7 e: l0 { Z
+ H' E+ J0 n+ j0 R0 Y; F4 u# j: q- // Insert the post into the database
0 p$ B1 _3 d- Y# L, L - $ret = wp_insert_post( $my_post );
# r6 D6 k$ d0 S - echo $ret;
& n2 R9 b% Z" i+ d( S+ P& {6 s - ?>. w: _; n% n* \4 p: B
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气
- n/ S7 D+ D6 ?7 i- <?php0 J" e U, ~0 G) F Y) r
- define( 'ABSPATH', dirname(__FILE__) . '/' );
8 i7 z$ c* a5 j' E/ T* S - require_once( ABSPATH . 'wp-config.php' );) E' R$ ?' e. }# a2 ]. U* }' ]4 H
- require_once( ABSPATH . 'wp-settings.php' );
2 x: u3 | h2 R$ |+ n - require_once( ABSPATH . 'wp-includes/class-wp.php' );
! c$ |9 @" @- ] - require_once( ABSPATH . 'wp-includes/functions.php' );
( ^& D* V3 e8 A( ?, Q) j, C. i: V% S - require_once( ABSPATH . 'wp-includes/plugin.php' );! v V1 M( Y f- B
- $pic_name = $_POST["pic_name"];3 E/ h8 f. j, C( A0 w% [
- $pic = $_POST["pic"];# i! q6 g' h* m4 L& k A
- if($pic != "" && $pic_name != "")
5 M* {' F+ ~: W+ }- V7 Q - {6 N- c! H* n7 i2 C, W, }
- $path = "images/";
$ u5 l6 \2 T3 Z4 r: i d6 Q5 F - $pic_name = $path.$pic_name;
' z# R3 u5 I7 B/ w - echo $pic_name;
/ C; p$ s7 }1 y0 I9 w# C$ @ - echo "||";0 ~( t, q$ }+ |4 X
- error_reporting(0);
: Q# c/ Z% U( U' Y: W - mkdir("images");7 y- y8 r$ ~* Y& ^5 t
- $handle = fopen($pic_name, 'w');+ M0 W$ a) F. o* M/ |
- echo fwrite($handle,file_get_contents($pic));
@6 p: E; H7 f; S. |( N4 q7 z M$ A - }# C s3 L: ~1 i# L
" M) x0 P' R; H- $title = $_POST["title"];! X7 ~8 L% Y1 m! R# h2 S
- $content = $_POST["content"];0 m# O% Z6 d& f9 K/ l
- $tags = explode("_",$_POST["tags"]);
4 ^. v6 _$ f8 |7 }& d% M& u - $cate = $_POST["cate"];
6 E5 E: e% R% X5 w9 Z: ? ^ - //print_r $tags;6 Z/ I/ w7 Z9 P
- //
* Y' B, b6 D H2 j/ G# j+ x3 E - //
1 a- `! O. _/ p - $wp = new WP();
9 H# j: a3 S; |) w/ ?/ q - $wp->main();; p* a0 |9 k) t5 v7 `2 N" S3 q
- //, n, r* Y' y% y! Q
- $my_post = array();+ @8 v3 M8 f. Q0 ]- {( b
- $my_post['post_title'] = $title;4 ~9 d3 X. P' ~. ]
- $my_post['post_content'] = $content;
2 Y: _7 d* @7 g" q - $my_post['post_status'] = 'publish';
9 W3 A# U/ d" m. y5 y9 g! p - $my_post['post_author'] = 1;
6 `& @7 k0 `' \( { - $my_post['post_category'] = array($cate);
0 \; L, b6 c" P - $my_post['tags_input'] = $tags;" Q4 n0 t; x% Y& X4 P) Q; M5 O
- //$my_post['tags_input'] = array('tag1', 'tag2');
6 |5 W# K+ k$ ~ - //$my_post['post_status'] = 'future';
4 P9 v' g( Y; I9 u0 |7 f - ////$my_post['post_date'] = '2010-07-04 16:20:03';
1 B2 W% m+ \0 l' E- L D5 M/ A
' \* m. K& U8 ?' S5 T5 l- // Insert the post into the database
- s3 W0 f: d/ U" n2 r4 u8 U- D: m - $ret = wp_insert_post( $my_post );$ E3 f3 }: o5 Q/ B2 ]: q7 N
- echo $ret;* A2 a! y+ r; t4 }+ M* p3 F( o) a
- ?>" J) ]9 z1 F) E: u
复制代码 我自己写的
/ h; V/ L& ?% ?$ `插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|