|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑
, q* G# {' g# G8 _. ?6 y: ?5 W7 {* P# P0 T
直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持
# T6 U& p* w: P( U# q2 P0 M# m2 u# b# O+ W
我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了* J. u; ~! f# R, j# `
% P, E5 ], V- s' q4 {. l8 f# n
方便实用 简单 何乐而不用呢
: M9 Y$ _- l7 a9 e) e1 `( B- <?php
5 L3 Y3 D; b% `, n5 v
) _9 L; m; n1 o9 G& T! e# z) z- define( 'ABSPATH', dirname(__FILE__) . '/' );
9 A$ f: s; d! ~. Q# W0 V ^ - require_once( ABSPATH . 'wp-config.php' ); F: z- A/ e+ g6 W v L
- require_once( ABSPATH . 'wp-settings.php' );
1 c+ O" w9 X0 T6 Z: e* v - require_once( ABSPATH . 'wp-includes/class-wp.php' );1 M( L: w% b& L. _4 G
- require_once( ABSPATH . 'wp-includes/functions.php' );
* \$ W8 C+ Q x! ~! q - require_once( ABSPATH . 'wp-includes/plugin.php' );
2 S+ ?( _% d* S0 Z9 v- O) |6 o+ x - $title = $_POST["title"];; v3 P' i6 t! q! |+ S' \7 @* Q
- $content = $_POST["content"];
& I% Y; s* @$ }- E2 r - $tags = explode("_",$_POST["tags"]);5 e/ S+ Z& ~9 Q8 q
- $cate = $_POST["cate"];* q# Q0 W9 m9 @/ \% ~
- //print_r $tags;
( `) ~: u, Z3 m - //; B2 O% b' c0 S% s* Y( d* v6 N
- //
* |8 g0 O9 y/ k2 l - $wp = new WP();( W3 t" L( Q6 j
- $wp->main();
. E) O( l/ N5 J - //
6 H! a- z. E! \6 o, b2 Q+ n - $my_post = array();
0 z/ X* ^4 @0 k( ~ - $my_post['post_title'] = $title;) K6 o) ?9 Y' Q b
- $my_post['post_content'] = $content;" ?7 g0 _: o/ z
- $my_post['post_status'] = 'publish';
) ~( W. b1 ?& I) C( b0 {# V - $my_post['post_author'] = 1; |$ u/ g) |/ Z
- $my_post['post_category'] = array($cate);7 I+ y" G& S! {! n z
- $my_post['tags_input'] = $tags;* x2 C! w- v1 I* e5 Y( Z
- //$my_post['tags_input'] = array('tag1', 'tag2');* m7 l: U Z1 J* _4 s0 d3 g/ G) x! ~
- //$my_post['post_status'] = 'future';: K/ V7 W8 t+ D
- ////$my_post['post_date'] = '2010-07-04 16:20:03';% l: q, K6 B( v) \+ J( b& `
7 ?, `) E+ M$ j/ ]- // Insert the post into the database
* M" C0 T5 _8 {& ~. c1 n; A. V/ ~ - $ret = wp_insert_post( $my_post );
0 Z% {4 t: r$ m* [) l8 J - echo $ret;
# s: E! j/ U- Z/ S7 n: [' f - ?>1 b6 n' C2 q; x. J+ \
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气
( X. J. @7 S4 e1 Q( a2 L2 }- <?php0 U* g3 }% R; g( Y& H# Z! o7 L
- define( 'ABSPATH', dirname(__FILE__) . '/' );
' w/ k5 Y9 Y7 r% Q3 r - require_once( ABSPATH . 'wp-config.php' );
( n7 t+ s- G6 n1 s% y - require_once( ABSPATH . 'wp-settings.php' );' b: I7 C# A! \- r( w9 Y( u, u
- require_once( ABSPATH . 'wp-includes/class-wp.php' );9 q6 _# F; P0 F2 a( }+ n- }1 m
- require_once( ABSPATH . 'wp-includes/functions.php' );
+ }: i9 r1 Y/ J) e - require_once( ABSPATH . 'wp-includes/plugin.php' );8 l& z H: d3 L9 I$ ^
- $pic_name = $_POST["pic_name"];
3 f. Y6 ~, L% P8 A. w. N - $pic = $_POST["pic"];
3 q+ F) p( n$ `, V& i4 ?$ F - if($pic != "" && $pic_name != "")" A( q2 ^ k& p6 E. d
- {: h5 k: {9 S! @% X3 O, `
- $path = "images/";
, \- y$ ~. Z1 D& w# n9 a( Z5 K - $pic_name = $path.$pic_name;
$ [' D! q! b! u6 [. {/ @ - echo $pic_name;
1 [) K5 k9 f" @& k - echo "||";/ U3 n" M% r$ `: z& y
- error_reporting(0);5 f( J9 N6 V4 N1 Z7 c
- mkdir("images");1 K) k) a# W8 x! w) k4 d9 k( {
- $handle = fopen($pic_name, 'w');4 x) L0 X% X& L5 f# W# z( ~( {6 e- @
- echo fwrite($handle,file_get_contents($pic));
5 {: F9 g6 u- ~3 T9 ] - }
3 B" g6 y* v: P* q
* @# V9 c6 W2 Q9 o- $title = $_POST["title"];: l2 z F: x0 i, V; _$ X/ h7 k, ?
- $content = $_POST["content"];; I E; t2 X5 u$ ?$ P1 w @
- $tags = explode("_",$_POST["tags"]);2 ?! `2 n) ~0 J: ?6 X
- $cate = $_POST["cate"];
% B, o" S. h$ }; H - //print_r $tags;
: `3 K! m- o7 L/ w* N - //
d- z8 d& \; B/ t" h! V. F - //+ R; Z A5 m: @4 o) [
- $wp = new WP();
" y& h& g+ J; I2 c$ t4 D( I. ? - $wp->main();
- s' o+ s1 W$ f7 c - //: ~" L7 ^' o0 B# t( g9 W5 ]! G
- $my_post = array();
( Q8 w( L9 W6 K# H4 U- B3 b+ [ - $my_post['post_title'] = $title;
& u) C6 s* A( s$ H9 y5 y - $my_post['post_content'] = $content;
w" t6 u6 ?7 f0 A; J6 l) {2 x; q - $my_post['post_status'] = 'publish';
* W( ~* b. ?1 F% M - $my_post['post_author'] = 1;5 o ~8 Z, l% E4 s0 v* ]
- $my_post['post_category'] = array($cate);
( ?8 ^6 i6 o( N& I! i5 e, p6 ?( j9 z - $my_post['tags_input'] = $tags;
, { N1 d! \, a% i- O, H - //$my_post['tags_input'] = array('tag1', 'tag2');0 b* S% C: P+ [
- //$my_post['post_status'] = 'future';
1 ?* Q% e+ x' K% P' V4 o& k" G - ////$my_post['post_date'] = '2010-07-04 16:20:03';
7 B- Y2 S6 h1 c p7 m k( {
, v3 y- t- r) U ?- // Insert the post into the database
' J0 g1 L$ P# ~ |- X2 H - $ret = wp_insert_post( $my_post );3 _. D( l( ]% O) V
- echo $ret;
1 k' t$ y* }5 O5 _9 g% ? - ?>& p" w/ D) A- E- [1 ]
复制代码 我自己写的
( E+ f0 R& S5 v8 n4 g' b插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|