下面这段是需要放入WordPress函数文件的
<?php //获取当前分类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号 } ?>
下面是放入主题模板里面的
<?php $args=array( 'orderby' => 'name', 'child_of'=> get_category_root_id($cat), 'order' => 'ASC' ); $categories=get_categories($args); foreach($categories as $category) { $two_class_parent_id = get_category($category->term_id)->parent; $three_class_parent_id = get_category($two_class_parent_id)->parent; if(!$three_class_parent_id){ echo '<li class="catego"><a href="' . get_category_link( $category->term_id ) . '" title="' .get_option( 'cat-title-'.$term_id ).'' . sprintf( __( "" ), $category->name ) . '" ' . ' class="cat-1">' . $category->name.'</a><div class="hr"></div>'; }else{ echo '<span class="cat-2"><a href="' . get_category_link( $category->term_id ) . '" title="' .get_option( 'cat-title-'.$term_id ).'' . sprintf( __( "" ), $category->name ) . '" ' . '>' . $category->name.'</a></span>'; } }'</li>' ?>
效果是