|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑 6 [( l m+ z7 X9 `. e* G
0 M' |+ i) H+ b- L' B0 o5 _2 I- a直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持
L- o) Q2 x. p5 p6 a6 z9 k' U7 T5 N6 q
1 m1 l. X8 M4 i; L7 g6 i' d9 V我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了* {: X& s# X ^7 ]! ]* a- i
* ~+ Z. B2 n. k
方便实用 简单 何乐而不用呢- 4 Z- C' _/ X0 t
- <?php
- p" j) ?2 N# \5 @ - 8 ?- T+ v* \+ ?
- define( 'ABSPATH', dirname(__FILE__) . '/' );
9 L) C% G/ B* ~4 D' r - require_once( ABSPATH . 'wp-config.php' );
# X6 m- O! X6 h6 T: ^/ p - require_once( ABSPATH . 'wp-settings.php' );
, O5 O+ g$ a. P& |4 P7 ]; Q* g9 t) Z - require_once( ABSPATH . 'wp-includes/class-wp.php' );
0 {5 G! D- r: t/ r - require_once( ABSPATH . 'wp-includes/functions.php' );9 w+ u$ I! J* e
- require_once( ABSPATH . 'wp-includes/plugin.php' );' S9 t/ d/ |& p) m
- $title = $_POST["title"];
/ H& E {# n% j% f - $content = $_POST["content"];
2 [2 ~6 n- f+ A- V - $tags = explode("_",$_POST["tags"]);
1 e" }5 {: L3 I4 P - $cate = $_POST["cate"];4 x, e S6 R: x" g& G/ E2 r' d( O, w
- //print_r $tags;
& A( R, _( [% }8 u$ M - //' U& q- Z* O* K8 l% b
- //
3 }. m" o0 n" ~ - $wp = new WP();
3 a$ M* u7 P9 T/ m+ }0 y4 W- E - $wp->main();
6 t! m$ ~! l& c - //
1 }0 V. c0 _, Z) O3 Q - $my_post = array();! W8 S. E D9 [4 Q
- $my_post['post_title'] = $title;( C, r$ B8 u) t3 F u9 {, ~! x
- $my_post['post_content'] = $content;
: Q9 a2 \5 _ S' o( X8 T# }0 v - $my_post['post_status'] = 'publish';
" @! O, i, Q: y r - $my_post['post_author'] = 1;
$ j$ I2 x: ]0 \* E C5 u" [& k - $my_post['post_category'] = array($cate);' V% X* M' i. @/ g6 g
- $my_post['tags_input'] = $tags;) `/ m9 y& G- _2 L
- //$my_post['tags_input'] = array('tag1', 'tag2');
* y# m0 |; u- P* |1 ~ - //$my_post['post_status'] = 'future';
" O$ \* p6 M+ z) u1 x" O - ////$my_post['post_date'] = '2010-07-04 16:20:03';
" F6 v; e( ^7 k - / W) G( E; @0 b" }
- // Insert the post into the database
5 Q, B7 `5 Y% w' S3 l - $ret = wp_insert_post( $my_post );* B! G$ i0 ~) \+ O+ E/ B! `
- echo $ret;
J. p, s: M5 G1 c - ?>5 r) M5 G+ H5 [8 c
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气- 3 P1 l% f$ s" I1 J$ l
- <?php
2 E; D$ S1 c7 R! l5 p - define( 'ABSPATH', dirname(__FILE__) . '/' );
% k0 O) @2 t- Y6 J# d# p* l - require_once( ABSPATH . 'wp-config.php' );- @& S# K2 F& |0 X& Z7 P
- require_once( ABSPATH . 'wp-settings.php' );
R" m7 T/ A. o2 [7 v) }5 @) V - require_once( ABSPATH . 'wp-includes/class-wp.php' );
! S4 ]0 p1 h4 W; ^% n: ^6 n - require_once( ABSPATH . 'wp-includes/functions.php' );
: l: n7 S& f; K# E0 e$ H - require_once( ABSPATH . 'wp-includes/plugin.php' );6 N& D8 a1 @3 _2 h$ W. a
- $pic_name = $_POST["pic_name"];8 ], x6 I0 G1 m9 z- E/ a6 n6 y9 J( e
- $pic = $_POST["pic"];6 A8 \4 J& _5 h: M+ V7 C7 N
- if($pic != "" && $pic_name != "")8 J6 j/ w/ A) U% D7 {, _9 b
- {
2 E$ j! d( _1 h5 U5 x6 r% ^' j - $path = "images/";
H+ k+ t( S" ?' J - $pic_name = $path.$pic_name;
' b( V# Z G- k' g" J - echo $pic_name;
" l1 k; x6 F+ M# {* }3 G+ X0 V+ o - echo "||";* `* m0 y% r4 S, K8 Q% Z T: _
- error_reporting(0);! b4 l3 s6 k5 f! ~" L3 m
- mkdir("images");
' l6 n V+ ~* G# M( z8 P& p6 k3 R: } - $handle = fopen($pic_name, 'w');. C/ Q6 k4 k4 E# ]: O
- echo fwrite($handle,file_get_contents($pic));
9 W- {& D6 ]. ]9 d - }
1 `6 w2 i" A2 p7 T6 c - 5 n: U" ^6 o3 x! S E) i
- $title = $_POST["title"];: i2 e" F4 G3 R+ c
- $content = $_POST["content"];* Y. a' g$ }2 X: {
- $tags = explode("_",$_POST["tags"]);3 o! x u# q5 y
- $cate = $_POST["cate"];
9 {$ L' h1 T, b0 k0 _! z; w - //print_r $tags;/ |& e- F: w! U, e
- //
9 Y# J: t [3 T/ c - //; f& I( M: M' [/ W
- $wp = new WP();4 n8 g2 Y$ E+ Q g3 i+ F+ |
- $wp->main();
. Z- ~9 Q8 `( V7 g7 i) c" U& L2 ~( q - //' c- k; C9 k7 e' P. T1 n3 R
- $my_post = array();! ?) ~" r$ Q2 m3 t1 m
- $my_post['post_title'] = $title;
9 n& e- q) r i" c - $my_post['post_content'] = $content;
$ L- O9 z+ }! c- ` E3 }- J& r" V: a z - $my_post['post_status'] = 'publish';1 x: H9 ` l8 p9 h( d& [! a
- $my_post['post_author'] = 1;
% k/ W0 `3 y% H - $my_post['post_category'] = array($cate);
8 _. L, O. ?6 t; T+ L - $my_post['tags_input'] = $tags;
$ j, x$ c6 S) t6 j. G4 u - //$my_post['tags_input'] = array('tag1', 'tag2');) V% S; k: Q6 A7 f- @" O' Y, D
- //$my_post['post_status'] = 'future';9 b- ]2 j/ S6 Z0 R1 g+ b/ i1 T1 K0 N
- ////$my_post['post_date'] = '2010-07-04 16:20:03';8 R5 [" Q% s) Q: _
( k4 u- o; M' E4 h; q- // Insert the post into the database
0 Z0 F7 L1 h* K* K# [ I- p - $ret = wp_insert_post( $my_post );$ ^4 R9 Z; h5 t2 {8 p8 E5 K& F
- echo $ret;4 G0 C; r; }# d; B3 e
- ?>3 F2 Q* x- ]' Y0 ^6 ?0 a4 x8 H
复制代码 我自己写的5 y/ C9 } M2 N+ l# `
插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|