|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑
, p& w2 D9 L$ l( R9 m# U, i5 |+ s6 _0 k ~+ _
直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持! l; i9 s: ~6 [# G& b$ I! c
. M J9 a0 i& k- r8 h" m' s我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了
9 w& y0 X0 g, n- k$ Y' e. ^7 ?1 C; f D+ J8 ?; a
方便实用 简单 何乐而不用呢
9 F6 D- L3 z3 T- |) t! m' a- <?php
) Q6 s4 t! P8 f5 {% @
/ w7 L) s' b- y' n- define( 'ABSPATH', dirname(__FILE__) . '/' );2 x" L$ \7 C& Y( S
- require_once( ABSPATH . 'wp-config.php' );
1 [% T7 A& `& T9 I- T/ Z# r$ O - require_once( ABSPATH . 'wp-settings.php' );
# l. \ M- h/ w8 W* d2 t - require_once( ABSPATH . 'wp-includes/class-wp.php' );( ^8 _ ]( J# `5 j% y
- require_once( ABSPATH . 'wp-includes/functions.php' );
9 z: y7 H, |+ b - require_once( ABSPATH . 'wp-includes/plugin.php' );
- q% y+ D1 n2 ~8 { - $title = $_POST["title"];
7 n8 ]& ^, `. g/ R! W - $content = $_POST["content"];
& V0 _' q& u1 P7 t; [! D' G - $tags = explode("_",$_POST["tags"]);
/ h! A3 S8 S; M8 Y - $cate = $_POST["cate"];
6 F# y, k* a. c" o. j - //print_r $tags;
2 _8 l9 R, ]" y8 U2 }! o- D! [7 R - //; A1 c& g4 a! p* z# o" b
- //% t: A' g& ?8 z1 W
- $wp = new WP();0 P9 Q+ ^( ?. o2 y" y: Q& N* M; @1 P6 O
- $wp->main();
/ v- R0 t1 k, K, C - //7 U4 A0 |9 @2 j+ Z! P( Q
- $my_post = array();
q; @, ?! ]% F$ S# ] - $my_post['post_title'] = $title;
+ K( e* _* ]( v: W: s6 S - $my_post['post_content'] = $content;1 R$ ^; i! G. Z, ^# d$ }
- $my_post['post_status'] = 'publish';5 q$ o7 l# z7 g' ]
- $my_post['post_author'] = 1;
2 u) c @% q( |+ L1 _& D, G1 x& U& \ - $my_post['post_category'] = array($cate);. V- i, d# C1 W
- $my_post['tags_input'] = $tags;
% i; j# ?& o+ P, x1 t - //$my_post['tags_input'] = array('tag1', 'tag2');4 ]4 z2 M. l& @. x$ i: t. `
- //$my_post['post_status'] = 'future';* M. |# r1 {+ Y3 p5 L
- ////$my_post['post_date'] = '2010-07-04 16:20:03';
) T7 o( p/ }# W
# v! v0 e, n0 k) }. C- // Insert the post into the database, {, ^, U7 T! H* J) l
- $ret = wp_insert_post( $my_post );
% M+ J/ M5 B5 |, D7 {- i - echo $ret;; p( u6 l7 m1 Q
- ?>
6 n1 @6 M; R8 k/ M2 A8 h" ]# m
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气
% w/ C4 V5 r1 Q0 Y! r- <?php" Z+ K% v7 |: a7 F
- define( 'ABSPATH', dirname(__FILE__) . '/' );
9 [ Y/ i* A. n0 t4 g% b* L3 C - require_once( ABSPATH . 'wp-config.php' );" i5 J- B2 N" r% |& A
- require_once( ABSPATH . 'wp-settings.php' );: [1 Q) W; k/ n
- require_once( ABSPATH . 'wp-includes/class-wp.php' );
- n7 f$ a# S2 g# r& O/ B; K I - require_once( ABSPATH . 'wp-includes/functions.php' );* i: _+ N8 i7 G$ k1 i( J
- require_once( ABSPATH . 'wp-includes/plugin.php' );
% H: N+ b0 R R# r - $pic_name = $_POST["pic_name"];
& U- j% A1 I0 I - $pic = $_POST["pic"];8 {5 c, T* K+ D# B& W$ |
- if($pic != "" && $pic_name != "")- |! O* q! |6 H1 F; y. a2 o
- {! C# G3 h1 U3 S/ |, _: Z
- $path = "images/";+ A1 d0 n A, Y% n: H
- $pic_name = $path.$pic_name;- [5 @ p9 t& C2 x" V1 u$ F
- echo $pic_name;* w0 m4 `- B3 Q5 w' ^# S# ~
- echo "||";$ W" X Z- S) y, r: M" @& U
- error_reporting(0);( K# R: Y; u* ?0 h
- mkdir("images");' g* k6 Y2 P) z" w
- $handle = fopen($pic_name, 'w');' D+ b* G+ f: F9 f
- echo fwrite($handle,file_get_contents($pic));
: d1 Z4 Q6 i6 g9 |5 y7 c - }
) `3 o' C y0 F1 \! W0 w' p/ z
2 }8 r! O, [% g/ V+ {3 ]& p4 ]4 d8 s- $title = $_POST["title"];
3 g b8 U1 @( @: t; ~9 |9 @ - $content = $_POST["content"];
/ G8 j1 }2 o7 s, `& k* { - $tags = explode("_",$_POST["tags"]);* `- q& D" w! ]
- $cate = $_POST["cate"];1 G0 D- p: {9 s# g
- //print_r $tags;" S2 _& m! f7 p
- //* S- d) f3 S' o& i( [
- //
, G7 v) A7 l: k& B! @0 r3 e - $wp = new WP();
8 A" P- y3 ]/ y+ h - $wp->main();4 I. O% L% J# a8 q# c+ w
- //! W2 N9 b0 X6 |( s: o
- $my_post = array();: f0 U7 F: V6 ?5 w" }' I( z
- $my_post['post_title'] = $title;
/ l& J+ ]/ F5 _7 h/ ^3 b. H4 d - $my_post['post_content'] = $content;
: L9 k8 y% F7 M) i5 ] - $my_post['post_status'] = 'publish';% m) d p1 `$ Z& `
- $my_post['post_author'] = 1;
" }5 Z" R; q. t ~( Q - $my_post['post_category'] = array($cate);
! _( D. f) Y4 h1 D+ _# A, ~ - $my_post['tags_input'] = $tags;7 D- ]0 y ^8 y) b
- //$my_post['tags_input'] = array('tag1', 'tag2');
- o8 l2 e) R0 w W7 ~1 P/ \ - //$my_post['post_status'] = 'future';
+ ?+ C# a# m1 `. D% B3 E - ////$my_post['post_date'] = '2010-07-04 16:20:03';
5 t+ Q, t, ~* |! h8 k/ |2 N - 0 r2 [- w" r @: G
- // Insert the post into the database; D! q& n7 E- {# F5 h- c
- $ret = wp_insert_post( $my_post );
}2 j3 c! V( U/ m" H - echo $ret;( U- t$ H5 n T6 s n
- ?>. u6 e( l$ F. L5 q
复制代码 我自己写的
4 y) @1 b k/ U0 B% v插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|