|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑 / N k1 C, z& r& W" n0 _
3 a. }7 S7 V4 B8 r# U
直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持1 `$ a: m5 P' K5 z
( U" D/ u# }8 q3 l
我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了' {4 s' [& R2 w% s. H7 ~1 H
- q) ]6 H5 X7 v* I
方便实用 简单 何乐而不用呢- * K" G" A, z% Z6 d1 H; d: V
- <?php
- _7 B/ V1 ~3 X( r a6 n - 7 `/ C: T: i2 A
- define( 'ABSPATH', dirname(__FILE__) . '/' );
2 p" j2 }0 x' z9 G/ D, r2 D - require_once( ABSPATH . 'wp-config.php' );
9 l! a+ o/ p6 B* ?5 Q - require_once( ABSPATH . 'wp-settings.php' );
. |1 y6 C) b) h" P6 R" Y - require_once( ABSPATH . 'wp-includes/class-wp.php' );0 _0 ]1 F& w7 ?8 t6 M) R
- require_once( ABSPATH . 'wp-includes/functions.php' );
0 M" E, o* k3 F) D- W - require_once( ABSPATH . 'wp-includes/plugin.php' );
+ L( I" T* T& j* M# j) T# C8 X - $title = $_POST["title"];9 K+ e9 F% S- U, y) L5 h
- $content = $_POST["content"];
4 Y$ F6 J d4 }& P4 g& A - $tags = explode("_",$_POST["tags"]);' ^- |) Z8 b; ]9 v
- $cate = $_POST["cate"];& Z2 g9 r5 m; n5 Z" L; i
- //print_r $tags;, C Z5 c0 n; d1 `
- //% G) ~# e7 f$ j
- //8 M9 k! `4 A% B2 C% q" q; H
- $wp = new WP();1 r: |# \9 Z5 D& ~9 Y ?" X9 ]; O# K
- $wp->main();* R3 N5 g) F1 t
- //
- f6 g2 M* O2 y& Q& l/ ^ - $my_post = array();
$ z9 T4 W( G4 m# N - $my_post['post_title'] = $title;8 ?0 f6 |3 b4 b# S0 V3 U
- $my_post['post_content'] = $content;
: V# L0 I* T7 } L( N# T - $my_post['post_status'] = 'publish';: i) n! |/ b( I: D2 B2 a3 H6 b
- $my_post['post_author'] = 1;
! ?( ] ^1 ^) u# c5 V" x8 L/ { - $my_post['post_category'] = array($cate);
/ F" J4 p1 u2 V: t - $my_post['tags_input'] = $tags;
% K2 p. S! z9 O - //$my_post['tags_input'] = array('tag1', 'tag2');
$ B% ^1 \2 B' z8 ]# A6 p3 H - //$my_post['post_status'] = 'future';
3 G% ^. ^" y" x$ w- K& J - ////$my_post['post_date'] = '2010-07-04 16:20:03';
0 {- z9 h7 {1 f" {
2 {/ P4 i) Q$ _7 d2 p K- // Insert the post into the database
8 A; Z6 J7 q* {, a1 Y - $ret = wp_insert_post( $my_post );9 H" J+ Z. y: t& s9 U7 Y! \
- echo $ret;( G! b' q, T% W* H
- ?>& Q z4 I: j6 q+ }0 k1 [$ c A: S
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气- 0 W" m( K% f1 o
- <?php) z6 u1 O+ |. A8 V
- define( 'ABSPATH', dirname(__FILE__) . '/' );/ f& I7 C- j" t x) h) C8 {
- require_once( ABSPATH . 'wp-config.php' );
; I0 h' V, }% \ }( u3 [+ R - require_once( ABSPATH . 'wp-settings.php' );
9 H+ Q& U! M4 P$ Q6 n9 v3 v - require_once( ABSPATH . 'wp-includes/class-wp.php' );
6 X; M4 Z. n, A, a& l9 F/ u - require_once( ABSPATH . 'wp-includes/functions.php' );
4 d1 s6 Z3 X2 S( Z - require_once( ABSPATH . 'wp-includes/plugin.php' );7 M4 V& X) R- h
- $pic_name = $_POST["pic_name"];7 V3 |, g2 t+ G2 y$ J. p! k. C
- $pic = $_POST["pic"];
9 F9 B, ~8 o0 B' e# a0 x* i - if($pic != "" && $pic_name != "")7 g) H* A g9 u% i1 f
- {# w6 T' t. J) v
- $path = "images/";% q% H: F0 L7 |( {
- $pic_name = $path.$pic_name;: g; [/ |! U& v) x8 b9 e. T' k
- echo $pic_name;1 C9 z9 @7 n6 @1 z1 o& ^/ g% x
- echo "||";. n& x6 Y0 C1 ]" d0 L
- error_reporting(0);
& \# r% f9 G) G w9 m - mkdir("images");! }. ~$ R. X5 ~- c9 N; Y( B: e
- $handle = fopen($pic_name, 'w');
5 \9 T4 D" l. w+ w A! A8 s - echo fwrite($handle,file_get_contents($pic));
- K0 R2 _+ |% b$ Z; @2 ^3 \$ g1 Q - }2 w5 z# o" W. f9 c# K
$ Y; ]3 G' n+ {% u- $title = $_POST["title"];! h! ?+ ]6 k; {! ] H; F
- $content = $_POST["content"];1 E! d4 V$ N. c8 T3 I
- $tags = explode("_",$_POST["tags"]);, `, _: O; [5 Q0 f
- $cate = $_POST["cate"];8 S/ R" t( V8 [* r8 F5 ^" n
- //print_r $tags;, k" f6 y X& g$ R8 a- e, l
- //
0 N( Y! M7 m) S+ m( l6 k - //
( s* z5 E+ j V- Z( r1 b D - $wp = new WP();
, n/ c. R/ {$ a. O - $wp->main();4 ?& r% |7 p: L
- //
. w2 _# z, r9 t w8 T6 F - $my_post = array();3 I, k; M( M$ a. H6 S4 n# X
- $my_post['post_title'] = $title;: W7 R" q4 G; R
- $my_post['post_content'] = $content;
' U. A7 Z' k T( ?3 Z: n - $my_post['post_status'] = 'publish';* J- z3 b; {( r5 K, ^1 c" A- j$ ]
- $my_post['post_author'] = 1;
% U( X7 g2 v* q3 _9 U3 e - $my_post['post_category'] = array($cate);, B) k3 A: ?! d' N1 d U+ M% H
- $my_post['tags_input'] = $tags;
' ?5 p3 `* J5 P - //$my_post['tags_input'] = array('tag1', 'tag2');1 G! E& D2 J1 \9 X2 N
- //$my_post['post_status'] = 'future';
* z4 ~/ m2 m# h2 W+ R4 }% d - ////$my_post['post_date'] = '2010-07-04 16:20:03';
9 O! s' e+ ~ k
4 F- w" f# a& T0 V9 V- // Insert the post into the database
% [* ]. {4 B" a% ~2 ^ J - $ret = wp_insert_post( $my_post );
: d( s3 }3 I$ n- P% V5 D7 b" N G - echo $ret;4 T! r) D, B* J" }4 C8 O
- ?>
/ \ E6 {3 ^) F( v
复制代码 我自己写的
t6 p, B' u- x+ q3 `插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|