要在每次查看WordPress文章时更改其发布日期,我们可以使用 the_post 动作钩子,每次在全局$post变量中设置文章时,该钩子都会被触发。这里搬主题就分享一下设置每次查看WordPress文章时自动更新文章的发布日期的方法。下面是一个示例代码段,你可以将其添加到主题的 functions.php 文件或自定义插件中:function wpxss_update_post_timestamp_on_view() {
if (is_single()) { // only update for single post pages
$post_id = get_the_ID();
$current_time = current_time('mysql');
$post_data = array(
'ID' => $post_id,
'post_modified' => $current_time,
'post_modified_gmt' => get_gmt_from_date($current_time),
);
wp_update_post($post_data);
}
}
add_action('the_post', 'wpxss_update_post_timestamp_on_view');这样设置后,就可以每次查看文章时更改文章时间戳,会影响文章在博客存档页面中的排序,并导致最近查看的文章总是显示在顶部/首页。
THE END