|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑 * U. j' l9 [7 @4 b9 q
! H0 W/ y. H+ |( b直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持& V7 m, ], t/ i/ g
8 t5 S$ A5 U5 [7 P& M# s) }) o我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了9 ~6 I4 o/ Y: N1 a1 V7 M6 e
8 u3 D9 o, Y. Y7 Z, ?- {' ~+ f
方便实用 简单 何乐而不用呢- 7 ?, ]* n* y& P- O, J
- <?php. V# k5 `; U/ x% M! j
" v {/ ?, G; Q* t6 @- define( 'ABSPATH', dirname(__FILE__) . '/' );( B3 h Y+ f$ H; Y. q- y- D$ q
- require_once( ABSPATH . 'wp-config.php' );
, D) c: t7 Y) ?( K- n - require_once( ABSPATH . 'wp-settings.php' );% w4 w ]9 T; Q8 p+ \$ J5 p
- require_once( ABSPATH . 'wp-includes/class-wp.php' );
5 W+ z7 h) j j$ y* h9 t$ q2 E5 \ - require_once( ABSPATH . 'wp-includes/functions.php' );
* f( Y2 M% D: @ - require_once( ABSPATH . 'wp-includes/plugin.php' );
6 B# S) n1 ]3 \1 `$ C" h - $title = $_POST["title"];
$ t* [5 F+ E% H3 Z! ` G - $content = $_POST["content"];
* e( l$ y9 L# O - $tags = explode("_",$_POST["tags"]);
* Q0 B& s( m. L( [7 E, P7 r& x - $cate = $_POST["cate"];
5 f! J% w8 D" y$ I- H - //print_r $tags;5 Z: w9 W) e8 @# D4 x% X
- //
4 c6 s7 C+ t& I# ?& ] - //
" h! w) p1 v0 D/ ]5 j; D - $wp = new WP();
3 J$ M A: i" b3 j8 C/ v0 D" i - $wp->main();4 \( L" @0 B# O, Z; J! I
- //: L% f3 ~5 g4 h6 C) X4 v
- $my_post = array();2 m" s; b+ `9 X3 X
- $my_post['post_title'] = $title;+ V; z1 F0 Y' R7 r6 R0 d5 b
- $my_post['post_content'] = $content;
* }5 i* i9 b) l - $my_post['post_status'] = 'publish';% w8 @ \+ n5 b* L
- $my_post['post_author'] = 1;7 Q- a5 [& l3 T* p+ w4 q
- $my_post['post_category'] = array($cate);
6 C& n$ ?2 f, K! g! e; ~ - $my_post['tags_input'] = $tags;
$ p) F8 ~: E' u( E% [! K! ~ - //$my_post['tags_input'] = array('tag1', 'tag2');& K! ` N+ F5 n, ?
- //$my_post['post_status'] = 'future';
`6 ?2 x @; U+ Z# L - ////$my_post['post_date'] = '2010-07-04 16:20:03';
. b. j! j; J( F. H0 ]
9 M5 c. `1 m \' S- // Insert the post into the database' q( O/ e( ?/ m7 R( N
- $ret = wp_insert_post( $my_post );
" [* ?: {6 `5 p6 d - echo $ret;5 Z, w' P0 b6 ^% s) k
- ?>6 n- Y# `! ?3 h+ l# p
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气- + Y$ _8 j' F, s0 c2 C/ K
- <?php
; v1 g% M( x' M/ \6 o7 H! z - define( 'ABSPATH', dirname(__FILE__) . '/' );
: \1 L3 {# O4 v0 B! X - require_once( ABSPATH . 'wp-config.php' );5 w i; V% F' d- L& x" Q
- require_once( ABSPATH . 'wp-settings.php' );$ Z" k# i5 g; N0 a+ U% w; n
- require_once( ABSPATH . 'wp-includes/class-wp.php' );
6 `% M8 g U3 ` W4 N - require_once( ABSPATH . 'wp-includes/functions.php' );0 N S; [9 i. g+ {* q7 {/ P* V
- require_once( ABSPATH . 'wp-includes/plugin.php' );
. D/ q6 Z3 l6 h - $pic_name = $_POST["pic_name"];! I8 t$ V! r& T/ z) |! D
- $pic = $_POST["pic"];$ W u3 Y& y+ S* y, Z
- if($pic != "" && $pic_name != "")
. ^# B1 v/ J0 c; ] - {& \; D0 S7 W% S: f5 T
- $path = "images/";2 D/ A" ]* x0 Y0 J6 _
- $pic_name = $path.$pic_name;
2 d1 }0 o. R: e - echo $pic_name;, o8 U" S. p! a$ I6 _; A# c
- echo "||";
f6 t5 z& d- q0 D - error_reporting(0);
9 e8 C- p& a6 O2 W5 ` - mkdir("images");: O6 E* w4 f: v1 R( J3 H
- $handle = fopen($pic_name, 'w');% b6 z2 K1 [5 D* l
- echo fwrite($handle,file_get_contents($pic));
' V0 @4 j2 q* N+ v - }% q7 K: e; {) O+ Y3 c+ k
- - ^1 G- s1 R. Z: X
- $title = $_POST["title"];
; V6 D! a( M, A2 s - $content = $_POST["content"];
$ L; H) k9 b; ^; L# Q- r - $tags = explode("_",$_POST["tags"]);
% G3 w2 g& o$ \! `+ C( V) K - $cate = $_POST["cate"];4 R6 f# p, I% w" D H
- //print_r $tags;& ~7 @& p5 H- N+ j. ?
- //
1 S3 m( {4 g0 x1 ~ p* |5 z2 L. z - //' r) U0 n% S% [
- $wp = new WP();! x$ Q: m& h9 O- M) a
- $wp->main();9 E' z: _6 {7 m- l) R7 D
- //
- `; U- P5 X8 n A; s - $my_post = array();" ^1 j1 F5 x( e( {% }
- $my_post['post_title'] = $title;! {/ E+ Z6 q! R: |: D* S5 ^0 Y7 a
- $my_post['post_content'] = $content;" D9 |4 \3 n# u9 H k. ~; [
- $my_post['post_status'] = 'publish';
8 p& @% x2 U& Z y - $my_post['post_author'] = 1;
; X: E |+ j6 { - $my_post['post_category'] = array($cate);. b; g) G d- Z0 {
- $my_post['tags_input'] = $tags;
/ Q7 r5 G1 w g) { - //$my_post['tags_input'] = array('tag1', 'tag2');
9 q! R, N5 L; K% r5 l0 U - //$my_post['post_status'] = 'future';
2 F5 G* |4 ~7 e& q0 s: x - ////$my_post['post_date'] = '2010-07-04 16:20:03';
+ I0 ~0 i3 b: ]) G& y - " v8 n5 ~. ]' x/ i; F5 M
- // Insert the post into the database
! K0 }8 \1 G! J - $ret = wp_insert_post( $my_post );/ I6 X! G% h; Q3 K' Y* T
- echo $ret;
% k5 d% J% U$ U6 I4 _3 o - ?>- x* z4 q6 l) s: K6 l* b) S& l+ d3 Y
复制代码 我自己写的
5 Y0 L# p0 q P5 o; l2 k( z8 w# e插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|