好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

wordpress不同分类调用当前子分类 - WordPress

wordpress不同分类调用当前子分类

wordpress不同分类调用当前子分类,这种需求在一些企业网站中经常见到,例如进入产品中心目录希望侧边栏显示产品中心目录下面的子分类,实现这种效果,我们需要通过两步骤实现:

1、获取当前目录的ID或别名;

2、使用某个分类下面的子分类标签完成调用。

以下是实现方法:

1、获取当前目录的ID;你需要在函数文件functions.php中添加以下获取当前分类目录ID的代码:

//获取当前分类ID   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号     } 

2、以上代码返回的当前目录的ID为:get_category_root_id($cat),下一步我们只需要通过wp_list_cats标签在参数中添加child_of的值为 get_category_root_id($cat) 就可以了,所以调用标签如下:

<?php wp_list_cats( 'child_of='  . get_category_root_id( $cat ) .  '&depth=1&hide_empty=0&hierarchical=1&optioncount=1' );?> 

查看更多关于wordpress不同分类调用当前子分类 - WordPress的详细内容...

  阅读:48次