CN2.HK 是专业经营国际CN2网络的服务商,为大陆中小企业提供稳定、安全、合规的国际互联网接入服务。

香港云 cn2.hk 香港云 cn2.hk

服务热线:400 888 9543

服务信箱:idc@cn2.hk

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

post-publication-spacing-in-minutes-wpdaxue_com

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

//设定每篇文章的发布间隔
//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


发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注

在线客服