|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑
* ]1 w4 s' h! c3 H, b* G' }5 _3 e0 {- G W
直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持0 I, v$ Y- Q1 w0 F" O
6 W! o* H# r% E+ H4 o7 C1 e! @5 \
我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了
& J( y {3 y$ E9 T% U3 a% }5 o' s T+ \
方便实用 简单 何乐而不用呢
% C- D% J6 K/ m& s2 {! _! ^2 K- <?php
7 E1 m7 Y4 e8 t- Y# i
5 P( R9 k8 e# p3 Z! i8 t8 x- define( 'ABSPATH', dirname(__FILE__) . '/' );
8 n5 B$ d. L6 c - require_once( ABSPATH . 'wp-config.php' );9 _, Q# a3 U% B5 y4 g& Z
- require_once( ABSPATH . 'wp-settings.php' );
& U% W" v _% `; F, ^; v - require_once( ABSPATH . 'wp-includes/class-wp.php' );
! M% J1 [% N( V0 Z - require_once( ABSPATH . 'wp-includes/functions.php' );
" w$ h1 f" p n9 ?9 [7 r/ G - require_once( ABSPATH . 'wp-includes/plugin.php' );
6 B% C; g! Y. {) H6 X. ?9 x2 r - $title = $_POST["title"];
1 ]) s+ n, ]$ D& o - $content = $_POST["content"];; ^8 C1 S) Q1 p/ n$ ~
- $tags = explode("_",$_POST["tags"]);
+ o) t4 v) }9 ]7 a- g9 _ - $cate = $_POST["cate"];
* L7 b5 y7 l' f' J1 M; C- Q" t - //print_r $tags;
1 Z' }: U! |, H. b) j0 k - //
- K5 A+ e7 x g K6 D0 j - //* o) P; {$ u- ?( I1 W
- $wp = new WP();
5 t+ k) g _# H+ _: r3 d! O! c- _ - $wp->main();
. q; C7 r/ {) B( f, U) V h - // ~5 S- U. a1 y6 \) z6 M7 i
- $my_post = array();
" f$ c. L4 M! F) f - $my_post['post_title'] = $title; V5 L6 Q/ z5 O8 B" A
- $my_post['post_content'] = $content;
4 z1 e$ S; J( o% M - $my_post['post_status'] = 'publish';
4 I0 _4 |2 {8 ~; ^ - $my_post['post_author'] = 1;' \! Z. \5 J6 A+ j
- $my_post['post_category'] = array($cate);9 J" s* d. Q* S% M
- $my_post['tags_input'] = $tags;
3 L; T- ?& D! |% _2 ?/ g - //$my_post['tags_input'] = array('tag1', 'tag2');
, c) T" B+ u% K) [% ^* G0 n - //$my_post['post_status'] = 'future';
$ H# j2 R% `- D5 Q. [ - ////$my_post['post_date'] = '2010-07-04 16:20:03';
a0 I( J' v& c9 s* \, Z: P" x3 P
4 @8 _$ c i! q4 t1 S; L* g1 i, N- // Insert the post into the database
- L+ z8 I; Q' X8 F5 W4 n- {# h - $ret = wp_insert_post( $my_post );
" W+ a; {$ s6 {) D6 J - echo $ret;
( c. x0 ]6 }0 h0 @! B9 f- @ - ?>
3 U' g/ @2 g% Y0 ]6 [. C) I- q
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气
1 r" j3 B' I9 K2 |4 c% C) e- <?php. k% D0 j: }, `1 }1 a( S4 g
- define( 'ABSPATH', dirname(__FILE__) . '/' );
# n* j- m3 l) X - require_once( ABSPATH . 'wp-config.php' );# j9 L7 S" {2 a' Z U
- require_once( ABSPATH . 'wp-settings.php' );
' y1 d( g$ ~5 h) Q/ X - require_once( ABSPATH . 'wp-includes/class-wp.php' );
0 w) @3 M8 B: l! Y - require_once( ABSPATH . 'wp-includes/functions.php' );
4 F4 z' t1 A/ g5 G' ? - require_once( ABSPATH . 'wp-includes/plugin.php' );) H( [( Q- A# p2 p/ I
- $pic_name = $_POST["pic_name"];" K4 u- k8 P% G1 Q
- $pic = $_POST["pic"];0 ?- d' A2 u4 C: [: z7 F
- if($pic != "" && $pic_name != ""): ~" x; X- W. {' T/ g. {
- {
9 X; s# W5 W) y - $path = "images/";
7 o5 g E5 N- t# m* Q0 [2 P3 ]! p - $pic_name = $path.$pic_name;$ X/ j+ @; O3 p1 B
- echo $pic_name;
8 i9 W1 z/ |/ q2 F- S7 K, Y! U( ]- q - echo "||";6 u- q% R( Y d; U9 [2 A( D
- error_reporting(0); S0 N+ r4 I# S1 y
- mkdir("images");
. i( Z$ r3 H, Q) ]& W8 M y - $handle = fopen($pic_name, 'w');
5 r6 M6 E& v+ N/ ? - echo fwrite($handle,file_get_contents($pic));
5 f8 x+ O: R% h: {1 C - }, c, _1 ^- O0 z8 i4 a6 s
) V' Q( \3 J* t- k; }6 B6 O- $title = $_POST["title"];1 o7 h- r5 ]5 F( D+ e
- $content = $_POST["content"];
' E" D; x6 P5 m! S' { - $tags = explode("_",$_POST["tags"]);
* n; B3 j& X7 G; u/ z6 O# ] - $cate = $_POST["cate"];
+ Z3 G- Q9 `3 Y( R# b3 S# Y - //print_r $tags;
' ~6 _/ J) y6 ]4 x. A1 G$ ^ m - //" g' H( c( K3 \
- //8 F& Z+ }, G G$ M' y: w0 ^! N
- $wp = new WP();* d+ i1 X0 H4 L5 |
- $wp->main();
) Z. t! O3 D& i3 ^ - //) o8 d' D( Q. d {& c
- $my_post = array();
; b! F. g7 [7 p0 a k$ f5 r7 ^1 b$ c - $my_post['post_title'] = $title;
) e ?7 y3 _5 s. U. L. s! g - $my_post['post_content'] = $content;
- e! K% w6 @. `; A2 j4 w4 n - $my_post['post_status'] = 'publish';& ^. P# Q$ `+ |9 i
- $my_post['post_author'] = 1;
, a% X+ e! ~$ N* P3 l6 i - $my_post['post_category'] = array($cate);( g* W5 o+ Q! z! J3 {0 o# Z
- $my_post['tags_input'] = $tags;
( s/ W8 M& d9 y - //$my_post['tags_input'] = array('tag1', 'tag2');
6 T! J0 Z2 {% U _3 J& Q( x - //$my_post['post_status'] = 'future';$ b6 }0 X4 P; O2 O1 v
- ////$my_post['post_date'] = '2010-07-04 16:20:03';1 {6 j1 q. {9 [- F& u; }
( x4 M. A- `% g- // Insert the post into the database9 b6 R4 K0 o; B9 f! U9 ]0 e% n! {
- $ret = wp_insert_post( $my_post );% e9 a! q' R9 z
- echo $ret;
( I" ?4 z% X# P5 f" K6 t: l2 Z+ {5 L! S - ?>& G' M0 K! V8 T; j7 N
复制代码 我自己写的/ ]. Q5 S9 r; j- V
插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|