|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑 , h4 [. l4 I: [5 H+ W9 A
' o" K$ \6 t5 p) x4 M1 k% ]
直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持. L [; x( Z8 A# {2 l
# Q$ W' h5 f) l+ S2 C我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了5 G* r* E, _; f4 R8 P
, ~9 Z" ~! l% ~方便实用 简单 何乐而不用呢
: l7 Q3 n( o1 |# m1 u; v' g. @- <?php6 x/ |2 R, i# J# J9 r
- 7 H2 O3 j" Q2 A" [) o2 ]: c- y
- define( 'ABSPATH', dirname(__FILE__) . '/' );0 R1 N6 x8 Q3 U& Q r4 _
- require_once( ABSPATH . 'wp-config.php' );
0 x$ M& b& q% t- }, b1 Z - require_once( ABSPATH . 'wp-settings.php' );
' _2 e* w9 u( j- V/ f6 a- v$ J* X$ J - require_once( ABSPATH . 'wp-includes/class-wp.php' );
4 P d& o# _+ `, `. J* M - require_once( ABSPATH . 'wp-includes/functions.php' );
E" e+ }. W) P - require_once( ABSPATH . 'wp-includes/plugin.php' );
/ ~& ?5 O* Z( M0 K5 Y, y+ b - $title = $_POST["title"];
& _# S! j R: h! n* j; l% Q9 v - $content = $_POST["content"];
6 u8 f" _( E/ ] - $tags = explode("_",$_POST["tags"]);
/ n1 Y% A5 [- C, g8 _8 _ - $cate = $_POST["cate"];, ^+ f$ w) w9 [' o2 p: J
- //print_r $tags;7 \* \* x" L0 u5 Q5 i w7 e
- //
& b/ Z2 F. O$ \7 { - /// a( a8 |* g- K( A+ o0 B0 z( @ i4 T* R
- $wp = new WP();* d6 u! x' d2 d. o' f
- $wp->main();& V) W8 _* v+ g' y) g! ^1 L' o7 Z* ^
- //
* c0 O+ H2 _ M( I9 x! D$ F - $my_post = array();
" W" ]' q* z/ n& j* g" T - $my_post['post_title'] = $title;
/ i X% d) R* P4 E: y7 P6 W - $my_post['post_content'] = $content;
/ a# h7 U0 [! h0 n3 e& r - $my_post['post_status'] = 'publish';' m- @ u2 h3 ?' T
- $my_post['post_author'] = 1;
1 e4 \* a8 c2 k' f3 m! o1 x0 y5 A$ d5 } - $my_post['post_category'] = array($cate);
' M( Y8 Z& N8 _* y8 I: [) r - $my_post['tags_input'] = $tags;1 z1 f! c' c+ }4 e3 @1 p
- //$my_post['tags_input'] = array('tag1', 'tag2');
$ T, \, N G5 J4 A* J: _ - //$my_post['post_status'] = 'future';# }) B1 J+ x+ ^+ ]2 E* Q
- ////$my_post['post_date'] = '2010-07-04 16:20:03';3 M# E% ?2 A7 z. ] z* k
- , t1 J& U, ?7 W
- // Insert the post into the database
+ V1 L! d2 h z6 P8 \ - $ret = wp_insert_post( $my_post );2 `+ N- ]! B; b
- echo $ret;
( _# z7 w, T( }- N9 z" r - ?>* B! Z7 ?4 d2 [! \
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气
/ f: k4 {1 R- d7 R; ]3 C, r* w5 A- <?php
( D/ i {- i8 s/ {! b! Y - define( 'ABSPATH', dirname(__FILE__) . '/' );
( c6 J3 {4 ?- B7 t! { - require_once( ABSPATH . 'wp-config.php' );
+ G. Z0 m3 N0 A: x - require_once( ABSPATH . 'wp-settings.php' );( ?0 F/ M9 }* c8 S
- require_once( ABSPATH . 'wp-includes/class-wp.php' );
: @$ d" ~8 e6 o/ ~. G7 J - require_once( ABSPATH . 'wp-includes/functions.php' );
& D7 e9 O. j( K- h - require_once( ABSPATH . 'wp-includes/plugin.php' );
0 K1 N# E+ Y! @( b v - $pic_name = $_POST["pic_name"];
8 B4 K1 Q- x, I7 U( Q- E - $pic = $_POST["pic"];
' N* V4 c+ S/ e6 y( [ - if($pic != "" && $pic_name != "")
2 m- \ W' w4 L6 r$ V - {1 `& @- Z3 t! R6 N
- $path = "images/";
: B6 o, D) P- S - $pic_name = $path.$pic_name;
3 n2 w" B/ s* k - echo $pic_name;( M; d$ I3 d. V: M( \ ]
- echo "||";
" \0 F$ S4 y. [7 m8 L$ `2 L6 j - error_reporting(0);+ V; {# v) ?% ?" V- {: z1 D
- mkdir("images");% \) ^0 z/ _2 S9 H3 j
- $handle = fopen($pic_name, 'w');" ~' l# V; e; d+ m; h$ I
- echo fwrite($handle,file_get_contents($pic));$ y8 y I- y5 w4 s% _
- }
5 \# ?- J! \& q+ K6 B. `8 G1 N5 W2 s9 W - 3 D$ D% h- E S% y& \
- $title = $_POST["title"];
' D9 V" ^/ h5 g' H; n) f - $content = $_POST["content"];1 P( A4 a5 O2 e# x% q- \
- $tags = explode("_",$_POST["tags"]);5 @! ~% A: V9 y! ]* z$ C
- $cate = $_POST["cate"];
$ Y s$ W4 ]3 k" f+ t1 T6 y5 p - //print_r $tags;
6 X& r% a5 w; e' S. I% P( y6 u+ M - //
, ?9 X3 t4 K0 s7 s' V - //
; ^& p& x$ w3 _5 w3 m/ i R0 y" C3 x( m - $wp = new WP();' n+ L, f$ r; d5 `- x7 B! O
- $wp->main();& p, h# t+ M5 C& \$ X2 v" c# o
- //
6 m6 f8 S& Z {) h( c0 V - $my_post = array();
8 z( _# t: z' m* J - $my_post['post_title'] = $title;/ V. g3 d& \* }! `0 w' U
- $my_post['post_content'] = $content;- V# v4 d2 {: a8 r0 W h/ R* M; \6 d
- $my_post['post_status'] = 'publish';
4 ~# [" S1 ?! s# V) O& v( T9 l - $my_post['post_author'] = 1;
6 D! _: y, W1 G3 Y5 a0 w7 B+ z: ~ - $my_post['post_category'] = array($cate);% n. U# p' H7 n
- $my_post['tags_input'] = $tags;
3 d9 G/ O' I* s6 ], I: ]( x - //$my_post['tags_input'] = array('tag1', 'tag2');
; Y% W3 [8 _4 Q4 Y, h- ~! N) I - //$my_post['post_status'] = 'future';
+ L/ i2 D) _/ C$ U - ////$my_post['post_date'] = '2010-07-04 16:20:03';0 S* w+ u/ V/ k7 l+ @- V
- 3 @2 Z; S" D' g$ [4 Y6 N
- // Insert the post into the database: H$ z8 |) [, F) m
- $ret = wp_insert_post( $my_post );7 R& I& k/ u% a1 d9 K
- echo $ret;* D* k6 o& R8 K! T) G; v, R
- ?>
6 v: J; h4 n4 U' m0 f- A! J3 \; F0 y7 C# E
复制代码 我自己写的
+ z3 W2 N' Q1 P插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|