也想出现在这里?联系我们

实现wordpress文章发表时间格式为“几分钟前”的方法

2023.11.21 wordpress教程
  • 文章介绍
  • 升级版本
  • 评价&建议

要在WordPress中将文章发表时间格式为“几分钟前”,您可以使用以下方法:

  1. 使用自定义功能:
    您可以在主题的functions.php文件中添加以下代码来实现这个功能:
function custom_relative_time($post_date) {
    $current_time = current_time('timestamp');
    $post_time = strtotime($post_date);
    $time_difference = $current_time  $post_time;

    if ($time_difference < 60) {
        return '刚刚';
    } elseif ($time_difference < 3600) {
        $minutes = round($time_difference / 60);
        return $minutes . '分钟前';
    } else {
        return human_time_diff($post_time, $current_time) . '前';
    }
}

function custom_display_relative_time() {
    echo custom_relative_time(get_the_time('U'));
}

add_filter('the_time', 'custom_display_relative_time');

这个代码将在文章发布时间处显示相对时间,例如“刚刚”或“几分钟前”。

  1. 使用插件:
    如果您不想编写自定义代码,还可以考虑使用WordPress插件来实现此功能。一个受欢迎的插件是 "WP Relative Date",它会自动将文章发布时间转换为相对时间格式。您可以在WordPress后台的插件部分搜索并安装它。

无论您选择哪种方法,都可以在文章中将发布时间格式为“几分钟前”来提供更友好的时间信息。

有用1
  • 2023.11.21初次和大家见面了!

等待您对该主题的建议

发表评论

还能输入240个字

Hi, 欢迎加入Wordpress技术交流群,带你装逼带你飞!

我要入群
也想出现在这里?联系我们
wordpress加速

我来推荐一个更牛逼的给你看看?

  • 猛戳我吧