在分类模板加入:

have()) { while ($this->next()) { foreach ($this->tags as $tag) { $tagName = $tag['name']; if (!isset($tagList[$tagName])) { $tagList[$tagName] = array( 'permalink' => $tag['permalink'], 'slug' => $tag['slug'], 'count' => 1 ); } else { $tagList[$tagName]['count']++; } } } $this->rewind(); } if (!empty($tagList)): // 按标签使用次数排序 uasort($tagList, function($a, $b) { return $b['count'] - $a['count']; }); ?> 音乐标签: $tagInfo): // 关键修改:给链接添加 from=music 参数 $link = $tagInfo['permalink']; if (strpos($link, '?') !== false) { $link .= '&from=music'; } else { $link .= '?from=music'; } ?> ()

步骤2:创建音乐标签模板
文件: /usr/themes/default/tag-music.php

步骤3:创建主标签模板(路由器)
文件: /usr/themes/default/tag.php

<?php
/**

  • 标签页主模板 - 根据参数路由到不同模板
    */

if (!defined('__TYPECHO_ROOT_DIR__')) exit;

// 方法1:检查URL参数
$fromMusic = isset($_GET['from']) && $_GET['from'] == 'music';

// 方法2:检查HTTP_REFERER(备用方案)
if (!$fromMusic && isset($_SERVER['HTTP_REFERER'])) {

$referer = $_SERVER['HTTP_REFERER'];
// 检查是否来自音乐分类页面
if (strpos($referer, '/category/ds-music') !== false || 
    strpos($referer, 'from=music') !== false) {
    $fromMusic = true;
}

}

// 方法3:检查当前标签是否属于音乐分类(增强判断)
if (!$fromMusic) {

$currentTagSlug = $this->request->tag;
$db = Typecho_Db::get();

// 获取音乐分类ID
$musicCat = $db->fetchRow($db->select('mid')->from('table.metas')
    ->where('slug = ?', 'ds-music')->where('type = ?', 'category'));

if ($musicCat) {
    // 检查当前标签是否有关联到音乐分类的文章
    $count = $db->fetchObject($db
        ->select('COUNT(*) as cnt')
        ->from('table.relationships r1')
        ->join('table.relationships r2', 'r1.cid = r2.cid')
        ->where('r1.mid = (SELECT mid FROM table.metas WHERE slug = ? AND type = ?)', $currentTagSlug, 'tag')
        ->where('r2.mid = ?', $musicCat['mid']))->cnt;
    
    if ($count > 0) {
        $fromMusic = true;
    }
}

}

// 根据判断结果加载不同的模板
if ($fromMusic && file_exists(__DIR__ . '/tag-music.php')) {

// 加载音乐标签模板
include __DIR__ . '/tag-music.php';

} else {

// 加载普通标签模板
$this->need('header.php');
?>

<div class="col-mb-12 col-8" id="main" role="main">
    <!-- 普通标签导航 -->
    <div class="tag-navigation">
        <?php
        $currentTagSlug = $this->request->tag;
        $db = Typecho_Db::get();
        
        // 获取所有热门标签
        $allTags = $db->fetchAll($db->select('*')->from('table.metas')
            ->where('type = ?', 'tag')
            ->order('count', Typecho_Db::SORT_DESC)
            ->limit(30));
        
        if ($allTags):
        ?>
        <div class="simple-subcats">
            <span class="label">热门标签:</span>
            <?php foreach ($allTags as $tag): 
                $permalink = Typecho_Common::url('tag/' . $tag['slug'], $this->options->index);
                $isCurrent = ($currentTagSlug == $tag['slug']) ? 'current' : '';
            ?>
                <a href="<?php echo $permalink; ?>" class="subcat-link <?php echo $isCurrent; ?>">
                    <?php echo $tag['name']; ?>
                    <span class="count">(<?php echo $tag['count']; ?>)</span>
                </a>
            <?php endforeach; ?>
        </div>
        <?php endif; ?>
    </div>
    
    <!-- 普通标签标题 -->
    <h3 class="archive-title"><?php $this->archiveTitle([
            'tag' => _t('标签 %s 下的文章'),
        ], '', ''); ?></h3>
    
    <!-- 普通文章列表 -->
    <?php if ($this->have()): ?>
        <?php while ($this->next()): ?>
            <article class="post" itemscope itemtype="http://schema.org/BlogPosting">
                <h2 class="post-title" itemprop="name headline">
                    <a itemprop="url" href="<?php $this->permalink() ?>"><?php $this->title() ?></a>
                </h2>
                <ul class="post-meta">
                    <li><?php _e('时间: '); ?>
                        <time datetime="<?php $this->date('c'); ?>"><?php $this->date(); ?></time>
                    </li>
                    <li><?php _e('分类: '); ?><?php $this->category(','); ?></li>
                </ul>
                <div class="post-content" itemprop="articleBody">
                    <?php $this->excerpt(200, '...'); ?>
                </div>
            </article>
        <?php endwhile; ?>
    <?php else: ?>
        <article class="post">
            <h2 class="post-title"><?php _e('没有找到内容'); ?></h2>
        </article>
    <?php endif; ?>

    <?php $this->pageNav('&laquo; 前一页', '后一页 &raquo;'); ?>
</div>

<?php
$this->need('sidebar.php');
$this->need('footer.php');

}
?>

测试2:直接访问标签页
直接访问标签页:http://你的网站/tag/标签名称

检查是否显示普通标签模板

测试3:从其他地方点击标签
从首页或其他分类页面点击标签

检查是否显示普通标签模板

标签: none

添加新评论