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

WordPress获取文章日期问题求助及相关index.php代码

解决WordPress文章日期获取/显示问题

看起来你在WordPress列表页添加文章日期时遇到了麻烦,我看你贴的index.php片段里确实没包含日期相关的调用逻辑,这就给你补上适配你代码结构的实用方案,顺便修正了作者链接的小问题(原代码里get_the_author_link()需要用echo才能输出有效链接):

修改后的完整代码片段

<?php get_header(); ?>
<div class="container">
  <div class="row">
    <?php if (have_posts()) { 
      while (have_posts()) { 
        the_post(); ?>
        <div class="col-sm-6">
          <div class="main-post">
            <a href="<?php the_permalink(); ?>"><h3 class="post-title"><?php the_title(); ?></h3></a>
            <!-- 文章元信息:作者 + 日期 -->
            <div class="post-meta">
              <a href="<?php echo esc_url(get_author_posts_url(get_the_author_meta('ID'))); ?>">
                <span class="author"><i class="ion-android-person"></i><?php the_author(); ?></span>
              </a>
              <!-- 新增日期显示 -->
              <span class="post-date"><i class="ion-calendar"></i><?php the_date(); ?></span>
            </div>
            <!-- 可选:添加文章摘要提升体验 -->
            <p class="post-excerpt"><?php the_excerpt(); ?></p>
          </div>
        </div>
    <?php } 
    } ?>
  </div>
</div>
<?php get_footer(); ?>

日期显示的几种常用场景方案

  • 默认格式(同日期文章仅显示一次):<?php the_date(); ?>会直接输出WordPress后台设置的日期格式(路径:设置→常规→日期格式),适合列表页,同一天发布的多篇文章只会显示一次日期。
  • 每篇强制显示日期:如果需要不管日期是否重复,每篇文章都显示日期,改用<?php the_time(); ?>,格式和后台设置一致。
  • 自定义日期格式:如果想自定义显示格式(比如2024-05-20 14:30),可以用:
    <!-- 直接输出自定义格式 -->
    <?php the_date('Y-m-d H:i'); ?>
    <!-- 或者获取字符串后再做额外处理 -->
    <?php echo get_the_date('Y-m-d H:i'); ?>
    

额外排查点

如果添加后还是不显示日期,检查下这两个地方:

  1. 后台是否禁用了文章日期的显示设置;
  2. 主题是否有独立的内容模板(比如content.php),可能需要在对应模板文件里添加日期调用逻辑。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.21 06:50:47