|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑 1 V* @& M! \. G9 c) ^" h3 d& S& x
f# d" _& b2 F* M: W直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持! L9 `1 k5 T8 k5 t2 w
; m4 R T: `- I7 q5 q我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了
' E# m- C/ p" M, T1 R" U" D9 z& M2 H3 e/ x0 x0 R5 w) p$ e
方便实用 简单 何乐而不用呢
" O1 J* v( Y' y: y- <?php+ g N- v6 Q$ z3 {4 }6 u. v
9 Y# K/ Z8 a: C: ~2 \ A1 i) s- d- define( 'ABSPATH', dirname(__FILE__) . '/' );9 L. b# ]+ N7 p1 z4 T: @& k
- require_once( ABSPATH . 'wp-config.php' );" g$ V0 V8 x4 N( b
- require_once( ABSPATH . 'wp-settings.php' );. w/ i$ H3 ~5 D4 \7 P7 P; X
- require_once( ABSPATH . 'wp-includes/class-wp.php' );
4 l2 c& x" T* u7 @ - require_once( ABSPATH . 'wp-includes/functions.php' );8 D: [$ z. g& v* g0 g$ s! N9 O
- require_once( ABSPATH . 'wp-includes/plugin.php' );
: @* ~; o; g7 H' x, t4 @. o - $title = $_POST["title"];( X# [2 K8 D2 Z# p% } @
- $content = $_POST["content"];7 F+ H: c" W6 U* X/ n( q' D8 w
- $tags = explode("_",$_POST["tags"]);- E: X% j8 ? j y: R3 d) B
- $cate = $_POST["cate"];9 Q( w3 `- W: x3 J: V, f) i: R
- //print_r $tags;
5 C7 i1 j- m( K, h$ | - //- l7 z6 t, F' n, x, F# M6 y
- //7 v1 v9 S9 y. c6 U) H
- $wp = new WP();
3 D- v8 k5 y6 U c% S8 j3 F* Q - $wp->main();2 N0 [: Y& ~5 v9 B9 w$ g* Z
- // z' F+ i: F9 W% |, F% V
- $my_post = array();
3 W5 m: P; n$ d3 O1 ^0 K - $my_post['post_title'] = $title;; q7 y/ Q) W3 i* t
- $my_post['post_content'] = $content;- X' x3 w- L& }
- $my_post['post_status'] = 'publish';' `3 l {% m1 ~7 t3 j
- $my_post['post_author'] = 1;# A8 F5 K! s7 U' j
- $my_post['post_category'] = array($cate);( t9 H' a# m a- p7 B( }" g
- $my_post['tags_input'] = $tags;
/ ~0 f" P8 H* n' P B5 S& K - //$my_post['tags_input'] = array('tag1', 'tag2');
* W# f# ~: W8 x* b - //$my_post['post_status'] = 'future';
" t1 D) _! [# _: G9 b - ////$my_post['post_date'] = '2010-07-04 16:20:03';
! j( l0 y4 j' |+ u8 R7 o' D% [ - # X) {3 p; {7 ^8 o
- // Insert the post into the database/ }" I6 W' T' ^) C$ `! M P7 {
- $ret = wp_insert_post( $my_post );! K0 f- G5 t5 z6 }- l& e
- echo $ret;
7 L2 O7 p9 s6 D2 E - ?>2 H+ y4 g2 t# w$ X$ a2 p
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气
( ^; |, }: |" p/ ]( p7 G- <?php
* z: T( r) j h% w* @6 s5 m6 C - define( 'ABSPATH', dirname(__FILE__) . '/' );8 v" I) V7 i K6 }2 A4 G% i1 O
- require_once( ABSPATH . 'wp-config.php' );
0 ?. J4 t- w' R8 A/ v- F* q - require_once( ABSPATH . 'wp-settings.php' );9 |7 P4 L5 }7 O- ]% f# p6 C
- require_once( ABSPATH . 'wp-includes/class-wp.php' );
& p$ a4 x3 Q6 h+ I7 z7 O* u# g - require_once( ABSPATH . 'wp-includes/functions.php' );
) ?' b7 x& P/ J: A. V - require_once( ABSPATH . 'wp-includes/plugin.php' );
2 F7 e) I/ r) y) M" P) ? - $pic_name = $_POST["pic_name"];
) e/ T j# R. s' P9 A3 V - $pic = $_POST["pic"];
0 B4 ]$ f* d3 i: @( s- \( h; ]9 W. Z - if($pic != "" && $pic_name != "")
+ {3 h3 O3 H/ W6 E$ q6 D - {
8 e9 C* e/ h- h8 N. S) N( {2 i) G" ? - $path = "images/";. U( e' |) T1 g
- $pic_name = $path.$pic_name;1 U3 ]: ~- ~" c A( `' l/ X
- echo $pic_name;; j. U" h9 r- w% ~+ J2 t
- echo "||";" P, ~: ]; w3 Y3 W: P$ _
- error_reporting(0); S" ~5 G S2 y4 G& K' |
- mkdir("images");
: L+ E6 f4 Y/ J, c+ b0 O) c - $handle = fopen($pic_name, 'w');
( k( o) h$ d7 @1 k. b - echo fwrite($handle,file_get_contents($pic));5 y t/ g# I/ B, Z" W) n, Z( {
- }- E" g: O5 w" t( z3 n4 ]2 H6 L
/ m7 L6 V% X3 P6 x4 P* s/ r- $title = $_POST["title"];% e' `& Y+ T, p7 J3 A$ E
- $content = $_POST["content"];( V0 j0 N: g8 W- |2 ?
- $tags = explode("_",$_POST["tags"]);
g* S0 `; ?8 z/ q' k, {! M - $cate = $_POST["cate"];# Z/ X) q6 J' r/ Q2 {
- //print_r $tags;+ S7 t5 S" f3 I/ A, }, f
- //
8 H+ Y6 c4 T( U& g. E; S - //
+ s( Z( Z+ e0 P" p; w' B - $wp = new WP();
' U) @' ]( V. e% `& j3 d - $wp->main();
' F9 p1 z$ z; F5 u2 x" _) o X5 F - //
. i3 M1 L) u0 i) O" G - $my_post = array();4 L0 c: g4 I; V4 X
- $my_post['post_title'] = $title;4 V o4 ~4 t& W, z
- $my_post['post_content'] = $content;9 a0 B1 T4 B9 b, `% Z/ Z& V1 Z) m
- $my_post['post_status'] = 'publish';
3 _( v6 ]# T8 Q' ? - $my_post['post_author'] = 1;0 Q' U4 r( t b/ Z/ S
- $my_post['post_category'] = array($cate);" m7 k; f2 m+ V5 U( m
- $my_post['tags_input'] = $tags;" I# ]: {& ^. W" w p
- //$my_post['tags_input'] = array('tag1', 'tag2');
6 @6 s3 a. `- z) g0 ?% Q6 q/ U - //$my_post['post_status'] = 'future';
/ K0 ^4 U) z. _0 e- r/ E - ////$my_post['post_date'] = '2010-07-04 16:20:03';
. ~' A% D) j' O1 I9 B4 |: X
$ n; L# P" K0 M- // Insert the post into the database( x' l# C3 ^0 J% ]# q w( k! y: u$ w
- $ret = wp_insert_post( $my_post );
1 e% s, s' \& N - echo $ret;1 d% g+ W9 R5 e1 E8 x( a4 E
- ?>
I) f6 P/ \* p3 E' P& i
复制代码 我自己写的
9 G8 Q9 R) I6 H, l x插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|