|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑 . O$ C% _( @* e: D' p
- P0 G0 \ Z7 {) u0 o直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持! {! L6 t# X2 D8 r8 k" h
6 \$ i* t6 s6 X! n我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了
5 ~2 F& }6 A- ?* S" A0 K! S
' ^6 |- P" u# ~; X6 V( B( n, M方便实用 简单 何乐而不用呢
3 H9 A3 n3 P) i' P! C* V( k: W9 U- <?php
) e8 G" ~! H# J% }6 L* T, P
" b( S! G$ Q* `$ Y+ W2 j& r- define( 'ABSPATH', dirname(__FILE__) . '/' );3 E, S- h/ B( j. [) M
- require_once( ABSPATH . 'wp-config.php' );
- e# ~( K& n9 Z& I/ O* O - require_once( ABSPATH . 'wp-settings.php' );
: J8 Y5 ]; J7 g8 w; Z) @ - require_once( ABSPATH . 'wp-includes/class-wp.php' );
& u, [- j4 c$ t% \# { - require_once( ABSPATH . 'wp-includes/functions.php' );2 E% c! s4 k/ [; l' O; M
- require_once( ABSPATH . 'wp-includes/plugin.php' );+ X J! s* c3 p% T
- $title = $_POST["title"];
2 i0 ^; l% G9 x& ^: Z: w3 G - $content = $_POST["content"];
$ _- {, t* D9 H* O/ N$ Z4 B - $tags = explode("_",$_POST["tags"]);
6 E$ G8 a' O* S; ?9 i9 Z% ^ - $cate = $_POST["cate"];
G I, @8 i6 j - //print_r $tags;% z. C7 m2 ^0 r' u2 o8 |
- //
* V% ?8 \2 b0 e2 b - /// S @2 E1 Q3 g$ a! V
- $wp = new WP();
- x5 X' e e! ]5 N T2 H$ I7 z - $wp->main();/ y% ^6 V1 V, ?3 }9 W
- //8 P9 J, |' P) P! d
- $my_post = array();* |7 \0 }( {$ q; s+ W Q) r! {1 ^
- $my_post['post_title'] = $title;2 c+ `0 ]. d+ l* b, V2 D
- $my_post['post_content'] = $content;" k% U# @* ~2 q; n$ x0 P
- $my_post['post_status'] = 'publish';4 H! U d s' {! c! n! q3 D
- $my_post['post_author'] = 1;
, T0 A# h* K( `8 y! t4 ` - $my_post['post_category'] = array($cate);
3 V c9 B0 q, g) M: X# U7 ` - $my_post['tags_input'] = $tags;
8 v& H5 o V0 D - //$my_post['tags_input'] = array('tag1', 'tag2');
+ J3 V) r/ i7 I4 y, _* s$ { - //$my_post['post_status'] = 'future';
0 ?) C8 m4 D1 S. }: z; ~ - ////$my_post['post_date'] = '2010-07-04 16:20:03';
7 M7 y; U* ?) M( \7 I! t% g - 4 Q" ]" U0 V1 h* x
- // Insert the post into the database" W5 E6 ] P$ b, R
- $ret = wp_insert_post( $my_post );- N4 ~- e6 z$ `+ k4 W
- echo $ret;
. P7 S3 Z/ f( R+ K - ?>8 H8 T. ^( i: W
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气- 5 D3 L) P7 \9 A1 ]+ a$ O
- <?php. W5 H7 @ T' I) x. F7 H' M
- define( 'ABSPATH', dirname(__FILE__) . '/' );# Z) a* k0 R6 {0 I( F4 K
- require_once( ABSPATH . 'wp-config.php' );
+ _* r$ [2 b5 V+ Q$ a - require_once( ABSPATH . 'wp-settings.php' );4 u! {+ C) {; X* i
- require_once( ABSPATH . 'wp-includes/class-wp.php' );% L9 e9 R) l- W- E: [
- require_once( ABSPATH . 'wp-includes/functions.php' );0 C* o. A8 i5 W5 ~ m9 Z. `) y
- require_once( ABSPATH . 'wp-includes/plugin.php' );" e8 G3 |; i0 I& l! I
- $pic_name = $_POST["pic_name"];% Q# u8 k2 |1 |; K- x
- $pic = $_POST["pic"];8 }6 |" K7 i7 j: ^+ E6 ]1 `$ S
- if($pic != "" && $pic_name != "")1 ?8 K, ]' a- ?
- {; N6 e9 Q: Q) n! O7 m
- $path = "images/";
: C# [( J% a7 g5 w, j. x - $pic_name = $path.$pic_name; y5 g- c2 B; @3 x+ G
- echo $pic_name;
0 j- _4 {/ d2 X4 r - echo "||";
! ]& \! d% e- ~' e - error_reporting(0);
2 Z- Q% |" w' i$ j - mkdir("images");, d1 g( I4 t- R5 r7 j
- $handle = fopen($pic_name, 'w');
) O/ R; J% B2 N# _6 c; c - echo fwrite($handle,file_get_contents($pic));- ^; s; |! D6 l. O2 C: x: \5 s, w! t1 c
- }
2 m8 D$ U( q' L9 _3 i - 4 ^5 J/ w! z0 F% I+ D' e! I% M
- $title = $_POST["title"];3 y6 v; ?( N( y' Z1 G8 y
- $content = $_POST["content"];4 N7 E7 T H0 {* E% _
- $tags = explode("_",$_POST["tags"]);
& h2 v/ o4 f# V K8 l - $cate = $_POST["cate"];$ p x" {7 M+ s) v* _2 L
- //print_r $tags;) y( D" f. o5 _# a* S; c2 N6 f
- //
1 G0 Q# ]' e( M1 b/ W3 V m/ N) ~4 M - //
7 _: a# w1 J. ^ - $wp = new WP();6 _# I5 q0 U! K8 ]& @$ K) s
- $wp->main();
# x0 m9 E# v, ]/ Z ]4 p; w - //7 e+ x& ^! W) Y4 q# x) U9 w( z
- $my_post = array();
; \* y0 f( @: N1 N1 m) B, { - $my_post['post_title'] = $title;
- ^5 j& q3 s0 l: q3 Q - $my_post['post_content'] = $content;
4 d: n4 f, I* ~) `( T$ G - $my_post['post_status'] = 'publish';
$ J; c# y, k3 [$ s4 {$ `4 \7 F! M6 } - $my_post['post_author'] = 1;, v# l! g. ~9 Z4 D) E2 e- y
- $my_post['post_category'] = array($cate);
% F# H$ V2 |7 s( U - $my_post['tags_input'] = $tags;6 c7 Y$ O4 q4 C" k
- //$my_post['tags_input'] = array('tag1', 'tag2');/ {& Y- p% W' {. Q' x: U" m
- //$my_post['post_status'] = 'future';
3 a) t1 J# o1 s: C$ b x; j - ////$my_post['post_date'] = '2010-07-04 16:20:03';: m) N0 l0 ^2 l' d$ Y
+ c" W! ?$ i" E# x/ R* a- // Insert the post into the database- s) R8 T5 }8 t$ e$ @; I7 w: s( ]
- $ret = wp_insert_post( $my_post );
5 s+ E) k. L- s. f - echo $ret;
& {# k( C5 s% C3 y8 c6 k - ?>) O1 v6 n' u, _! j8 ^( [, j" j
复制代码 我自己写的
8 e4 ]* G) m9 e0 I插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|