|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑 8 X, G- v- h$ ]& j
6 T( i5 {- o9 |2 t! d% {/ K
直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持, {+ W; L' L: B. c
( Q8 }. n- ~, W! T) ?9 U我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了3 G1 G7 F4 ]$ _% D% K7 L- k
3 T, y( x% @& D/ A) I0 S' O9 @$ x, P3 ]
方便实用 简单 何乐而不用呢- 1 I" V! o, l: \ H7 ?- F' x
- <?php
! v* k. [0 p' N
' O8 q6 C) ?% }' t8 K4 s/ |, H- define( 'ABSPATH', dirname(__FILE__) . '/' );
) T) q) a3 u; g* g0 F - require_once( ABSPATH . 'wp-config.php' );% b, Y% S" J4 Q2 m
- require_once( ABSPATH . 'wp-settings.php' );
) o! V! @# I: @5 S - require_once( ABSPATH . 'wp-includes/class-wp.php' );
' f/ B1 O3 v3 N6 Q$ w3 d7 i - require_once( ABSPATH . 'wp-includes/functions.php' );( r" C! E6 Y4 e0 H0 s' g
- require_once( ABSPATH . 'wp-includes/plugin.php' );
$ `% s+ B: g& G7 ~ - $title = $_POST["title"];5 x! x! s- H, G7 u* a+ Y0 q
- $content = $_POST["content"];
; ~9 F ^, {! r" Q5 ` - $tags = explode("_",$_POST["tags"]);# F% ~. N' A7 n* t- k
- $cate = $_POST["cate"];( v/ W! r( i; |$ n0 W8 p6 P
- //print_r $tags;
* i( r. \0 x. Y8 k! L/ Y - //, q* m/ B$ O3 r! D( i
- //" y" _4 `4 ]# p- K! I
- $wp = new WP();( n6 [% j! n# ? N
- $wp->main();
o/ k' x$ N0 c/ q& r. ?! A - //* [4 J! @) M+ v& E
- $my_post = array();# [$ t; e# C+ V" ?% g; {
- $my_post['post_title'] = $title;
6 Z0 Z: ~9 ~# j, ~& \: N - $my_post['post_content'] = $content;
3 j' u. W& _. f! [ - $my_post['post_status'] = 'publish';( R* W. g8 h8 E0 I5 ~1 i
- $my_post['post_author'] = 1;4 V6 l; `+ H: M7 B- ?4 Z( q
- $my_post['post_category'] = array($cate);
# T. e7 C+ \1 C - $my_post['tags_input'] = $tags;
% f; |3 P2 J5 x6 E; ?# p7 D - //$my_post['tags_input'] = array('tag1', 'tag2');
1 @9 V5 T) O" m+ P1 Z4 E - //$my_post['post_status'] = 'future';
8 a. S* X* `; ]2 ]0 A! Y+ V2 Z - ////$my_post['post_date'] = '2010-07-04 16:20:03';
B( W5 ~9 m. k# ^
0 ?/ ?! _- H, x; ~0 f- // Insert the post into the database
" N2 h( o" A( a3 ?; | - $ret = wp_insert_post( $my_post );
. u# O" G% t3 p5 Q, p/ I1 m - echo $ret;
* G( P/ h. H* _" r5 V! L - ?>
1 E7 F# p! a: C% G: ~4 ^: u
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气
3 h3 I7 Q, e% l* U% R, L8 @- <?php
7 M) ?5 _, _4 D2 o6 i - define( 'ABSPATH', dirname(__FILE__) . '/' );
+ L2 o8 b$ M/ v - require_once( ABSPATH . 'wp-config.php' );) x# ^2 W1 V/ I! q8 S6 O
- require_once( ABSPATH . 'wp-settings.php' );& ^6 G: b6 S( _* p' o
- require_once( ABSPATH . 'wp-includes/class-wp.php' );
5 h) ^% c3 Z ^# Y" O! y - require_once( ABSPATH . 'wp-includes/functions.php' );# h, `8 m# l$ B8 e9 I! q
- require_once( ABSPATH . 'wp-includes/plugin.php' );
9 ~* [& k) o9 r* @7 Z& ? - $pic_name = $_POST["pic_name"];) u, A( @' R- m' V5 z
- $pic = $_POST["pic"];
% O4 i3 B* e' Y5 O/ M+ r& n( W& Y - if($pic != "" && $pic_name != "")
, W( d/ r. X0 Q* W - {
: k/ h' M) Z4 r) ~0 d - $path = "images/";
" U Z* W# Z+ c+ j, Y: | - $pic_name = $path.$pic_name;' p2 \: t- A: ]5 P3 t. I6 C" }3 q
- echo $pic_name;
) e6 Z' H- v+ c% S" ]- g - echo "||";+ c" R" y& s, Z
- error_reporting(0);8 ^' w X- a9 x. B) B1 v9 v
- mkdir("images");% `& n& K* f% K. R- _; {# E
- $handle = fopen($pic_name, 'w');
+ e. I) Q/ s5 a# g; X - echo fwrite($handle,file_get_contents($pic));
/ E1 U; I+ }5 M* O+ u - }
$ w7 T$ u" Y2 M7 p: N/ d' a
' o2 ^% |3 c- u! k$ a% ]- $title = $_POST["title"];7 S4 U& Y: G- L4 p) _
- $content = $_POST["content"];7 e2 ]* v! m" s# b0 o& ~
- $tags = explode("_",$_POST["tags"]);8 N9 J) u5 X7 R( q& B' d
- $cate = $_POST["cate"];0 `6 ~8 F$ Z1 j( @( D$ e
- //print_r $tags;
|$ A0 X9 a' V' ?8 e* |8 ] - //) x5 j+ V L4 v3 l, E+ |+ M
- //. Y: X% M6 f" ]8 c" o0 {
- $wp = new WP();
( k; [# x1 b* g+ Q) a0 ^1 |/ B - $wp->main();
{4 B' ~! z( u. [/ W. O - //0 m9 H, ] F! w: y9 Z2 w
- $my_post = array();+ P& g# G7 }3 p$ ?8 F+ }1 c& f$ U
- $my_post['post_title'] = $title;. A# B4 u) o5 g$ N4 U
- $my_post['post_content'] = $content;
& G5 f; h% Q1 J" _/ C - $my_post['post_status'] = 'publish';9 ~' {' G, i' h+ y2 o( R
- $my_post['post_author'] = 1;( T4 S' Z7 |7 t/ G5 V, u
- $my_post['post_category'] = array($cate);' g4 O( r7 P- d/ z4 T: t2 ?
- $my_post['tags_input'] = $tags;
9 g9 Q. |* V7 x9 q n+ l7 p) U2 ` - //$my_post['tags_input'] = array('tag1', 'tag2');
+ I( h1 ~. I& g% N# u, g! |( u - //$my_post['post_status'] = 'future';" b- a6 c4 t% D2 x# E" l6 y
- ////$my_post['post_date'] = '2010-07-04 16:20:03';% z) n( i7 C, N0 u9 \" M8 _1 C g
- " `/ u* ~2 k' b* w
- // Insert the post into the database2 T) F: P, X- @
- $ret = wp_insert_post( $my_post );/ E0 `$ E) v! J4 D
- echo $ret;" a' _) e5 t! t! N' @
- ?>3 z. S6 V9 Y) c: {: \$ z
复制代码 我自己写的
: g9 [/ [ j* Z: K) N插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|