在 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; // 替换 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;

  • 获取文章状态
1
2
3

echo $this->status; // draft, published 等