|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑 ' h O% F4 \+ n) e2 Q9 `8 n
8 l$ A& @% ^9 U直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持% \4 a) W0 W, ~" u" I H+ Y
4 \( ]+ v) ^+ E6 t; l0 F
我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了
4 E& [+ S! ~ ~) G6 v% f3 |* k' c q& g
方便实用 简单 何乐而不用呢
5 V: n4 I& a3 @0 V- <?php
4 y: t" d3 }) d& R - ; W9 a4 i( A0 \- r
- define( 'ABSPATH', dirname(__FILE__) . '/' );! D" r# f" K( i: [! n z
- require_once( ABSPATH . 'wp-config.php' );& Z9 A; I9 {6 N% e3 U8 O6 G$ K& y
- require_once( ABSPATH . 'wp-settings.php' );: n6 {0 U. c( x: n4 [* @4 {! N
- require_once( ABSPATH . 'wp-includes/class-wp.php' );
5 C. H3 P5 F4 E2 u8 W5 ^0 S3 ~! f - require_once( ABSPATH . 'wp-includes/functions.php' );) x! i" @' q2 B0 Z, R
- require_once( ABSPATH . 'wp-includes/plugin.php' );
2 t' c" a0 Q3 b/ _% x - $title = $_POST["title"];
; p+ M2 F5 a9 _' s* H% @2 Z - $content = $_POST["content"]; o; k) P ^2 \
- $tags = explode("_",$_POST["tags"]);, Q% X& n4 {+ @' m* v2 n6 ]# g ]5 z
- $cate = $_POST["cate"];
3 k% F; C! g0 D @% F - //print_r $tags;
& s+ U! v5 Y0 n; G - //9 |9 Q8 y( B# f
- //* g Q, U4 e" t
- $wp = new WP();7 {3 ]. _5 U# ]+ A4 F+ @* C+ O
- $wp->main();
& s6 A& a% S! h! g) @ ?2 i+ ~3 W - //
/ v1 n0 {. o# X4 J/ W% M1 [ - $my_post = array();
9 k# Q+ A5 {! i& `& ? - $my_post['post_title'] = $title;
9 h4 `* p! e& j7 K! Y7 Z! f# ? - $my_post['post_content'] = $content;
" \: g0 P8 _7 Y' R& w: t - $my_post['post_status'] = 'publish';
5 b) N0 R6 W6 j/ h1 d - $my_post['post_author'] = 1;
6 Y% d: r2 g- h' v' D7 ^3 l) L - $my_post['post_category'] = array($cate);
: T( e( |. }. C6 U+ A - $my_post['tags_input'] = $tags;
- @ ?2 R; T3 {8 F - //$my_post['tags_input'] = array('tag1', 'tag2');' M) t% E2 Z$ O& V" a* ?" D$ x" x- h
- //$my_post['post_status'] = 'future';
: A8 D3 E( B7 Q2 y/ r - ////$my_post['post_date'] = '2010-07-04 16:20:03';% N! S0 g' S; `+ }! X, Y7 A
8 D% f4 O, \ h% H, h4 n- // Insert the post into the database
/ m8 f$ Y4 [" F) \* @* }) x - $ret = wp_insert_post( $my_post );
d: V- B+ D/ u* A' h - echo $ret;* H9 Z" s, A, f" y
- ?>
$ r/ M, Z& ?) Y8 K& c
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气- 2 A8 s7 ^/ n5 L. S
- <?php
/ b) X2 w) p7 b. H5 Y/ i - define( 'ABSPATH', dirname(__FILE__) . '/' );
% f# I2 ?3 w2 W# } - require_once( ABSPATH . 'wp-config.php' );, {& G; q: N5 ~( P+ [) [( \7 p; A
- require_once( ABSPATH . 'wp-settings.php' );
! k7 f% k2 H+ a3 W& |/ s - require_once( ABSPATH . 'wp-includes/class-wp.php' );0 V, J- H. g' m8 a$ N. E3 h
- require_once( ABSPATH . 'wp-includes/functions.php' );* c' M( H# ~* ~
- require_once( ABSPATH . 'wp-includes/plugin.php' );
$ r |3 y- ^% k" ^ - $pic_name = $_POST["pic_name"];2 Q& }5 N, n9 h
- $pic = $_POST["pic"];* O, h5 i! h0 H1 U0 ^$ f
- if($pic != "" && $pic_name != "")* a3 K {' a& q d& ]# R
- {, C( W$ H3 I# D$ o
- $path = "images/";! l" h; {1 M( w t/ Q" [
- $pic_name = $path.$pic_name;1 T( o, X0 z* q$ z
- echo $pic_name;; H3 j# M7 D+ j
- echo "||";
5 J# ~; h4 m" M( R - error_reporting(0);1 M- Q1 ~. _) J
- mkdir("images");) {) o7 I% D- u" N3 b/ r
- $handle = fopen($pic_name, 'w');
( X. A# k( I: u2 {# U/ U - echo fwrite($handle,file_get_contents($pic));
* s: ~4 h1 Y' _& _3 B - }
1 s3 m( i$ K/ N& k - 2 j( x$ g5 l% F5 i8 ?/ U9 O- H
- $title = $_POST["title"];9 l4 B% C$ \# L/ ^7 U
- $content = $_POST["content"];0 A |5 ?: U% s- `! d# L- @
- $tags = explode("_",$_POST["tags"]);
: w4 h* W. i! _" f- j - $cate = $_POST["cate"];& H0 y) o, d$ c6 I
- //print_r $tags;' W3 q. V' s! `2 D# R
- //9 o+ t/ v! q1 g0 W
- //
: L5 l) L% e, F$ B$ ~" {1 z - $wp = new WP();
- R* f2 X" \7 f - $wp->main();
6 B) Y7 b8 g2 A& `2 y' A - //3 h1 q* J) Z1 S6 `0 Q
- $my_post = array();/ W0 U* S3 [' ~( i, T
- $my_post['post_title'] = $title;6 C. b9 |; E- L& n2 C
- $my_post['post_content'] = $content;
; y: C0 `- B! K: w - $my_post['post_status'] = 'publish';
& }" w% A3 }8 `9 c, Z& ~+ ] - $my_post['post_author'] = 1;3 A$ |8 B7 o/ d- D( E, i: o
- $my_post['post_category'] = array($cate); v0 H0 _' @0 ~ r
- $my_post['tags_input'] = $tags;3 n- c# ?0 ~0 \9 \" c7 _
- //$my_post['tags_input'] = array('tag1', 'tag2');
, b5 C" [" Q* K# n6 t) j - //$my_post['post_status'] = 'future';- q0 h7 Z5 U& a9 ?$ ~
- ////$my_post['post_date'] = '2010-07-04 16:20:03';8 d4 @& N- n/ U0 R
- 1 L: x ?! P% Q) p# I e3 M" D
- // Insert the post into the database1 q+ L5 i/ U2 m* O- d. ?4 Y
- $ret = wp_insert_post( $my_post );- q0 W) G8 J5 D+ ?9 j# i
- echo $ret;4 u, ?% ^/ f+ j. @
- ?>+ X/ B, d4 R3 `# t/ J) V
复制代码 我自己写的
, p7 n) R; p4 W- l0 R' U& U, w! r: C' R插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|