|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑
S& _# v1 s0 M2 b9 j# `) L* G4 ^( p8 _' n$ q W; \ x8 y$ l0 I1 o
直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持+ V' c6 c4 B5 l" T3 d
. s9 V( `% |1 Z4 B# I$ X
我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了
6 ^/ |+ k" K5 Y) O8 j6 G4 _) L" l& @3 H; Y! \$ |, j6 J' X9 p
方便实用 简单 何乐而不用呢
8 r3 x _/ L) l3 n$ B0 l- <?php
/ O. J- a9 ? }9 o9 ~
7 E, u' C4 t% y' ]9 x) b$ y- define( 'ABSPATH', dirname(__FILE__) . '/' );$ u0 a4 d s7 Y* A
- require_once( ABSPATH . 'wp-config.php' );
& }' H( q D+ o# c7 T- z. O - require_once( ABSPATH . 'wp-settings.php' );
0 E# x3 o% o p/ Y1 Y4 ^: `! B - require_once( ABSPATH . 'wp-includes/class-wp.php' );
* @) Y: |1 v U8 q7 t8 H J/ o4 J$ h - require_once( ABSPATH . 'wp-includes/functions.php' );
* A4 m$ \# }% O; K2 m - require_once( ABSPATH . 'wp-includes/plugin.php' );
+ v$ c* c7 i# X5 M$ J! n - $title = $_POST["title"];+ U% ^' ]7 H/ }! l
- $content = $_POST["content"];3 n+ U2 {5 Y! g+ g+ D6 f
- $tags = explode("_",$_POST["tags"]);
2 u* h, ~5 g) `" F0 j - $cate = $_POST["cate"];- w) V7 |. P6 h1 w7 |6 C
- //print_r $tags;
7 W# G3 z( W6 D: ?; H' M6 Q2 } - //
" R2 ?" j' w; D - //
2 T' M) r( U* g - $wp = new WP();( z: ]6 c/ {4 O
- $wp->main();+ b( T7 f7 g! }; P" W
- //
) A( {7 F+ n/ Q/ A - $my_post = array();
$ E0 l+ E9 P1 a0 N9 U: M - $my_post['post_title'] = $title;
+ {! W; V) z _! i# m3 q3 @' C+ [ - $my_post['post_content'] = $content;
' t5 J5 T9 Q1 Z8 ] i3 u - $my_post['post_status'] = 'publish';2 L4 O% h' c: S: \. n* \7 f4 f2 n# y
- $my_post['post_author'] = 1;
2 `/ k. z: E2 Z y* |; x9 [ - $my_post['post_category'] = array($cate);
- _- b! _9 t, o* g' m - $my_post['tags_input'] = $tags;3 f. y _3 L, ^; `
- //$my_post['tags_input'] = array('tag1', 'tag2');
) {' T; B: _" c" J6 v/ i9 t" C$ u8 Q - //$my_post['post_status'] = 'future';
+ @ ~; H1 y' H, @ - ////$my_post['post_date'] = '2010-07-04 16:20:03';( y/ J5 N( z( s G Q% R) [$ k
- ( O2 n# t9 n7 j. B
- // Insert the post into the database
. K: |5 I+ c C* e4 I$ F6 u - $ret = wp_insert_post( $my_post );! d& R" {6 O. T
- echo $ret;. Q! w+ I2 X1 P* [1 _/ }
- ?>- ]) Z( T& C, g! ~ Y$ [
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气- 4 c$ x5 R- X6 E
- <?php
# P; o/ j5 Y8 \4 u7 B6 [# Y% y - define( 'ABSPATH', dirname(__FILE__) . '/' );% P E. N! z- d( Y) X2 d
- require_once( ABSPATH . 'wp-config.php' );9 Z4 j1 v% S8 s: _8 y
- require_once( ABSPATH . 'wp-settings.php' );4 T4 [2 D$ u" o1 M+ w
- require_once( ABSPATH . 'wp-includes/class-wp.php' );
7 \' g$ R$ C" M& { - require_once( ABSPATH . 'wp-includes/functions.php' );
( Y5 Q9 }2 s6 c2 u - require_once( ABSPATH . 'wp-includes/plugin.php' );
- ^( g5 e, H) D( ] - $pic_name = $_POST["pic_name"];
# z" U+ H0 K4 A' v% a2 @# d2 B6 L - $pic = $_POST["pic"];: s% n2 b) @! j: R$ Y
- if($pic != "" && $pic_name != "")1 B2 p" {, F( O) `6 ^
- {" ?3 L) V& ^: o* a
- $path = "images/";
6 S. a* ^ v# G0 W9 i1 Y& _ - $pic_name = $path.$pic_name;" G+ P* F+ `, S! v
- echo $pic_name; m# L( Q% N* x; o. P8 h$ c* {
- echo "||";+ W! H& O1 j; I. o: l
- error_reporting(0);* g: v* V) e1 e$ m
- mkdir("images");3 ^; ]3 c5 z4 z7 ]1 z4 L
- $handle = fopen($pic_name, 'w');
: r' @* X4 l6 v8 w+ _6 N6 Q- o- N - echo fwrite($handle,file_get_contents($pic));# H7 q; k7 k- \0 |" U$ S
- }
0 b8 K; ~( p# \0 S
+ `" R6 W; m" r. y/ H3 F+ Z' z. X- $title = $_POST["title"];) G% {* Z8 }7 f
- $content = $_POST["content"];4 n; z1 W: e- v2 z+ }. v7 p& m/ b
- $tags = explode("_",$_POST["tags"]);. e* J; w" I. M( ?- H3 q6 U: M
- $cate = $_POST["cate"];
( n$ f+ |1 i% S- o, V% M - //print_r $tags;& ]' e/ z# x1 V' x2 S
- //, S3 m* D O. x
- //
( V T* j. w: P0 M7 q- c3 P - $wp = new WP();, {5 K, D( |9 \0 O
- $wp->main();
% W6 o( e+ j* n4 O. ^0 } - //
( f' U$ P( P+ \/ V; [7 H - $my_post = array();
2 }6 ~( N: n1 ?- { - $my_post['post_title'] = $title;
$ b2 B7 ^+ J7 Y* T+ u - $my_post['post_content'] = $content;0 \* x% l4 m% j- |7 G
- $my_post['post_status'] = 'publish';2 Q' ^7 V* ?( @' h. v7 {7 S
- $my_post['post_author'] = 1;8 ]$ h* t7 N5 p/ l
- $my_post['post_category'] = array($cate);4 v C. ]# n* ]8 Y: Y
- $my_post['tags_input'] = $tags;
+ m5 ]1 h0 D. _" N) K9 v - //$my_post['tags_input'] = array('tag1', 'tag2');5 W: u) G5 F- T( D& f: j" X( R
- //$my_post['post_status'] = 'future';
- u: o" ~. w6 ?4 Q - ////$my_post['post_date'] = '2010-07-04 16:20:03';. a/ B) G; W! [% ^3 P4 O
- a+ f" P" p7 b5 c) V, _: h4 q
- // Insert the post into the database
2 ]- d# G( G5 h: j - $ret = wp_insert_post( $my_post );! T, Q( y2 E% t* E
- echo $ret;& p; T a8 e( \% v
- ?>* e# A' c; A# z6 H& G# `/ t
复制代码 我自己写的2 K+ [0 W5 k* w& l4 p N# m" `: ~7 l# ?
插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|