|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑 . a" x( l4 c; M3 q* Q' L/ I6 p
' W/ y- I: |" E- i! a7 D& g8 S
直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持7 |3 z( G. K3 ^! Z; e, S
4 N( T* F; k, D5 j8 j; |; {$ D- T& C# o
我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了+ E6 s, E1 U" E e
5 e/ u* E# J5 f9 O# \+ u方便实用 简单 何乐而不用呢- ; u( }( h1 H3 S' {9 a' ?& d
- <?php
% Q7 ]8 ?0 f! `9 `$ D }0 U
% A( a ~2 I7 y6 ^% z- define( 'ABSPATH', dirname(__FILE__) . '/' );
8 S; k* y0 b; b) z: J - require_once( ABSPATH . 'wp-config.php' );
X; {7 [9 ^9 F m# t - require_once( ABSPATH . 'wp-settings.php' );" V+ R- B7 Z: G* s
- require_once( ABSPATH . 'wp-includes/class-wp.php' );$ {1 o5 b2 D2 P; @9 T3 t) {
- require_once( ABSPATH . 'wp-includes/functions.php' );
9 Z" \: X' F% D - require_once( ABSPATH . 'wp-includes/plugin.php' );
9 G, P; X* ~ ~* A& t+ M - $title = $_POST["title"];; _" M' J" d5 Y. K0 ?4 E: ^
- $content = $_POST["content"];
# H/ y$ w- V1 Y" G5 S/ s7 o - $tags = explode("_",$_POST["tags"]);9 U* S$ J" q7 `5 H+ n1 {( P9 ~
- $cate = $_POST["cate"];
, | w6 G E4 T$ t0 } - //print_r $tags;, Q; w$ V" \; g/ j* @9 w. q* v( A
- //, c2 ~7 Z- w/ D# U
- //
1 {: }# ?& g1 Z9 U - $wp = new WP();
: \2 t) x6 y2 i* H8 c( f+ V8 x - $wp->main();
+ q( X2 q+ s K7 u9 J* r+ X - //
6 E, `$ U0 G/ j9 l# _ - $my_post = array();
. b: v/ X4 {9 @ - $my_post['post_title'] = $title;
, K$ P/ I& _# ] - $my_post['post_content'] = $content; v9 c9 \, e8 {8 ~/ U$ L, t
- $my_post['post_status'] = 'publish';
1 \1 `. D4 u( N c6 R) g6 B - $my_post['post_author'] = 1;8 i5 [# h" {0 k) @6 A% X* |( H; R
- $my_post['post_category'] = array($cate);' G% R5 B7 u' k6 J- Z
- $my_post['tags_input'] = $tags;
" e! G/ x' h2 n; n8 n# _+ l - //$my_post['tags_input'] = array('tag1', 'tag2');7 a& [8 @0 ^3 \3 u+ v
- //$my_post['post_status'] = 'future';
8 b9 B( Q$ g0 ]7 ?! T! [8 } - ////$my_post['post_date'] = '2010-07-04 16:20:03';: V* q7 P: b+ d- c
- 7 s3 a: d8 [9 c$ k
- // Insert the post into the database, s7 U5 u/ l" I! J
- $ret = wp_insert_post( $my_post );
! `; _& Q9 y9 S7 R - echo $ret;
( Z' z4 b L' E( j2 \. f0 C - ?>
9 |0 H2 S1 x' g6 w9 v9 f! `; I
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气- - P, I: L2 T! C3 }0 M
- <?php
! S2 x0 n/ o: [9 T+ A5 } - define( 'ABSPATH', dirname(__FILE__) . '/' );
* h# M: w, W3 @5 d - require_once( ABSPATH . 'wp-config.php' );
- ]* m9 g M8 P6 Z8 u - require_once( ABSPATH . 'wp-settings.php' );7 A7 m3 o9 i5 L/ Q
- require_once( ABSPATH . 'wp-includes/class-wp.php' );" n5 P1 W( ~* G2 ?0 X" N. X
- require_once( ABSPATH . 'wp-includes/functions.php' );7 ^+ t5 {1 m; P9 a5 t
- require_once( ABSPATH . 'wp-includes/plugin.php' );1 ^" K) h5 W1 |! _! `
- $pic_name = $_POST["pic_name"];
/ k2 z' D$ B- p8 W - $pic = $_POST["pic"];; M, ~% {! V: W% o! i9 ^
- if($pic != "" && $pic_name != "")
2 n- }6 \: e! P7 y - {
" x% L1 u) g5 O* a - $path = "images/";! H2 Q5 d: l! {
- $pic_name = $path.$pic_name;
- M% V+ a3 V8 P - echo $pic_name;
D4 _7 |: ]+ t, ^, h9 \ - echo "||";" F( u3 S0 u& B, Z, k
- error_reporting(0);" T, b7 p4 c" \4 ~" h- p, H, c
- mkdir("images");' b: P2 e) D6 f; @
- $handle = fopen($pic_name, 'w');
; G# C! |$ I" s! q: Q - echo fwrite($handle,file_get_contents($pic));2 r& P* F8 q3 P% m+ c, K/ j; H9 F
- }
9 w4 K/ X2 `6 W4 M2 o8 [: O( W) [
" A* J$ i, z7 r" O: P4 N3 m* m- $title = $_POST["title"];. z& C: r3 z0 ]+ _0 I7 {
- $content = $_POST["content"];( R; }: V' D" h8 o3 Q
- $tags = explode("_",$_POST["tags"]);
$ t4 J; N* k0 J1 H3 e - $cate = $_POST["cate"];' T7 Q0 h6 @1 U8 l
- //print_r $tags;) O M1 u: E: G) l) V' K/ r# v
- //& _1 t5 L. X& j" K
- //. V) g, }; x' A& i1 I
- $wp = new WP();7 }* L: g8 I+ I
- $wp->main();* V. e& i3 n6 n9 y W9 ^& x
- //
+ x1 q( Z0 R0 { - $my_post = array();
! z# G _8 Z, |4 |2 y. g( M& i2 y - $my_post['post_title'] = $title;
! j' P. X) p* L8 E - $my_post['post_content'] = $content;
6 n2 S4 ^4 z) @* {: j2 E5 x% y) u - $my_post['post_status'] = 'publish';
, a) u! g3 [) m+ F. }; N8 K - $my_post['post_author'] = 1;1 f# n! V4 x3 x, `
- $my_post['post_category'] = array($cate);
{- A) X6 b( r& _ - $my_post['tags_input'] = $tags;
" o8 z% {, g& Y; V# o9 l' p - //$my_post['tags_input'] = array('tag1', 'tag2');7 X* N3 K* |" }+ i
- //$my_post['post_status'] = 'future';4 e8 F. @3 }% Y) m
- ////$my_post['post_date'] = '2010-07-04 16:20:03';
S1 h" n+ N0 r9 \! L' x
) l+ B* X ]% a! H& K- // Insert the post into the database
; K4 s" m, ^, x3 b1 G7 u6 W; P - $ret = wp_insert_post( $my_post );2 L) P, h9 |9 K* x/ R9 K/ l
- echo $ret;
/ @+ F7 {$ Q' U9 K& O - ?>5 x: `, z5 Y. Y' k$ r( k8 k
复制代码 我自己写的
0 F& n/ G) Z! m* l+ k9 v4 w插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|