需求
在分类页面下,显示当前分类的子分类带超链接的名字,点击名字,进入子分类页面后,继续显示子子分类带超链接的名字...,需求就是三级分类,但实际上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 授权协议,转载请注明来源,谢谢!
能让后台也一次显示所有的分类吗?
这个在子类能显示父类名称和链接吗
没有,这个只是显示当前分类下的子分类的链接和名字!不过你应该可以自己写输出当前分类名字和链接
其实我原来也写过一个,就是爸爸找儿子规则hhhhhhh
其实用途好像并不大,只是觉得挺有意思的
我用来做特定分类检索下级了,挺有用的,做自定义导航
我也是写这个的时候才知道typecho可以无限套娃的
会员机制可以的!
昨天安装的插件哈哈
付费知识
哈哈哈,毕竟别人花50块钱让我写的,免费发出来不太好