阿里云服务器

今天给大家分享如何在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>';
 }
?>				


相关阅读:
  • wordpress编辑框不让span过滤的方法
  • wordpress搜索为空时仍然返回结果问题
  • wordpress注册页取消邮箱的方法
  • wordpress用户头像在线调用代码
  • wordpress自动隐藏email注册,直接用户名密码注册
  • wordpress设置游客只能访问指定页面的方法
  • wordpress列表分页显示404页面的解决办法
  • wordpress页面默认排序修改的方法
  • wordpress新版本5.4 “Adderley” 发布上线,更新多项功能
  • wordpress默认编辑器功能增加的方法