|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑 8 r7 u i/ d9 P
1 [2 B/ c6 u# P" g0 [直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持1 ? a8 M( I& |- j7 d3 n$ i
; Q( c! S: _! y7 K6 P
我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了
0 {$ H7 F6 R! R( _5 K# y) A* Y* ?% `$ e& v& S# [+ ?
方便实用 简单 何乐而不用呢- ( Z7 Q; n' t6 W" ? l6 t0 N k
- <?php
0 u" E" K E& k1 B* t' I5 W - + {) q' ]7 `6 R; P. g$ W; O- R$ t' t
- define( 'ABSPATH', dirname(__FILE__) . '/' );
# Z2 X# V" A/ Z. V l1 u7 [ - require_once( ABSPATH . 'wp-config.php' );
" X1 f0 H6 n4 X% u, Q. Q - require_once( ABSPATH . 'wp-settings.php' );5 z, Z' ]+ U4 v0 _) f
- require_once( ABSPATH . 'wp-includes/class-wp.php' );
7 v% Q% @' t7 w; u8 Z2 E - require_once( ABSPATH . 'wp-includes/functions.php' );5 m2 E* K& K( c- E
- require_once( ABSPATH . 'wp-includes/plugin.php' );, n/ }0 P; e4 S6 H3 H% x# r' {
- $title = $_POST["title"];# Q* U% A6 L& N3 W) G/ g
- $content = $_POST["content"];3 C, n0 o- {. x2 t
- $tags = explode("_",$_POST["tags"]);
. z- o, w! h1 \+ R% [ - $cate = $_POST["cate"];, g- ~2 g0 p3 a1 o6 g
- //print_r $tags;% z( Q6 x; m6 ~5 h) R
- //
* K* q6 m6 P2 q: B - //
. l. @* X! V5 O - $wp = new WP();
) Z# C! ^; [9 c' D( h - $wp->main();
5 Z8 P( c2 f6 O - //
% o6 L! ~) C6 t* X( r- X" j - $my_post = array();
+ ~% Z( f: I% E% l0 f! d9 h - $my_post['post_title'] = $title;, y) e6 `1 p( w) o( @8 p, s3 b
- $my_post['post_content'] = $content;! Y% R* `) V9 w$ ]! ?" N6 L
- $my_post['post_status'] = 'publish';
1 _, n5 Q0 G2 e - $my_post['post_author'] = 1;; i; S2 M4 `) H7 Q3 f
- $my_post['post_category'] = array($cate);3 V% [. ^2 @! ~4 a0 L
- $my_post['tags_input'] = $tags;+ c3 U; \3 E+ U: C1 N: f5 x3 b# a
- //$my_post['tags_input'] = array('tag1', 'tag2');' J: E% M. Z5 O
- //$my_post['post_status'] = 'future';# O" p6 |( q. g' D; v0 Z
- ////$my_post['post_date'] = '2010-07-04 16:20:03';7 R' C% L+ w( f3 @+ u) ~
% b9 k) A) }6 {- // Insert the post into the database c7 M! L. T2 z' S" y
- $ret = wp_insert_post( $my_post );
! ]' y8 D* ~: i. h! C - echo $ret;$ ]- L- t1 w- P+ F, n+ Q; T- I8 J0 _& `
- ?>9 D7 w. ?" q: q/ H" I- X1 z4 B: ?
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气- " B; n ^* X8 d
- <?php
5 G- \9 v9 `4 ]6 V3 h - define( 'ABSPATH', dirname(__FILE__) . '/' );# S/ O0 X8 {5 l& n" U7 W
- require_once( ABSPATH . 'wp-config.php' ); h2 i# N5 p: z: _
- require_once( ABSPATH . 'wp-settings.php' );6 _5 y+ J) J1 f
- require_once( ABSPATH . 'wp-includes/class-wp.php' );6 K2 I7 b8 f9 s0 \
- require_once( ABSPATH . 'wp-includes/functions.php' );; x `" C3 d! C$ e! t
- require_once( ABSPATH . 'wp-includes/plugin.php' );8 ]0 y, E! s- X! `5 R& W
- $pic_name = $_POST["pic_name"];
2 R* O' L1 v" W& U3 j2 i3 T1 s - $pic = $_POST["pic"];
) _- f& g0 V# f; y5 ~& _ - if($pic != "" && $pic_name != ""). F4 E2 E- K. H" \
- {
1 z- v3 u7 b# q6 Y" {: ?6 @/ f3 H+ a - $path = "images/";
+ z8 Q8 U8 e) Z( C* D/ R5 u7 t' N - $pic_name = $path.$pic_name;
; v) {! E0 n8 p4 Z' A - echo $pic_name;
; b7 U, S$ [: }$ ` - echo "||";, ]$ N( B7 a7 [
- error_reporting(0);
* t* ]0 d6 z5 S$ W2 W5 b - mkdir("images");
: {6 o3 N x6 t# [. {8 {- R - $handle = fopen($pic_name, 'w');& S" ^4 m0 w" [
- echo fwrite($handle,file_get_contents($pic));9 H7 l5 K/ U" @5 P
- }
0 O) h" L. m2 q' H
" c' E: y; u- r! ?- $title = $_POST["title"];
6 x2 I( c- w; N& q5 }- \ - $content = $_POST["content"];
. W$ p+ O" M4 P; z1 w" X B. r - $tags = explode("_",$_POST["tags"]);
5 P: \0 l% U5 s( P( R/ j - $cate = $_POST["cate"];: J! m) E" Y5 u5 \% Q9 b
- //print_r $tags;+ {7 g2 i: d& I+ H
- //) w( R. z: S4 a K1 a: X% ]
- //+ E- K8 B! O# z2 h; p
- $wp = new WP();. `7 y! u; L2 p
- $wp->main();/ k& a; P. |; ~/ [6 B0 j
- //
& i/ H4 M+ B. Y: ^; l9 Z! e3 E# \ - $my_post = array();, f' q F2 ^$ w" q) A6 M
- $my_post['post_title'] = $title;& |: w5 y* j& P/ C, s
- $my_post['post_content'] = $content;
8 t% p2 \( g+ p7 T - $my_post['post_status'] = 'publish';
1 ] V; M0 y M6 K F - $my_post['post_author'] = 1;+ ?* [/ s0 }- t' c3 \0 U3 k
- $my_post['post_category'] = array($cate);
T9 z; n! X: R) k/ M - $my_post['tags_input'] = $tags;
1 M- y) h) t! Y6 o6 |2 D - //$my_post['tags_input'] = array('tag1', 'tag2');1 P7 _& a& {% j2 m
- //$my_post['post_status'] = 'future';: q: r" U. |$ a! ^' s* y
- ////$my_post['post_date'] = '2010-07-04 16:20:03';
W3 g3 `* t5 \( q7 ]* Q
; M0 t7 _3 Y1 f/ K- // Insert the post into the database
: Z& @# E: w) n; X2 L7 E - $ret = wp_insert_post( $my_post );
/ k/ ]" L4 W& |9 l- e/ G# g4 \2 v - echo $ret;% n9 d! ]4 F$ f# ^. M
- ?>
1 L6 g7 S9 d& u3 w5 z
复制代码 我自己写的
9 |- b2 U2 e- N" q. n+ Y: F* m插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|