让WordPress每篇文章的发布时间至少间隔1小时

作为WordPress多作者博客,也许你会希望发布文章的时间不要凑在一起,有一定的时间间隔可能更适合读者。比如,让WordPress每篇文章的发布时间至少间隔1小时,就是一个不错的做法。

post-publication-spacing-in-minutes-wpdaxue_com

将下面的代码添加到当前主题的 functions.php 文件:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
//设定每篇文章的发布间隔 
//From https://www.wpdaxue.com/post-publication-spacing-in-minutes.html
function force_time_between_posts($data, $postarr) {
	global $wpdb;
	if (empty($postarr['ID'])) return $data;
 
	$latest = $wpdb->get_var("
		SELECT post_date
		FROM {$wpdb->posts} 
		WHERE post_status IN('future','publish') 
		AND post_type = 'post' 
		AND ID != {$postarr['ID']}
		ORDER BY post_date DESC
		LIMIT 1");
	$distance = 60; // 时间间隔(分钟)
	$latest = strtotime($latest);
	$current = strtotime($data['post_date']);
 
	if ($latest < $current) {
		$diff = $current - $latest;
	} else { 
		$diff = 0;
	}
 
	if ($diff >= 0 && $diff < ($distance * 60)) {
		$new_date = $latest + ($distance * 60);
		$date = date('Y-m-d H:i:s',$new_date);
		$date_gmt = get_gmt_from_date($date);
		$data['post_date'] = $date;
		$data['post_date_gmt'] = $date_gmt;
		$data['post_status'] = 'future';
	}
	return $data;
}
add_action('wp_insert_post_data','force_time_between_posts',1,2);

//设定每篇文章的发布间隔
//From https://www.wpdaxue.com/post-publication-spacing-in-minutes.html
function force_time_between_posts($data, $postarr) {
global $wpdb;
if (empty($postarr[‘ID’])) return $data; $latest = $wpdb->get_var("
SELECT post_date
FROM {$wpdb->posts}
WHERE post_status IN(‘future’,’publish’)
AND post_type = ‘post’
AND ID != {$postarr[‘ID’]}
ORDER BY post_date DESC
LIMIT 1");
$distance = 60; // 时间间隔(分钟)
$latest = strtotime($latest);
$current = strtotime($data[‘post_date’]); if ($latest < $current) {
$diff = $current – $latest;
} else {
$diff = 0;
} if ($diff >= 0 && $diff < ($distance * 60)) {
$new_date = $latest + ($distance * 60);
$date = date(‘Y-m-d H:i:s’,$new_date);
$date_gmt = get_gmt_from_date($date);
$data[‘post_date’] = $date;
$data[‘post_date_gmt’] = $date_gmt;
$data[‘post_status’] = ‘future’;
}
return $data;
}
add_action(‘wp_insert_post_data’,’force_time_between_posts’,1,2);

以上代码的 15 行的 60 为时间间隔,你可以根据自己的需要修改,默认为 60 分钟。

代码的效果:发布新文章时,会自动检测上一篇文章的发布时间,如果超过60分钟,就直接发布,如果小于60分钟,就自动定时间隔60分钟发布。需要注意的是,所检测的是所有已发布和定时发布的文章中的最后一篇。如果最后定时的那篇文章是第二天早上8点,那你后面写的文章,想定时到第二天早上8点以前都是不允许的,会自动定时为9点。

参考资料:http://wordpress.stackexchange.com/questions/104677

本人擅长Ai、Fw、Fl、Br、Ae、Pr、Id、Ps等软件的安装与卸载,精通CSS、JavaScript、PHP、ASP、C、C++、C#、Java、Ruby、Perl、Lisp、Python、Objective-C、ActionScript、Pascal等单词的拼写,熟悉Windows、Linux、OS X、Android、iOS、WP8等系统的开关机。

通过下面的方式来联系我们:

电邮:138762189@qq.com

联系QQ:点击这里给我发消息

官方站:www.tadke.com

※ ※ 联系请加我的企鹅号 ※※

※ ※技术支持请微信联系站长 ※※

Copyright © 2023 Tadke.com. 琼ICP备20000547号