我试图动态地针对如下设置的不同级别的分类法:
我的目标是第一级和第二级,就像这样:
<?php $parent = get_queried_object()->parent;
if($parent == "0"){ ?>
Top level
<?php } else { ?>
Second level
<?php } ?>我怎样才能瞄准第三级呢?
<?php $term_id = get_queried_object()->term_id;
$ancestors = get_ancestors( $term_id, 'categories' ); ?>
<pre>
<?php print_r($ancestors); ?>
</pre>当在类别页上有上面的代码时.顶层显示数组为空,第二级显示父级的id,第三级显示两个级别的数组。我怎样才能针对每一个个人水平?
发布于 2018-08-23 09:49:01
尝试使用get_ancestors() WP函数:祖先()
function get_tax_level($id, $tax){
$ancestors = get_ancestors($id, $tax);
return count($ancestors)+1;
}
$current_term_level = get_tax_level(get_queried_object()->term_id, get_queried_object()->taxonomy);
if ($current_term_level = 0) {
// show first drop-down
} else if ($current_term_level = 1) {
// show second drop-down
} else {
// show third drop-down
}
https://stackoverflow.com/questions/51982571
复制相似问题