|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑 2 y. k! O! ^% x/ ]$ b. n
, S7 Q# f" P3 i: R" K; U
直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持4 _5 B" V0 {2 e g. s3 X
5 ^. ?3 ~: ?" K7 i! Y/ m* f) W3 K我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了
8 g/ I/ \6 I; J' r9 G
" ~' q6 e+ D4 ]* |; M4 M: A( @. _方便实用 简单 何乐而不用呢- 0 Z6 a+ i+ U3 p* `/ w L
- <?php
1 s" p/ Z" f; C x" z3 V - 9 d. V) e a. |& K$ P5 V9 C
- define( 'ABSPATH', dirname(__FILE__) . '/' );
+ y; q2 Y/ a+ R2 C7 E - require_once( ABSPATH . 'wp-config.php' );
; H5 E0 B: l0 J) A; v - require_once( ABSPATH . 'wp-settings.php' );: d" @1 Y$ {' x' F
- require_once( ABSPATH . 'wp-includes/class-wp.php' );
, {* m: m" a" ] - require_once( ABSPATH . 'wp-includes/functions.php' );
. D4 r" n; M) B7 g' W - require_once( ABSPATH . 'wp-includes/plugin.php' );+ }" G$ A r* P, B# I5 D2 s
- $title = $_POST["title"];
0 C0 s; Z7 r g+ |5 A1 W - $content = $_POST["content"];, r5 @/ W# O2 M. ^5 e
- $tags = explode("_",$_POST["tags"]);
5 P% f# p$ t% R& `5 `) a - $cate = $_POST["cate"];8 r0 f, c/ G' y& q
- //print_r $tags;
& _3 ?. p: ?' ?7 | - //' e. b3 L& e6 i3 h( x7 t" I; X: e
- //
# ]( y; L5 | c; ^1 A - $wp = new WP();2 _: p- Z: e$ y# L }8 r
- $wp->main();
0 l% |5 T) N0 d - //
/ t: j% j1 f9 B - $my_post = array();
4 r6 D: M" o4 U9 F( @% ?4 Q) [, y. z - $my_post['post_title'] = $title;
Z# \% ?) H9 h2 h - $my_post['post_content'] = $content;6 i) A$ r2 |; ?
- $my_post['post_status'] = 'publish';
5 l! M s4 q$ \1 c P) y - $my_post['post_author'] = 1;! U# O' S) W; Y) O) C. z7 ?
- $my_post['post_category'] = array($cate);% g! Z: _) ~; m/ t) v0 U
- $my_post['tags_input'] = $tags;
& r$ M" E# Z) r) @# ~* `' G# _ - //$my_post['tags_input'] = array('tag1', 'tag2');& v0 x" B, [" V
- //$my_post['post_status'] = 'future';! z7 n5 f* s7 |( \) h. K @0 ]
- ////$my_post['post_date'] = '2010-07-04 16:20:03';
, X" k3 A0 m! o- x1 a3 m: W2 c1 A - ) Y- o* c; u0 |/ } T3 f$ M
- // Insert the post into the database
$ |3 x! O) ]0 b7 c - $ret = wp_insert_post( $my_post );
3 l1 O4 ? I" t$ k2 d - echo $ret;
4 o+ s# u0 [$ G& F- D S" O4 [& J - ?>4 s% e7 c! z+ Y( Q
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气
" b/ J# i* N2 U! K- <?php
- D6 [" ~8 l# }! p4 ^ - define( 'ABSPATH', dirname(__FILE__) . '/' );
1 b7 Z9 F" ] t) ]% I - require_once( ABSPATH . 'wp-config.php' );2 i! V* _: D! A% T; x- ]9 P* c
- require_once( ABSPATH . 'wp-settings.php' );
2 r0 m' W4 K& x; c - require_once( ABSPATH . 'wp-includes/class-wp.php' );
. ~1 q( T! g7 Z$ e4 [# F - require_once( ABSPATH . 'wp-includes/functions.php' );& [/ T( I0 z6 U# j9 P! _# z
- require_once( ABSPATH . 'wp-includes/plugin.php' );
: x( M" f1 X1 {: W- j( V - $pic_name = $_POST["pic_name"];3 @8 ?5 ]5 y; A2 c/ F- i, w) H" l- r
- $pic = $_POST["pic"];; L6 O. ~5 s. c) }
- if($pic != "" && $pic_name != "")- Q: K7 o4 p! y0 O
- { h1 j5 z' g+ H J' y1 r! K
- $path = "images/";; X" X5 Z5 B' N9 L" o& @
- $pic_name = $path.$pic_name;. `' l& V3 A* \3 t* f, f# S5 A4 w( s* c
- echo $pic_name;6 z" M8 U- g Y5 F. g0 m
- echo "||";
) M0 R! w S' s" g) ~ - error_reporting(0);- ^9 p3 x% c1 v c& u
- mkdir("images");
# P" S9 v4 C9 g# G7 E - $handle = fopen($pic_name, 'w');
: ^. U1 U. C" h9 q& |7 i. H - echo fwrite($handle,file_get_contents($pic));* Z4 E/ x' ]7 f8 ~! `* e+ G
- }2 t# W8 g* j- z3 W
- 2 j3 H6 \7 G0 ^# {
- $title = $_POST["title"];+ @' U3 x# q$ F0 S
- $content = $_POST["content"];
& d1 R' ^) o/ q - $tags = explode("_",$_POST["tags"]);
: h! P& Y* \3 T' y0 m - $cate = $_POST["cate"];8 N' w: m# @. r2 k. h* j
- //print_r $tags;
4 u+ v, n# M/ p' C" L) \8 `% K: a - //5 M$ ]- V) V. p! c8 J
- //$ u$ {+ p, d7 Z) @7 |% ?$ u
- $wp = new WP();: ?( ?# d* Y. K/ K" m: v+ f
- $wp->main();( p; P" |' O. g$ v. Y) I
- //, l; A' E6 t! ~; h1 V
- $my_post = array();4 p/ l- P7 q0 j) r0 u& T
- $my_post['post_title'] = $title;
7 H& v- _# V: E/ \ - $my_post['post_content'] = $content;
6 ^9 `: i+ v# ~ - $my_post['post_status'] = 'publish';
, M# B5 a, O) C7 Y' H# l - $my_post['post_author'] = 1;
9 p$ d( [! B# p - $my_post['post_category'] = array($cate);
% |0 x: a4 e$ b* t I" a# T - $my_post['tags_input'] = $tags;5 b7 r# B8 K6 {+ X' Y
- //$my_post['tags_input'] = array('tag1', 'tag2');3 k# t2 ?) E8 w# T/ G& k6 X' F
- //$my_post['post_status'] = 'future';
# I3 \, B$ f; ?; P# d - ////$my_post['post_date'] = '2010-07-04 16:20:03';
! O) \! u7 k0 i6 x p5 I7 X+ @ - 5 ^( x2 i" s; M& d0 J/ S
- // Insert the post into the database3 X4 `- H& `1 |+ q& G# D- ]) s; b
- $ret = wp_insert_post( $my_post );5 ]& n4 O, T7 o9 r' V, q& c n: J4 q
- echo $ret;" _$ @! X0 h4 F4 L5 H+ b: T
- ?>
5 Q+ d. P9 L' L4 i
复制代码 我自己写的
P# K5 \8 S$ l' T插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|