Typecho 分类调用标签的实现
- 作者: mylens
- 时间:
- 分类: 技术教程 Technical Tutorials
is('category')) {
$categorySlug = $this->getArchiveSlug();
// 通过slug获取分类ID
$category = $db->fetchRow($db->select('mid')
->from('table.metas')
->where('slug = ?', $categorySlug)
->where('type = ?', 'category')
->limit(1));
if ($category) {
$categoryId = $category['mid'];
}
}
echo "";
echo "";
if ($categoryId > 0) {
// 获取当前分类及所有子分类的ID
$categoryIdsQuery = $db->select('mid')
->from('table.metas')
->where('type = ?', 'category')
->where('parent = ? OR mid = ?', $categoryId, $categoryId);
$categoryRows = $db->fetchAll($categoryIdsQuery);
$categoryIds = array_column($categoryRows, 'mid');
echo "";
// 查询这些分类下的文章ID
$postIdsQuery = $db->select('table.relationships.cid')
->from('table.relationships')
->join('table.contents', 'table.relationships.cid = table.contents.cid')
->where('table.relationships.mid IN ?', $categoryIds)
->where('table.contents.status = ?', 'publish')
->where('table.contents.type = ?', 'post')
->group('table.relationships.cid'); // 去重
$postRows = $db->fetchAll($postIdsQuery);
$postIds = array_column($postRows, 'cid');
echo "";
// 查询标签
if (!empty($postIds)) {
$tags = $db->fetchAll($db->select('table.metas.mid', 'table.metas.name', 'table.metas.slug')
->from('table.metas')
->join('table.relationships', 'table.metas.mid = table.relationships.mid')
->where('table.relationships.cid IN ?', $postIds)
->where('table.metas.type = ?', 'tag')
->group('table.metas.mid')
->order('count(*)', \Typecho\Db::SORT_DESC));
echo "";
// 显示标签
if (!empty($tags)) {
echo '';
foreach ($tags as $tag) {
echo "";
// 修正标签URL,确保包含index.php
$tagUrl = $this->options->siteUrl . 'index.php/tag/' . $tag['slug'] . '/';
echo '- ' . $tag['name'] . '
';
}
echo '
';
} else {
echo '暂无标签';
}
} else {
echo '暂无标签';
}
} else {
echo '暂无标签';
}
?>
标签: Typecho