需求

在分类页面下,显示当前分类的子分类带超链接的名字,点击名字,进入子分类页面后,继续显示子子分类带超链接的名字...,需求就是三级分类,但实际上typecho貌似可以无限套娃,所以需要写个一劳永逸的方法。

原理

在分类页面下获取当前分类的身份证号(mid),然后根据身份证号(mid)找儿子...,但是typecho获取当前分类的身份证号(mid)有点问题,只能获取到文章用的分类的mid,所以改为获取缩略名,然后通过缩略名查到(mid),进而在通过mid找儿子

实操

functions.php中加入如下代码

class Widget_Post_cat extends Widget_Abstract_Metas
{
    public function __construct($request, $response, $params = NULL)
    {
        parent::__construct($request, $response, $params);
        $this->parameter->setDefault(array('pageSize' => $this->options->commentsListSize, 'parentId' => 0, 'ignoreAuthor' => false));
    }
    public function execute()
    {
$db= Typecho_Db::get();
$prefix = $db->getPrefix();
$po  = $this->select()->from($prefix.'metas')
->where('table.metas.slug = ?',$this->parameter->slug)
->where('table.metas.type = ?','category')
->order('table.metas.mid', Typecho_Db::SORT_DESC);
$po = $this->db->fetchRow($po);//根据缩略名找到该行
$select  = $this->select()->from($prefix.'metas')
->where('table.metas.parent = ?',$po['mid'])//从所有分类中找到爸爸是这个mid的
->where('table.metas.type = ?','category')
->order('table.metas.mid', Typecho_Db::SORT_DESC);
$this->db->fetchAll($select, array($this, 'push'));
    }
}

archive.php中加入如下代码

<div style="background: red;color: #fff;display: block;"><h2>分类列表</h2>
<?php if($this->is("category")): ?>
<?php 
$this->widget('Widget_Post_cat@cat', 'slug='.$this->getArchiveSlug())->to($cat); ?>
<?php while($cat->next()): ?><br>
<a href="<?php $cat->permalink() ?>"><?php $cat->name(); ?></a><br>
 <?php endwhile; ?>
<?php endif; ?>
</div>
版权属于:泽泽社长
本文链接:https://blog.zezeshe.com/archives/typecho-fenleitaowa.html
本站未注明转载的文章均为原创,并采用 CC BY-NC-SA 4.0 授权协议,转载请注明来源,谢谢!