wordpress分类页显示父子分类

ė 81 6 0
wordpress分类页显示父子分类

wordpress分类页输出父子分类链接效果

看到知更鸟博客在wordpress分类列表中有子分类的链接,然后根本它提供的 “获取当前分类子分类”只得到子分类,并没有有父分类。在网上找了一大堆不是只输出父分类的链接,就是只输出父分类的名称。后来还是找到方法了。

//以下代码放入functions.php

//获取当前分类子分类
function get_category_root_id($cat) {
// 取得当前分类
$this_category = get_category($cat);
// 若当前分类有上级分类时循环
while($this_category->category_parent) {
// 将当前分类设为上级分类
$this_category = get_category($this_category->category_parent);
}
// 返回根分类的id号
return $this_category->term_id;
}

//获取子分类的父分类
function get_category_cat() {
$catID = get_query_var('cat'); // 当前分类ID
$thisCat = get_category($catID);
$parentCat = get_category($thisCat->parent);
// 输出父分类的链接
echo '<a href="'.get_category_link($parentCat-&gt;term_id).'">'.get_cat_name($parentCat-&gt;term_id).'</a>';
}

在需要输出的分类页模板中输出代码

<div class="cotegorylist"><ul><li class="cat-item"><?php echo get_category_cat();?><?php //echo get_category_parents( $cat, true, ' &raquo; ' ); ?></li>
<?php wp_list_categories('child_of=' . get_category_root_id($cat) . '&depth=1&hide_empty=0&current_category=0&$parent_category=1&hierarchical=1&optioncount=1&title_li=');?></ul></div>

<style>
/*css*/
.cotegorylist ul {
display: flex;
justify-content: center;
flex-wrap: wrap;
margin: 5px 0 7px;
padding: 2px 0 0;
}
.cotegorylist .cat-item {
background: #fff;
width: auto;
font-size: 14px;
font-size: 1.4rem;
color: #444;
text-align: center;
line-height: 180%;
display: inline-block;
cursor: pointer;
margin: 0 4px 8px;
padding: 8px 16px;
border-radius: 5px;
box-shadow: 0 0 0 1px rgb(68 68 68 / 5%);
}

</style>

  • 灵魂发问:谁告诉你我国防空洞位置了吗?怕是99%中国人不知道,我也不知道,为什么,因为没告诉咱们啊!怕是很多当官的都不晓得吧!所以别老想着打这个那个,真打起来,你躲都不知道躲哪?最好的位置是到没有军事区,机场等落后位置,因为人家炸这些位置不仅没有利益还会被国际谴责。 --- 2025/03/08
主机推荐
:网友留言区仅供网友表达其个人观点,并不表明栖息邦最终立场。



管理员登录
注册

提示:点击上下面区域关闭

字号调整

View My Stats
空位 空位
本次加载耗时:1.417s