Typecho 的文章页(post) 中,如果你想输出当前文章的 标签(Tags),可以使用 Typecho 提供的模板变量 $this->tags 来实现。


✅ 标准方法:在 post.php 中输出当前文章的标签

以下是一个完整、安全且美观的写法,适用于你的主题文件 post.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

<?php if ($this->tags): ?>

<div class="post-tags">

<?php foreach ($this->tags as $tag): ?>

<a href="<?php echo $tag['permalink']; ?>" rel="tag"><?php echo $tag['name']; ?></a>

<?php endforeach; ?>

</div>

<?php else: ?>

<p class="no-tags">这篇文章暂无标签。</p>

<?php endif; ?>


🧠 输出说明:

  • $this->tags:返回一个数组,包含当前文章的所有标签。

  • 每个元素是包含 'name''permalink' 的关联数组。

  • 使用 <a> 标签将每个标签显示为可点击的链接。

  • 如果没有标签,会显示提示信息。


💡 只输出标签名称(逗号分隔)

如果你只想输出纯文本的标签名,并用逗号分隔:

1
2
3
4
5
6
7
8
9
10
11
12
13

<?php

$tagNames = array_map(function($tag) {

return $tag['name'];

}, $this->tags);

echo implode(', ', $tagNames);

?>

输出效果类似:

1
2
3

标签1, 标签2, 标签3


🔒 安全建议:

  • 始终使用 if ($this->tags) 判断是否存在标签,防止空指针错误。

  • 不要直接输出用户输入内容,如需自定义字段请做适当过滤。


📌 示例样式(CSS 推荐):

你可以加上一些简单的 CSS 美化标签样式:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

.post-tags a {

display: inline-block;

padding: 4px 8px;

margin: 0 5px 5px 0;

background-color: #f0f0f0;

border-radius: 3px;

font-size: 14px;

color: #333;

text-decoration: none;

}