|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑
5 ~7 o$ M: d3 C5 U( {. a! K
/ S; v8 M" R8 f1 ~2 E5 n/ O% F直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持2 a. g1 D: \# |- e
q3 K, d9 c% ^4 C+ Q. M我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了$ O/ c' g& s8 D" }; ^2 C9 w L
; J' F" d. ?8 e( @7 x( p; c
方便实用 简单 何乐而不用呢- $ f0 `* a& Z. w3 o
- <?php8 }# l3 M5 X$ z. @7 T2 _" X' l
- + Y9 s2 S& g# ^1 q t+ `
- define( 'ABSPATH', dirname(__FILE__) . '/' );+ N" m" d3 @% j
- require_once( ABSPATH . 'wp-config.php' );
) K$ Y' Y, ]+ @' s% f - require_once( ABSPATH . 'wp-settings.php' );
, ~7 n, @+ |/ `) |- Q( N- X. Y - require_once( ABSPATH . 'wp-includes/class-wp.php' );* R( Q9 t" _+ f- g. h' R% r q8 j9 r
- require_once( ABSPATH . 'wp-includes/functions.php' );2 R0 M% p5 w, V( e7 Y. p
- require_once( ABSPATH . 'wp-includes/plugin.php' );
- @& T, ^1 [$ z2 | - $title = $_POST["title"];( @$ U* L7 r8 l5 p
- $content = $_POST["content"];
# W5 }+ I* M0 J3 C - $tags = explode("_",$_POST["tags"]);3 [ E0 k) g* `" R, L
- $cate = $_POST["cate"];+ M6 `9 o! s1 M
- //print_r $tags;/ }# ?* M& ^! K$ @# k
- //
! x: |. i; @5 j& }. ?5 Z - //, b+ A, W6 H, F" @2 c
- $wp = new WP();
* [/ L. Q$ U" a - $wp->main();
. V1 y/ U3 A3 n( N: f7 N- w - //7 C' d4 }, m8 j$ F" e
- $my_post = array();, G" n" `0 \; \% ]8 Y
- $my_post['post_title'] = $title;+ @' b% {! b- d' f; ^
- $my_post['post_content'] = $content;5 n k8 l& X3 \6 ?3 T3 h9 _3 S
- $my_post['post_status'] = 'publish';& s3 v! r0 Z7 x
- $my_post['post_author'] = 1;4 P9 v- e) R5 {) s
- $my_post['post_category'] = array($cate);
( D C( `4 P2 o5 R7 j4 S" { - $my_post['tags_input'] = $tags;. K* N' W% p8 r1 J
- //$my_post['tags_input'] = array('tag1', 'tag2');
& o' z4 ~* L- i: K - //$my_post['post_status'] = 'future';* a% I6 e0 }+ s5 I) J0 u# J& V
- ////$my_post['post_date'] = '2010-07-04 16:20:03';# W& e6 o/ ~8 w, M5 r
3 y) o- Y! a) A% Q3 M- // Insert the post into the database. O6 i- X2 K7 s8 t. H
- $ret = wp_insert_post( $my_post );
9 q) f2 g* G7 X$ x3 q$ j) Z5 z3 @8 d - echo $ret;
, N0 r# t" y3 C, R2 v) ^ - ?>
) o6 Z# O# u5 |" d0 l
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气- 8 Y/ t* k5 B( c! k& F& d. n2 G; `
- <?php! [; u; g7 f+ v; R* }, L
- define( 'ABSPATH', dirname(__FILE__) . '/' );+ |6 i* h6 H6 L4 k8 b' M
- require_once( ABSPATH . 'wp-config.php' );8 I, X8 J V) R+ a3 }# C
- require_once( ABSPATH . 'wp-settings.php' );+ t* p3 {) {# M1 t) s$ _
- require_once( ABSPATH . 'wp-includes/class-wp.php' );0 |3 z# O+ h7 W5 q; Q
- require_once( ABSPATH . 'wp-includes/functions.php' );( f2 c2 r( e1 }! e2 i
- require_once( ABSPATH . 'wp-includes/plugin.php' );
* U0 ]; Y) `% a+ F2 x - $pic_name = $_POST["pic_name"];
' y7 e' l3 t: I% U) Y# w+ I - $pic = $_POST["pic"];
% |5 r/ U9 \. Z" F: G# t" L* R - if($pic != "" && $pic_name != "")! v0 v) [, q& ~% K; H- `
- {
# Q( h0 u" N4 s/ j7 [3 z - $path = "images/";7 u N- j$ C* T9 K& {; p& m
- $pic_name = $path.$pic_name;
# D& @8 \3 E( x1 \ - echo $pic_name;
- u: Z9 H8 b8 v8 Q# I2 r- T - echo "||";3 I P+ E9 u& X" o, M
- error_reporting(0);
|) _) T ]( a( B% S( h; Y1 s - mkdir("images");/ p0 O1 f. l6 p$ n' v2 o
- $handle = fopen($pic_name, 'w');
9 f& T6 L3 E/ d1 k0 X: W - echo fwrite($handle,file_get_contents($pic));' a ?8 f/ Y, J8 m
- }2 }8 R: U6 d8 p+ t
- * B t5 u6 [5 [; d
- $title = $_POST["title"];1 n; [8 P$ ~) i( q
- $content = $_POST["content"];
* Q2 ]2 D* G' f - $tags = explode("_",$_POST["tags"]);
- U& M) Z' V( ` - $cate = $_POST["cate"];7 x8 L" Q7 e6 q! Y5 C
- //print_r $tags;- T+ i- F" i" o4 R8 L
- //
4 r# `8 H3 R7 U$ P - //
4 N) R# Y! Z% R# T) O0 | - $wp = new WP();0 v7 O) A5 q. c9 i# D- w
- $wp->main();
0 K' C+ M! C' z) V+ G - //
, j8 k, ]5 R$ W0 s - $my_post = array();& r, H ^; z( d: z2 b- C
- $my_post['post_title'] = $title;
5 u7 i& z P& @ - $my_post['post_content'] = $content; ?+ U) { @3 D& g" g
- $my_post['post_status'] = 'publish';
9 ^" ~0 T. n$ {9 Y" O* y - $my_post['post_author'] = 1;9 D& Y# e2 T+ H& D
- $my_post['post_category'] = array($cate);
- Y/ J, y7 I+ z9 L0 g( Z' d/ ^ - $my_post['tags_input'] = $tags;
( F1 _9 A: Z8 T+ w# X/ W5 i, T y - //$my_post['tags_input'] = array('tag1', 'tag2');
' K' }. B8 M9 ^- b6 p% r - //$my_post['post_status'] = 'future';/ E0 @* v: N( M2 M Y0 t9 a5 C
- ////$my_post['post_date'] = '2010-07-04 16:20:03';. F' t7 j8 O8 P: F/ s& t9 v
, b+ a% |7 y3 s3 n6 O! v, t! `3 A- // Insert the post into the database
# [! f9 X. ` x5 l - $ret = wp_insert_post( $my_post );
% h3 z& W1 `. v - echo $ret;
6 A8 ~7 N; v4 u: \3 o! ~ - ?>
( ^% T% r' g& y3 e5 X
复制代码 我自己写的
( Q) ]0 o* p. ?9 d& }: L插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|