新手求教
新建的自定义分类叫产品,products
,模板taxonomy-products.php
它的下面有子分类产品一,产品二
我在网上找的代码
1.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号
}
2.模板里用
<?php
if(is_single()||is_category())
{
if(get_category_children(get_category_root_id(the_category_ID(false)))!="" )
{
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");
}
}
?>
用了但是没有调用出来。是不是因为自定义分类的原因。
怎么在模板中调用这个产品的子分类产品一,产品二。产品模型都是一样的,用的模板也是一样的
我在官网找到一个方法,这种定死了term id。有没有好的方法不定term id
<?php
$term_id = 4;
$taxonomy_name = 'products';
$term_children = get_term_children( $term_id, $taxonomy_name );
foreach ( $term_children as $child ) {
$term = get_term_by( 'id', $child, $taxonomy_name );
echo '<li><a href="' . get_term_link( $child, $taxonomy_name ) . '">' . $term->name . '</a></li>';
}
?>