|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑 ) q6 j1 q9 C4 l# O" G% P
; A8 \' j- m+ f* x直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持
( ?& K. ~& p' h, b6 H
7 p& ~5 T, y! @/ _" J% C0 H我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了0 ?, Q6 t# l/ X. G
6 e7 E2 f" r5 t+ H! f
方便实用 简单 何乐而不用呢
u0 j7 k$ @% D# n) w4 n2 d, E( g- <?php; k$ C# k. F. [4 L. c2 S( K- M
- " @, q1 I( g! v
- define( 'ABSPATH', dirname(__FILE__) . '/' );
2 Z1 a& X0 p7 Y1 r/ ]! { - require_once( ABSPATH . 'wp-config.php' );) l( J* s7 _: m
- require_once( ABSPATH . 'wp-settings.php' );: ?' X( Q0 `% S! U- h# m, d9 @
- require_once( ABSPATH . 'wp-includes/class-wp.php' );
" q {* J5 |6 ~" ]3 J; n - require_once( ABSPATH . 'wp-includes/functions.php' );
0 u& x3 L) G: u: Z - require_once( ABSPATH . 'wp-includes/plugin.php' );0 i$ D+ Z9 w* T6 |* q6 ~$ c; {& n; V
- $title = $_POST["title"];
/ Z9 `0 t U2 a4 C& j r2 b# Y, O - $content = $_POST["content"];6 x2 i/ i2 i5 W, a* Z
- $tags = explode("_",$_POST["tags"]);
) |4 l s/ i, [4 w - $cate = $_POST["cate"];
- k- O, [$ [/ n" X4 o - //print_r $tags;
9 H; p$ a6 K4 q5 C- d+ V% [ - //
% o0 N+ Y; z9 P9 _! q; p) e6 Z' c - //& {- u9 W8 X3 p# d6 i; g
- $wp = new WP();* H! s% ?' g7 x
- $wp->main();
- F Z5 c+ `8 I - //
7 Q- U; F; R& K3 Y - $my_post = array();
( |+ I0 s0 l2 R0 U* Z! _2 p - $my_post['post_title'] = $title;( `" H; u g9 a
- $my_post['post_content'] = $content;
" m- S8 {% ]* T& l2 E! L - $my_post['post_status'] = 'publish';
8 X8 N% s. S- @ - $my_post['post_author'] = 1;& @ Y2 Z8 L+ H, C& M z
- $my_post['post_category'] = array($cate);
9 @* o- w& o8 q - $my_post['tags_input'] = $tags;
& v8 v5 `5 c# C6 |& J - //$my_post['tags_input'] = array('tag1', 'tag2');, _: {* M/ L2 i8 c/ n7 z- T
- //$my_post['post_status'] = 'future';
4 F+ X, v6 P( [/ o& | - ////$my_post['post_date'] = '2010-07-04 16:20:03';
2 T( G6 t- W; ]$ q+ v! W# J+ o
* B; `9 d/ m5 x9 r) \- // Insert the post into the database. U* |- T8 ]7 \& o7 T
- $ret = wp_insert_post( $my_post );
( M" O9 s0 h* T r- K% _! ? - echo $ret;" G3 g' C5 _! i- L1 L6 B' U6 K
- ?>0 Z$ p" v: g- C4 z Z) P$ W. _. W
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气
& k' p" I& Z6 w, b- <?php3 B0 e0 i, m4 O3 `* k F
- define( 'ABSPATH', dirname(__FILE__) . '/' );
3 `+ H, R1 ^$ M9 {6 q: c' |7 F b - require_once( ABSPATH . 'wp-config.php' );
5 @; b- m5 N0 Q" [) G( Q - require_once( ABSPATH . 'wp-settings.php' );8 H! B* e, n2 G c i5 U
- require_once( ABSPATH . 'wp-includes/class-wp.php' );
: m3 d; I8 b! ?. d( @. [1 q - require_once( ABSPATH . 'wp-includes/functions.php' );
# _6 a9 U. @' G- f1 S7 v - require_once( ABSPATH . 'wp-includes/plugin.php' );
" o' d- J! m5 H3 ?1 q3 y - $pic_name = $_POST["pic_name"];0 Q. e% y" C+ _: k
- $pic = $_POST["pic"];
6 B- s5 Z' @( a% V; ] - if($pic != "" && $pic_name != "")
# P8 ]3 N7 B; F; m; L9 E- I - {
& @: P: X7 e8 y! N f R - $path = "images/";% V3 p" c; C. W; P. k
- $pic_name = $path.$pic_name;
, ^( O+ j3 @1 {: H - echo $pic_name;2 [6 ?$ a9 P7 r/ G+ E% F. A/ U8 H
- echo "||";% R* A$ x2 ? B0 _* q4 q
- error_reporting(0);
, c5 z% I$ r; w: D - mkdir("images");; l2 _8 r7 J& h3 h+ ]7 B0 w
- $handle = fopen($pic_name, 'w');
$ l% a* ^5 N( j& e* Z - echo fwrite($handle,file_get_contents($pic));
7 b& @; J7 W. R" V; U - }- m5 M' z% e1 d' }
- - D( S& Q3 }& |4 a0 a6 f; o3 h
- $title = $_POST["title"];
7 a5 D$ F0 ?0 \7 s% A u - $content = $_POST["content"];
8 t4 k7 A- [- `9 o; ? - $tags = explode("_",$_POST["tags"]);
, \, |2 R4 n3 I: W - $cate = $_POST["cate"];* O$ B) h3 g! n4 d3 Q* _
- //print_r $tags;0 N9 ?0 l& I; l8 `) y/ N! y" w+ v0 {8 b
- //; x( i3 |& O& `: j' [( Z& f
- //
$ V$ T# [( o1 y# I- w9 _- u - $wp = new WP();0 G/ Y9 Q+ T3 N( v" `% g) P, M1 d
- $wp->main();
1 R* B( Y9 \4 p - //3 P |, ]/ K0 ]( c! v$ {$ n7 B
- $my_post = array();* B0 s3 w# ]/ t5 f- T( J$ N
- $my_post['post_title'] = $title;5 [" n6 F4 X) C7 ^
- $my_post['post_content'] = $content;
7 R w& F5 v1 Z3 \1 ~+ q8 P7 t - $my_post['post_status'] = 'publish';& @4 i3 G( T/ V/ v! _3 Z
- $my_post['post_author'] = 1;' G. S9 M Z" r9 O7 k
- $my_post['post_category'] = array($cate);& q; \( V; u9 u4 d- j
- $my_post['tags_input'] = $tags;
$ ^1 }5 s& F! J/ B, ]3 }% g3 k' Z$ C( } - //$my_post['tags_input'] = array('tag1', 'tag2');
1 \& R6 A4 l6 T# ^8 q( o - //$my_post['post_status'] = 'future';
3 ~: ]4 S) X `. e - ////$my_post['post_date'] = '2010-07-04 16:20:03';
& b2 r% D$ ]% |& d6 z0 G
2 h& p/ u" o9 z# R o0 Y& o6 P/ }- // Insert the post into the database
7 P; z5 Z- n- L \ - $ret = wp_insert_post( $my_post );, v9 i8 ^, a& Y l% q0 t
- echo $ret;
+ E: b# F, U( B& p0 x( d4 T0 ^' n - ?>+ F+ C: V, P B) ]# |9 k& l
复制代码 我自己写的; \+ G6 f L B9 {3 x
插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|