效果:

描述

普通的分类,正常输出;有子分类的父级分类,点击后它会展开它下面的子分类

源代码

<li id="menu-item-443" class="menu-item menu-item-type-custom menu-item-object-custom menu-item-has-children dropdown menu-item-443 nav-item">
<a title="潮流文摘" href="javascript:;" class="nav-link">
<span class="nav-icon">
<i class="text-xl iconfont icon-bookmark"></i> 
</span>
<span class="nav-text">潮流文摘</span>
</a>
<ul class="sub-menu" role="menu">
    <li id="menu-item-442" class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-442 nav-item">
    <a title="CES 现场报道" href="javascript:window.open('https://theme.nicetheme.xyz/lighthouse/ces-现场报道');" class="nav-link">
    <span class="nav-text">CES 现场报道</span>
    </a>
    </li>
</ul>
</li>

注意:【menu-item-has-children图中箭头所指样式,当只存在父级分类时这段代码是没有的。可以直接不写或者加一些判断来控制是否显示】

转换后的代码

<!-- 1 -->
<?php $this->widget('Widget_Metas_Category_List')->to($categorys); ?>
<?php while($categorys->next()): ?>
<?php if ($categorys->levels === 0): ?>
<?php $children = $categorys->getAllChildren($categorys->mid);$n++; ?>
<?php if (empty($children)) { ?>
<!-- end 1 -->

<li id="menu-item-<?php $categorys->mid(); ?>" class="menu-item menu-item-type-custom menu-item-object-custom  dropdown menu-item-<?php $categorys->mid(); ?> nav-item">
<a title="<?php $categorys->name(); ?>" href="<?php $categorys->permalink(); ?>" class="nav-link">
<span class="nav-icon">
<i class="text-xl iconfont icon-<?php $categorys->slug();?>">
</i> 
</span>
<span class="nav-text"><?php $categorys->name(); ?></span>
</a>
</li>

<!-- 2 -->
<?php } else { ?>
<!-- end 2 -->

<li id="menu-item-<?php $categorys->mid(); ?>" class="menu-item menu-item-type-custom menu-item-object-custom menu-item-has-children dropdown menu-item-<?php $categorys->mid(); ?> nav-item">
<a title="<?php $categorys->name(); ?>" href="javascript: void(0);" class="nav-link">
<span class="nav-icon">
<i class="text-xl iconfont icon-<?php $categorys->slug();?>">
</i> 
</span>
<span class="nav-text"><?php $categorys->name(); ?></span>
</a>
<ul class="sub-menu" role="menu">

<!-- 3 -->
<?php foreach ($children as $mid) { ?>
<?php $child = $categorys->getCategory($mid); ?>
<!-- end 3 -->

<li id="menu-item-<?php echo $child['mid']; ?>" class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-<?php echo $child['mid']; ?> nav-item">
<a title="<?php echo $child['name']; ?>" href="<?php echo $child['permalink'] ?>" class="nav-link">
<span class="nav-text"><?php echo $child['name']; ?></span>
</a>
</li>

<!-- 4 -->
<?php } ?>
<!-- end 4 -->

</ul>
</li>

<!-- 5 -->
<?php } ?><?php endif; ?><?php endwhile; ?>
<!-- end 5 -->

以上是我根据Typecho设置父分类和子分类不同样式的方法以及查阅资料总结的,方法取巧不一定适合所有情况只作为参考!

Last modification:June 25th, 2020 at 10:00 am
If you think my article is useful to you, please feel free to appreciate