在 Typecho 中,获取文章的元信息(Meta Information)是构建动态和丰富内容的关键部分。Typecho 提供了多种方法来访问这些信息,下面是一些常见的文章元信息及其获取方式。
常见的文章元信息
假设你正在 post.php 或者其他相关模板文件中操作,可以通过 $this 对象访问当前文章的信息。
✅ 文章的基本信息
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
|
echo $this->title;
echo $this->permalink;
echo $this->content;
echo $this->excerpt(100, '...');
echo date('Y-m-d', $this->created);
echo date('Y-m-d', $this->modified);
echo $this->author;
$this->category(',', true, '未分类');
|
✅ 获取标签
如前面所述,你可以使用以下代码来输出文章的标签:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| if ($this->tags):
foreach ($this->tags as $tag):
echo '<a href="' . $tag['permalink'] . '">' . $tag['name'] . '</a>';
endforeach;
else:
echo '这篇文章暂无标签。';
endif;
|
✅ 获取评论数
1 2 3
| echo $this->commentsNum;
|
或者如果你想显示“条评论”的话:
1 2 3 4 5
| $commentsNum = $this->commentsNum;
echo $commentsNum == 0 ? '暂无评论' : $commentsNum . ' 条评论';
|
✅ 获取自定义字段
如果你为文章设置了自定义字段,可以这样获取:
1 2 3
| $value = $this->fields->customFieldName;
|
如果不确定字段是否存在,可以先检查:
1 2 3 4 5 6 7 8 9 10 11
| if (isset($this->fields->customFieldName)) {
$value = $this->fields->customFieldName;
} else {
$value = '默认值';
}
|
📌 其他有用的方法
1 2 3 4 5
| $this->thePrev();
$this->theNext();
|
1 2 3 4 5 6 7
| if ($this->isTop):
echo '这是置顶文章';
endif;
|