一般我们在自己制作或者选择的其他人Typecho主题的时候,都会默认单篇文章会有调用单篇文章的标签Tag,且可能在侧栏或者底部会调用部分热门的Tag。不过我们有些朋友希望需要将所有的Tags聚合到一个页面,这里就需要独立制作一个标签模板页面。

<?php

/**
* 全部标签
*
* @package custom
*/

//代码
?>
<?php if (!defined('__TYPECHO_ROOT_DIR__')) exit; ?>
<?php $this->need('component/header.php'); ?>

<!-- aside -->
<?php $this->need('component/aside.php'); ?>
<!-- / aside -->

      <h1 class="m-n font-thin text-black l-h"><?php _me("全部标签") ?></h1>
      <small class="text-muted letterspacing indexWords"><?php $this->options->description() ?></small>
      </header>

        <h2 id="tag-cloud-title" class="widget-title m-t-none text-md"><?php _me("标签云") ?></h2>
        <?php Typecho_Widget::widget('Widget_Metas_Tag_Cloud','ignoreZeroCount=1&limit=150')->to($tags); ?>
        <?php if($tags->have()): ?>
            <?php while ($tags->next()): ?>
            <span id="tag-clould-color"  style="background-color:rgb(<?php echo(rand(0,255)); ?>,<?php echo(rand(0,255)); ?>,
                <?php echo(rand(0,255)); ?>)">
                <a  href="<?php $tags->permalink();?>" target="_blank">
                <?php $tags->name(); ?></a>
            </span>
           <?php endwhile; ?>
    <div style="margin: 71px;">
    </div>
    <?php endif; ?>


<!-- footer -->
<?php $this->need('component/footer.php'); ?>
  <!-- / footer -->


这里我们直接在模板目录创建一个tags.php模板页面丢进去,然后在后台新建页面,选择 tags.php 作为模板页面就可以。以上是参考:https://www.moewah.com/archives/3397.html 文章中他基于一款主题的,如果我们自己需要需要修改样式。
或者我们可以参考:
<?php

    $db = Typecho_Db::get();
    $options = Typecho_Widget::widget('Widget_Options');
    $tags= $db->fetchAll($db->select()->from('table.metas')
            ->where('table.metas.type = ?', 'tag')
            ->order('table.metas.order', Typecho_Db::SORT_DESC));
    foreach($tags AS $tag) {
        $type = $tag['type'];
        $routeExists = (NULL != Typecho_Router::get($type));
        $tag['pathinfo'] = $routeExists ? Typecho_Router::url($type, $tag) : '#';
        $tag['permalink'] = Typecho_Common::url($tag['pathinfo'], $options->index);
        echo "<a href="".$tag['permalink']."\"&gt;".$tag['name']."</a> ";
    }
?>    


这个是调用所有TAG的代码,我们可以单独设置到标签页面然后新创建页面作为模板使用。


扫描二维码,在手机上阅读!