|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑 : r5 f \7 e7 h% z+ X
% v9 l$ x. E Z6 S3 e5 J直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持
6 @7 ]0 {! S. b+ n
) l% |$ m. M' ]我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了
6 w) x( U( W, G& P0 G! [* a- Q" t% d: u
方便实用 简单 何乐而不用呢
# ^( j/ o( Y. C- <?php
6 H0 j0 }4 }" i! U% c# r/ x, ^ - ! s' o, u" I$ K$ h2 j+ @; n
- define( 'ABSPATH', dirname(__FILE__) . '/' );
/ d7 ~1 f0 v/ W& g @ - require_once( ABSPATH . 'wp-config.php' );
! K2 @# x4 [& Y( h( y - require_once( ABSPATH . 'wp-settings.php' );# H4 [, d2 N. s6 K" y3 T
- require_once( ABSPATH . 'wp-includes/class-wp.php' );7 N1 T" T% `/ f1 T. \9 F/ u
- require_once( ABSPATH . 'wp-includes/functions.php' );
$ w0 F! E# {) D; {/ f - require_once( ABSPATH . 'wp-includes/plugin.php' );3 q( c9 n) Z& T! T4 T
- $title = $_POST["title"];0 V0 u5 w4 f6 H9 k. `% U; v
- $content = $_POST["content"];) H7 v9 d, b: E9 G9 V) g- [
- $tags = explode("_",$_POST["tags"]);
' h$ S+ m$ i3 W' w* k* m/ g - $cate = $_POST["cate"];
- l& n' Y I0 p/ _7 S. a - //print_r $tags;9 o9 y' V5 W6 p$ f2 @. E. C! H
- //
, @$ i$ J0 \0 k$ w - //4 N6 V7 M6 m, k2 P( ?+ Y
- $wp = new WP();
! W2 X7 y( M- g5 c( ^ - $wp->main();
( X/ A8 i8 t. Z+ q. N - //
* J* e' R, X& R1 g- e( D - $my_post = array();1 b$ m1 K. p' H
- $my_post['post_title'] = $title;
! q9 f0 X$ i0 [8 t1 J4 | - $my_post['post_content'] = $content;& e/ r/ |+ _ C% k
- $my_post['post_status'] = 'publish';$ F3 Y$ [& y( x+ s+ }5 X( \
- $my_post['post_author'] = 1;0 ]) ]% x# H" O8 a
- $my_post['post_category'] = array($cate);
- a7 y3 k) C1 ?% O - $my_post['tags_input'] = $tags;. Q+ w+ L! d8 E
- //$my_post['tags_input'] = array('tag1', 'tag2');
, v6 O% B' G& t' G: ?1 V- d - //$my_post['post_status'] = 'future';. O5 g- T$ r! m% h4 u( t
- ////$my_post['post_date'] = '2010-07-04 16:20:03';. e0 {" z+ K6 O8 [) j( {
7 @$ N/ |( I5 T% s- // Insert the post into the database( N8 {2 h8 g$ g, x
- $ret = wp_insert_post( $my_post );, s8 Q# s8 n3 q. r1 I. {6 y
- echo $ret;
) @0 Y) e$ d4 s* w" T - ?>
1 u- z* C o0 C$ `
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气- ! V$ b5 g! i) z8 v/ E, w1 R
- <?php
( M5 R8 h w: r - define( 'ABSPATH', dirname(__FILE__) . '/' );' ^7 x3 R* |! f5 F9 F
- require_once( ABSPATH . 'wp-config.php' );4 t9 `) Z& ^5 _
- require_once( ABSPATH . 'wp-settings.php' );
& O4 s* V8 O- O9 V* z! X - require_once( ABSPATH . 'wp-includes/class-wp.php' );" U5 W% k; P8 u* k9 P3 `0 C
- require_once( ABSPATH . 'wp-includes/functions.php' );9 m( E5 r/ Q/ O* q W3 H
- require_once( ABSPATH . 'wp-includes/plugin.php' );
7 B- U' G: O4 C, a. c0 W - $pic_name = $_POST["pic_name"];8 M3 y7 s9 f9 I" t5 W
- $pic = $_POST["pic"];- C: h5 t5 u' t# E7 i2 e
- if($pic != "" && $pic_name != "")+ @" o. [0 O$ I* v
- {
- c' g j- J8 @( W$ M' v3 L - $path = "images/";
8 }. M2 B4 D$ e+ g6 A - $pic_name = $path.$pic_name;
8 `+ k+ {( X: b - echo $pic_name;7 x: |8 q% |3 X( M% l, ?( X4 v& j/ x
- echo "||";2 C; k3 }0 y) u+ l# |; S
- error_reporting(0);
) `' o8 K' e( @0 e - mkdir("images");
) U, Y3 F w7 |5 s- v3 f4 y; x( _$ } - $handle = fopen($pic_name, 'w');1 |7 Z8 S z/ p! z+ N" {2 n G) a
- echo fwrite($handle,file_get_contents($pic));
% N* m9 Y& ~) W+ Z9 }& y$ W - }0 l; p1 m, j3 T8 A+ W* s5 n0 [
/ a& g: o' ?) `' J2 K- $title = $_POST["title"];
) ]# o7 t5 F g/ z3 H. b5 Y+ N - $content = $_POST["content"];
; e4 C0 h- s6 B4 _1 q( [, j! {& j - $tags = explode("_",$_POST["tags"]);
2 A: a( c* B) O' h" `! x+ [+ G - $cate = $_POST["cate"];
+ \: M( G) O; n8 a - //print_r $tags;3 I- f7 J0 a+ J. T
- //, \ U! x" ]: Y* S
- //: _2 S. v, \+ {3 N
- $wp = new WP();% r% r" Q- a# T( `; G) ^0 N
- $wp->main();( W! `7 E" { C# Q0 A, ]
- //3 O. f7 q. h6 X- T0 Y/ ]+ ^$ ~
- $my_post = array();
% f/ k5 ^; u! R: L7 w" b - $my_post['post_title'] = $title;& I1 @9 C1 r9 t8 V0 K2 M
- $my_post['post_content'] = $content;/ b2 G9 u* h1 k3 V8 A* k
- $my_post['post_status'] = 'publish';
; A% Y! H( n) e+ {7 a/ P - $my_post['post_author'] = 1;
6 b# q2 R% V; @) c- B - $my_post['post_category'] = array($cate);
2 R0 t, w3 h" L2 {9 E# C6 t& s - $my_post['tags_input'] = $tags;* E, R+ J5 j3 Y! s" \7 j7 {
- //$my_post['tags_input'] = array('tag1', 'tag2');! F8 a$ I, R2 G9 X. b- f
- //$my_post['post_status'] = 'future';4 S/ I6 X# f" e, Q
- ////$my_post['post_date'] = '2010-07-04 16:20:03';! d5 ?8 z# q/ E8 ~
- 6 d$ C6 ^9 G o7 A G& K
- // Insert the post into the database
( h' v1 C" a5 s7 D5 @ - $ret = wp_insert_post( $my_post );$ X7 @* p; V6 \
- echo $ret;
2 w; D% v. E9 b1 F - ?>2 P% z, h( ^, M/ ~' O5 I x; u& h' i
复制代码 我自己写的
3 g0 |2 B# A, C; [# a- Z' g插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|