|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑
: m6 v' c9 Q& n- z& E# f2 T- `8 f6 i/ l! G; C |6 P' ~: Z/ i* o
直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持0 ^8 F% f0 p. q4 E( P* j
" z% q8 g6 X! Q$ N# }5 d0 O
我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了
/ T3 F: c+ ?' g6 _7 k) i. K' M+ a0 p, j% c
方便实用 简单 何乐而不用呢
( g* S" K0 I2 l; S. Y- <?php
& M! P- n8 ?- _5 r) D
, N+ Q1 J- `6 U5 k2 a: J8 y- define( 'ABSPATH', dirname(__FILE__) . '/' );
! U4 Y+ I4 o `& X& k - require_once( ABSPATH . 'wp-config.php' );6 q0 l% f; |& w8 K
- require_once( ABSPATH . 'wp-settings.php' );% u# B \4 l; K! Y; Q& N% A
- require_once( ABSPATH . 'wp-includes/class-wp.php' );
5 }5 u# X Z' C6 x- }9 W8 q! h6 p( a - require_once( ABSPATH . 'wp-includes/functions.php' );* n% G4 M4 W: u7 @
- require_once( ABSPATH . 'wp-includes/plugin.php' );, B4 m! y/ u* }) _
- $title = $_POST["title"];8 K; p0 o2 d d5 V2 |
- $content = $_POST["content"];$ h- v& a/ k4 k4 O$ y
- $tags = explode("_",$_POST["tags"]);; M4 K% u$ X* a; o; ~" s* w8 ^
- $cate = $_POST["cate"];
' Q- J9 z- J2 _8 g - //print_r $tags;
2 J- j8 Q6 h X1 |. g9 n' W( ^ - //
- f2 U$ p) P3 c8 [ - //
# W: e+ Y4 c" K7 j8 M" n - $wp = new WP();9 s1 A) C3 a/ Y1 v# M9 a
- $wp->main();
1 [) k; d0 b w, M. G( f - //
& y( n$ x; \" _1 X" R, O' m! p - $my_post = array();
( o! a f, Q; O d" G - $my_post['post_title'] = $title;) {- V, ^# ]% R2 I2 v7 Q# k
- $my_post['post_content'] = $content;/ K7 ]. _5 a9 x5 t0 d* j, @. f7 K0 W
- $my_post['post_status'] = 'publish';4 A) b$ d# i1 @6 _/ ~/ F4 c. S; w
- $my_post['post_author'] = 1;
/ U% ]0 N6 m/ L8 j - $my_post['post_category'] = array($cate);/ R% D9 W" E+ Z% [7 W
- $my_post['tags_input'] = $tags;1 r! p+ \; }2 `+ a# r3 z6 m
- //$my_post['tags_input'] = array('tag1', 'tag2');1 \+ C: ]% f' b/ e
- //$my_post['post_status'] = 'future';
0 Y2 e+ G7 P- \* H3 _ - ////$my_post['post_date'] = '2010-07-04 16:20:03';
9 H4 J/ K; o A% n, \0 j
^9 J) f. V8 }3 \% `! `2 n* ^- // Insert the post into the database$ \1 G: @ _9 z+ k$ O
- $ret = wp_insert_post( $my_post );! Q& P: u' _" w" b9 N
- echo $ret;2 \5 x; G# r0 z) _, k" o: G/ x$ o
- ?>
* u5 U' E- \7 v
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气
; g7 Y: a$ o& a. W- <?php
4 E5 ~6 \ A# s9 p6 y - define( 'ABSPATH', dirname(__FILE__) . '/' );
5 `9 A& s) l. E - require_once( ABSPATH . 'wp-config.php' );
9 H3 L- Z0 a! d" P) y: R - require_once( ABSPATH . 'wp-settings.php' );9 D i: v# K9 Z/ _) Z$ f% p( t, I
- require_once( ABSPATH . 'wp-includes/class-wp.php' );
& @/ ^3 I' t5 @. I6 ^ - require_once( ABSPATH . 'wp-includes/functions.php' );
2 w; t" X+ O9 Q# a; d* M - require_once( ABSPATH . 'wp-includes/plugin.php' );
! b# O0 ]6 Y* b0 P2 A& f - $pic_name = $_POST["pic_name"];
# _1 p4 q/ ~* ^5 @' K+ s, k - $pic = $_POST["pic"];
; y/ S; F& a6 ^# r5 [ - if($pic != "" && $pic_name != "")5 V; ^0 q4 ~% ?6 ?3 t
- {
; S; ~" w5 v0 b+ f - $path = "images/";. @+ h% j$ s6 D2 G) Q
- $pic_name = $path.$pic_name;% \, ?4 p. H6 E: V- P7 s% |
- echo $pic_name;" `" J: q. \: V
- echo "||";
. s9 g0 A: t) q1 y. g, k5 { - error_reporting(0);( J3 Y) n$ s! s
- mkdir("images");8 ]% O: ^- x8 m2 M
- $handle = fopen($pic_name, 'w');
- U- t* m: V2 S - echo fwrite($handle,file_get_contents($pic));3 D* c9 S' h) F! j
- }$ Q8 R; O5 b$ |0 ~
, x" t+ n" `. Y4 g- $title = $_POST["title"];
& W4 Y* P! a7 F+ F4 E; }' s- N0 g - $content = $_POST["content"];; w% h* {9 \7 l5 ?; t. W6 V- H
- $tags = explode("_",$_POST["tags"]);
$ ~' _9 L( m( Z; ]3 m - $cate = $_POST["cate"];( D- q$ [( Z: o. G$ P! w# P
- //print_r $tags;
* |% Z3 B9 G9 |2 H - //2 a+ N/ C" j* K$ }3 K8 u
- //* o4 K4 ^8 U; i8 ]2 @# O6 ]! x8 l" C
- $wp = new WP();5 u' e1 r; W% |/ T, T
- $wp->main();% O! Y; ?* \, j1 L1 ?! z
- //
2 e8 r3 v/ V9 J g; e# |7 [' m3 j - $my_post = array();
( ~% b9 n# F: B( E - $my_post['post_title'] = $title;. j9 [; n6 X5 ?8 y5 N9 I* }
- $my_post['post_content'] = $content;
. T% u$ l) D* E: g5 o% I - $my_post['post_status'] = 'publish';/ c. Q7 c- m$ t8 \5 C
- $my_post['post_author'] = 1;
: |$ w4 C$ u3 h7 H5 H) f - $my_post['post_category'] = array($cate);
1 `0 ? l2 k: a - $my_post['tags_input'] = $tags;
; y( r* S0 h& p) j - //$my_post['tags_input'] = array('tag1', 'tag2');1 ]* X" g: t4 B1 e6 L# q# T) r7 O; I
- //$my_post['post_status'] = 'future';( J2 G* ?# e$ e/ C
- ////$my_post['post_date'] = '2010-07-04 16:20:03';
& I" _ V' F0 k. h# _+ r8 |
& a3 O4 j& @6 D" j+ e/ ^- M$ l. m- // Insert the post into the database
( b# N8 T% Q9 H( l' D - $ret = wp_insert_post( $my_post );
# `1 V, R# R' a( z, `- K - echo $ret;& j* r( R: I' ^4 S$ p; }
- ?>
- F) Z/ \! \/ A# o( p" B. ~' Y+ e2 D
复制代码 我自己写的
3 Z' H( j+ X1 ?0 E. d插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|