|
|
本帖最后由 finder 于 2014-12-28 15:13 编辑 _6 u( l8 N. u3 a: f! h/ [$ _! e$ K
6 u. c! w9 \, ]* h6 \直接插入数据库 字段太多混乱 同时MYSQL 还得支持外连 很多虚拟空间不支持
( r$ Y4 f: z8 h/ ?# T8 D( c: a, k2 i) Q; R+ q
我们可以利用 wordpress 自带的函数 wp_insert_post 调用插入一个新的文章 如果你采集的文章直接使用一个post操作就可以了
4 s. P- G% ]; E5 b Q; Z! r
3 l: [- ?# p, I9 J" E方便实用 简单 何乐而不用呢- - y" F5 u$ ~: p6 h% j# O# N; W: a
- <?php
4 V( P! Q, {) W. B# v% E
+ z, o2 K; L( d8 w- define( 'ABSPATH', dirname(__FILE__) . '/' );
" h! Y; R, t8 f. a F Y3 B - require_once( ABSPATH . 'wp-config.php' );
( f5 a) d$ z0 w - require_once( ABSPATH . 'wp-settings.php' );
5 Z' G+ Y2 ?5 ?. C2 T' Z - require_once( ABSPATH . 'wp-includes/class-wp.php' );
6 Y+ Y& q" S; s# ]9 R/ F - require_once( ABSPATH . 'wp-includes/functions.php' );3 R4 n6 T; U4 i/ c W- T2 A
- require_once( ABSPATH . 'wp-includes/plugin.php' );
l F; O( T8 j! u4 N! D2 m9 A/ f; N - $title = $_POST["title"];" g/ h v* G) z3 g0 C
- $content = $_POST["content"];0 S6 H) B4 c# l$ T* f5 L `$ `
- $tags = explode("_",$_POST["tags"]);5 ~3 }8 n* I0 M2 Y* D
- $cate = $_POST["cate"];
G( `* W8 L3 g - //print_r $tags;2 e$ D" a0 n0 e2 ^
- //
# y2 U* w8 F& Y G3 g8 u! r - //
, D' ]6 e6 T& V! F" l! ?# ~. H; e - $wp = new WP();
/ W& Q6 V0 f- k* I# `; d; N3 }& V - $wp->main();4 m( R A) P/ k' d& {8 j+ H$ v/ M
- //
# @1 s% P. v, e: Q# b& x. ] - $my_post = array();/ |5 R" K; P! n- x6 A
- $my_post['post_title'] = $title;
- g+ ~& r: p$ @/ y6 G/ @ - $my_post['post_content'] = $content;
6 G5 o1 Q/ c6 R2 @2 \: l - $my_post['post_status'] = 'publish';8 A1 n. ^: Z5 e0 O9 |1 {# k" @
- $my_post['post_author'] = 1;: |9 e' x" i& X1 g( g2 L
- $my_post['post_category'] = array($cate);, k2 c6 s/ d, B. y1 N
- $my_post['tags_input'] = $tags;, ], h" Q( s( z. y" v
- //$my_post['tags_input'] = array('tag1', 'tag2');/ n, @; Y6 a- {! H. w3 y6 M
- //$my_post['post_status'] = 'future';; q" f. B9 [: I4 E8 [
- ////$my_post['post_date'] = '2010-07-04 16:20:03';% { L( W; w9 m! Q9 A4 Z
9 Y: Z: ?4 N: B2 [! n7 v- // Insert the post into the database
1 b+ e: g1 q5 B - $ret = wp_insert_post( $my_post );
5 P+ E( \$ w& K3 x" J) S - echo $ret;
: \3 L. |* `% v1 a0 B - ?>: N! m9 z$ @: X/ v' {
复制代码 下面是更带更新图片版因为有些网站屏蔽了盗链接 小气
* ~) I. r) L! |! ^, l- <?php8 L. p8 m2 M- D1 ]/ ^/ R
- define( 'ABSPATH', dirname(__FILE__) . '/' );' @ S7 w) [; u
- require_once( ABSPATH . 'wp-config.php' );2 b+ h. M" h: ]2 o
- require_once( ABSPATH . 'wp-settings.php' );
' M0 p0 L6 E, ~1 T" `- f: t - require_once( ABSPATH . 'wp-includes/class-wp.php' );
) e3 a* h) s( a - require_once( ABSPATH . 'wp-includes/functions.php' );
# k) i0 n" P; Z+ _$ C - require_once( ABSPATH . 'wp-includes/plugin.php' );% A ?) {3 u" m. @0 V) a% k
- $pic_name = $_POST["pic_name"];7 K) ^3 ]# J) X
- $pic = $_POST["pic"];$ N% F9 B: ]! Q: {# T
- if($pic != "" && $pic_name != "") W0 G/ q: X7 E* E; L3 Q4 [
- {
3 a+ T# h8 H9 d& K2 W1 D - $path = "images/";' |$ h5 ~, R( [3 r3 q# f
- $pic_name = $path.$pic_name;. l) T4 u( c1 A$ P2 X3 E7 ]
- echo $pic_name;; X9 U+ F3 b$ G
- echo "||";
6 y% K$ Y% a1 E - error_reporting(0);" Z( v$ J+ b- I/ L! U1 P
- mkdir("images");
2 C$ D4 u* T( |+ g: z2 N/ j, h3 B - $handle = fopen($pic_name, 'w');
0 x1 w3 u/ Y' q( g* G$ l& O - echo fwrite($handle,file_get_contents($pic));
: [% [' y7 ~+ n/ |% R. S - }! e; n3 f6 l5 b1 D" l
- & }9 t7 T. E; \5 L3 E! P
- $title = $_POST["title"];
/ l$ X% o7 T, C5 r6 D9 K$ H - $content = $_POST["content"];* ~. h3 E0 ^$ i# J( m: i; c
- $tags = explode("_",$_POST["tags"]);
) r3 g+ Q% B& F c/ ] - $cate = $_POST["cate"];! R6 m5 Q( m0 v% z- `; G' E' u
- //print_r $tags;
" r* w/ e5 X' \: g9 u7 K - //
# [5 a% C& D8 F0 m6 ]" G( h3 ~4 D - //+ i# e+ d) M( V) `, G% U
- $wp = new WP();
! C6 t9 B+ {. \$ \7 c - $wp->main();
) j/ F$ c4 }% K' E8 @ - //
1 Q( I" e9 j* Q. Q7 W, w8 a/ G - $my_post = array();
' y% O6 Y8 E1 W: ^ - $my_post['post_title'] = $title;
+ j0 V- j) I) F9 x8 k2 G6 k. F - $my_post['post_content'] = $content;
* ?4 N' \8 V2 m3 i$ t; X - $my_post['post_status'] = 'publish';' w0 q" b" X% A/ Q6 C: _5 {' h
- $my_post['post_author'] = 1;
" h: H- K) q8 C1 t3 y( o8 y - $my_post['post_category'] = array($cate);5 a; W* A3 e" N: P; f+ u
- $my_post['tags_input'] = $tags;3 Y F: ]7 i! B4 l% y B; C; Z
- //$my_post['tags_input'] = array('tag1', 'tag2');
k5 ~: m; L4 u" |* H3 I - //$my_post['post_status'] = 'future';) s+ Q" C& d8 `, y
- ////$my_post['post_date'] = '2010-07-04 16:20:03';
/ F: H' ^/ I( T& S1 S
" ~% _& J. P; x- // Insert the post into the database
8 c+ N% c9 _- s _% ?5 ^, S+ ` - $ret = wp_insert_post( $my_post );' w& q I; R9 C; O' u( Z* p
- echo $ret;2 {3 h: @7 D' j4 E' l$ X" l
- ?>
" o- z' |& j- u" q
复制代码 我自己写的
9 F. @' A$ y. t插入效果看图 不看广告看效果 已经测试过有需要的 拿去 |
评分
-
查看全部评分
|