You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WordPress文章日期显示异常求助:新文章误展示修改日期

解决WordPress文章日期显示问题:区分发布/修改状态

你的代码核心问题是判断逻辑错误——新文章的get_the_modified_date()不会返回空值,它和发布日期完全一致,所以!empty($modified)的判断永远为真,导致新文章也显示修改日期。

以下是修正后的代码,替换你原有的astra_post_date()函数:

function astra_post_date()
{
    $format = apply_filters('astra_post_date_format', get_option('date_format'));
    $published_date = get_the_date($format);
    $modified_date = get_the_modified_date($format);
    // 获取Unix时间戳用于精准比较
    $published_timestamp = get_the_time('U');
    $modified_timestamp = get_the_modified_time('U');

    $output = '<p class="posted-on">';
    if ($modified_timestamp > $published_timestamp) {
        // 修改时间晚于发布时间,判定为已编辑
        $output .= 'Last Modified: <span class="updated" itemprop="dateModified">' . esc_html($modified_date) . '</span>';
    } else {
        // 未修改,显示发布日期
        $output .= 'Published: <span class="published" itemprop="datePublished">' . esc_html($published_date) . '</span>';
    }
    $output .= '</p>';

    return apply_filters('astra_post_date', $output);
}

关键修改说明:

  • 改用时间戳比较:通过Unix时间戳判断修改时间是否确实晚于发布时间,这是判断文章是否被编辑的准确依据。
  • 修复HTML结构错误:原代码末尾多了一个多余的</span>,现已修正为正确闭合外层<p>标签。
  • 设置日期格式默认值:如果过滤钩子未返回自定义格式,自动使用WordPress后台设置的日期格式,避免日期显示为空。

内容的提问来源于stack exchange,提问作者Sef Dee

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.19 11:37:10