文章页标题下面,通常有一行“作者:某某 | 分类:某某 | 时间:某年某月”。这些都是模板标签输出的:
| 要输出 | 用这个函数 | 例子 |
|---|---|---|
| 分类 | the_category() |
the_category(', ') |
| 标签 | the_tags() |
the_tags('标签:', ',', '') |
| 作者 | the_author() |
the_author() |
| 发布时间 | the_time() |
the_time('Y-m-d') |
| 修改时间 | the_modified_time() |
the_modified_time('Y-m-d') |
<?php the_category( ', ' ); ?>
参数是“多个分类之间的分隔符”。一篇文章属于多个分类时,会输出 分类A, 分类B。
如果想输出成“不带链接的纯文本”,用 get_the_category() 拿数组,自己拼。
<?php the_tags( '标签:', ',', '' ); ?>
三个参数:
注意:文章没有标签时,the_tags() 什么都不输出——连“标签:”这三个字也不输出。所以外面不用再判断。
如果想判断有没有标签再决定要不要显示某个区块,用 has_tag():
<?php if ( has_tag() ) : ?>
<div><?php the_tags( '标签:', ',', '' ); ?></div>
<?php endif; ?>
<?php the_author(); ?>
输出显示名。想输出作者的更多信息(头像、简介),用 get_the_author_meta():
<?php echo get_the_author_meta( 'description' ); ?>
<?php the_time( 'Y-m-d' ); ?>
参数是 PHP 的日期格式:
Y-m-d → 2026-11-06Y年m月d日 → 2026年11月06日H:i → 14:30想做“几小时前”这种相对时间,WordPress 没有内置函数,得自己写或者用插件。
看它的 single.php:
<span>分类: <?php the_category(',') ?> </span>
<span>发布时间: <?php the_time('Y-m-d H:i') ?> </span>
<span><?php edit_post_link('编辑'); ?></span>
这几行都在循环外——但它们能正常显示,是因为前面那个“作者 while 循环”刚好把 $post 设好了(第二卷讲过这个坑)。
这次重构,我把它们全挪进了主循环里,逻辑就直了。
文章页最好同时显示“发布时间”和“修改时间”。搜索引擎(尤其百度)对“最近更新”比较敏感,显示修改时间有助于判断内容新鲜度。
<span>发布于 <?php the_time('Y-m-d') ?></span>
<span>更新于 <?php the_modified_time('Y-m-d') ?></span>
但注意:如果修改时间和发布时间一样(从没改过),显示两个一样的日期有点傻。可以判断一下:
<?php if ( get_the_time('Ymd') !== get_the_modified_time('Ymd') ) : ?>
<span>更新于 <?php the_modified_time('Y-m-d') ?></span>
<?php endif; ?>
一句话记住:分类 the_category、标签 the_tags、作者 the_author、时间 the_time——它们都读全局 $post,必须放在循环里。
卷三到这里结束。你已经掌握了页面上的各种“零件”:head、导航、侧栏、搜索、评论、分页、面包屑、文章 meta。
卷四开始讲 functions.php 和钩子——主题的“功能层”。