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); // 将当前分类设为上级分类(往上爬)
}
return $this_category->term_id; // 返回根分类的id号
}
然后在需要调入子分类的分类和文章页面加入调用代码:
<?php
if(is_single()||is_category())
{
if(get_category_children(get_category_root_id(the_category_ID(false)))!= "" )
{
echo '<ul>';
echo wp_list_categories("child_of=".get_category_root_id(the_category_ID(false)). "&depth=0&hide_empty=0&title_li=&orderby=id&order=ASC");
echo '</ul>';
}
}
?>
这个函数就是在分类页面和文章页面中显示二级分类。
需要调用子分类图片可以用到下面代码配合插件:Categories Images
<?php
$categories=get_categories("child_of=".get_category_root_id(the_category_ID(false))."");
foreach($categories as $category) {
if (function_exists('z_taxonomy_image_url'))
echo z_taxonomy_image_url();
echo '<div class="swiper-slide">
<a href="'.get_category_link( $category->term_id ).'">
<div class="m-pic-bg">
<img src="'.z_taxonomy_image_url( $category->term_id ).'" alt="'.$category->name.'">
<div class="m-bg"></div>
<div class="swi-title">'.$category->name.'</div>
</div></a>
</div>';
}
?>
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END



















