|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑 3 _6 j3 ~8 I5 s% e! E8 Z4 _
7 r& P" ^/ B, r1 M. y- n
直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持
% ^- ^- Y! g' [5 ^9 H, ?* B. o1 }0 V+ O; z5 {0 C
我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了
7 t' C% K% G" Z& E; c, N) A( A) S( C- }' V/ e
方便实用 简单 何乐而不用呢- . v, @0 ]* @ _! _# H
- <?php
9 y; }9 h% B6 M - 3 W' U& y! @1 c
- define( 'ABSPATH', dirname(__FILE__) . '/' );
6 v8 i' [: S9 D8 D( s3 w - require_once( ABSPATH . 'wp-config.php' );
3 `/ ^& N: Z l* j+ F1 R - require_once( ABSPATH . 'wp-settings.php' );
* l- j) l# K6 j' W3 c( b - require_once( ABSPATH . 'wp-includes/class-wp.php' );
' x2 I: r; B. r0 E* C3 I - require_once( ABSPATH . 'wp-includes/functions.php' );: y3 x8 D- E% ]& @+ e/ q3 y1 `% W- o
- require_once( ABSPATH . 'wp-includes/plugin.php' );, L/ q8 ?- ^0 n- e0 D& E# i
- $title = $_POST["title"];
p- B1 W$ o- R' ]0 S& O# ~ - $content = $_POST["content"];
$ f7 o- o! _" Z; R8 Z - $tags = explode("_",$_POST["tags"]);
, }' P. m8 f% V& q9 u% l Y H - $cate = $_POST["cate"];
) I8 S7 d; B3 J* M& R+ a - //print_r $tags;
1 g7 ^- f2 n* p$ f+ H( i* S - //" j/ E" n( W2 V$ N
- //$ i7 q# J4 L0 K) W5 Y7 j$ H
- $wp = new WP();' E. j# g+ t z( _4 Y, Q
- $wp->main();
- V W. {8 a- S) ^ - //7 n* O @0 H' i6 r
- $my_post = array();
% V d( }! a3 C! B2 L" r - $my_post['post_title'] = $title;
( b, l, }/ b9 ^' E" z+ U - $my_post['post_content'] = $content;
0 I& l3 k$ _7 [6 ~) ~ - $my_post['post_status'] = 'publish';
% D! K# D: J ` - $my_post['post_author'] = 1;
- p9 b; _# Z' U9 [ - $my_post['post_category'] = array($cate);* Z. w7 ^0 X1 ~6 B: G3 C3 z/ b8 z
- $my_post['tags_input'] = $tags;; M J- O& R3 X4 Q; @
- //$my_post['tags_input'] = array('tag1', 'tag2');
) e" q% l/ s$ i0 ]6 Z8 g - //$my_post['post_status'] = 'future';
7 @. Q; q1 V, B2 ?- E6 V - ////$my_post['post_date'] = '2010-07-04 16:20:03';0 j3 F& L$ ^" G% z$ c' |1 g, E
- 9 k) P+ U/ Q0 n: K1 @! ~
- // Insert the post into the database
! a+ _: ?, }$ F# b8 B - $ret = wp_insert_post( $my_post );
) u, i8 r/ d# S4 k& z3 ` - echo $ret;
0 I7 g3 Z X6 B. J - ?>& N2 l; e/ a# \5 z$ i
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气
: u0 V/ L, V$ X; |7 a- <?php% a% X9 ~5 E$ [; E7 J. P* [
- define( 'ABSPATH', dirname(__FILE__) . '/' );$ n+ C }) M! Y7 F+ a
- require_once( ABSPATH . 'wp-config.php' );
2 J2 h0 t9 J, J9 k8 m - require_once( ABSPATH . 'wp-settings.php' );
% v9 x b' \; B - require_once( ABSPATH . 'wp-includes/class-wp.php' );
+ }: |! V( W* ~& O. x0 K - require_once( ABSPATH . 'wp-includes/functions.php' );
+ o4 X' v" F, [! f - require_once( ABSPATH . 'wp-includes/plugin.php' );
4 X; `& c. L* Y& B0 | - $pic_name = $_POST["pic_name"];3 M1 ~' u- f2 ]% i
- $pic = $_POST["pic"];* r$ J% w, B9 z, ^; ^: w
- if($pic != "" && $pic_name != "")( V, d7 P: I2 H
- {
. ~2 P% b, m' {5 ?" I2 S - $path = "images/";
5 ^3 Z- A) R& ^; R0 s! X9 | - $pic_name = $path.$pic_name; e5 W$ p4 E4 z$ I8 d3 t
- echo $pic_name;
0 K4 Q+ o6 Q$ X* k3 N( I/ @ - echo "||";0 {( Y ^ x) Z) h1 H
- error_reporting(0);6 \- {3 n7 |8 O, S* A% u( s
- mkdir("images");. ~; a8 N z9 K" D- U+ L3 B
- $handle = fopen($pic_name, 'w');8 a4 l! }7 a& |" k- y e
- echo fwrite($handle,file_get_contents($pic));
8 s% j L" r9 G2 x2 V( Q7 [ - }4 M( p1 G' y {" c" V
- * B/ ]+ d9 _( S) q( P. g
- $title = $_POST["title"];& X! H+ L7 S3 N, }* B4 ?
- $content = $_POST["content"];
# ~- o; ^7 v. w0 R - $tags = explode("_",$_POST["tags"]);6 s7 ]$ z+ M$ W4 u1 ]% z
- $cate = $_POST["cate"];
% { @6 _- h- _% O$ X# b5 e- n - //print_r $tags;
9 m- r) T. @" j; P/ T" {% K - //
5 d: ?2 _# U" T/ Z - //
* b3 Z( l+ Y2 f& q6 {! d. ~8 B0 M6 w. r - $wp = new WP();3 Y8 B' P, t& D. g2 U4 J
- $wp->main();
7 _& r' L" z, {2 [$ B. k& S# K - //2 e( I1 F5 m( f) M: r/ l5 x, L0 E( E2 C
- $my_post = array();3 |" E' K' _% P7 E7 Q, _( g0 [
- $my_post['post_title'] = $title;, G* L" e+ D1 }& v( U6 ?
- $my_post['post_content'] = $content;* K$ g2 p1 \. y# _1 ^- k
- $my_post['post_status'] = 'publish';7 p; N( |6 w, A: p% G8 Z
- $my_post['post_author'] = 1;7 F3 c1 K6 L7 ]- \/ R
- $my_post['post_category'] = array($cate);
4 L5 }% U" e( t0 L+ U: L9 a - $my_post['tags_input'] = $tags;% F. p: _* B. h/ B) n3 F
- //$my_post['tags_input'] = array('tag1', 'tag2');8 U% `- ~0 U u! C
- //$my_post['post_status'] = 'future';
6 u$ l9 Z2 g. R: k& K4 _ - ////$my_post['post_date'] = '2010-07-04 16:20:03';
$ m) j4 y0 m$ K) I2 F: ~
7 N( M) ]+ D J* A [/ L- // Insert the post into the database7 A1 V# i0 H1 D' c) J" |" [) k
- $ret = wp_insert_post( $my_post );
O- p' S7 V2 s) d; v - echo $ret;6 d2 C* c# b% z' O+ s
- ?>
% ]3 `* \- X( O& a9 I& C2 Z9 M9 z
复制代码 我自己写的
) i# ^ y0 F4 M3 L/ X: h插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|