如果自定义分类法模板上有帖子,则显示子术语



我有一段代码,显示了taxonomy-term.php模板文件中当前父术语的自定义分类法的子术语。我想只在存在子项时显示html。现在,即使没有子项,也会显示标题2。下面是代码:

<?php
$term_children = get_terms('onderwerp', array( 'parent' => get_queried_object_id(),));
if ( ! is_wp_error( $terms ) || ($term_children->count != 0)  ) { ?>
<section>
<h2>Ik heb een vraag over:</h2>
<div class="onderwerp-links">
<?php
foreach ( $term_children as $child ) { ?>
<?php $img = get_term_meta( $term->term_id, 'ndftm_img', true ); ?>
<a href="<?php echo esc_url( get_term_link( $child ) ) ?>" style="background-image: url(<?php echo $img; ?>)">
<span><?php echo $child->name; ?></span>
</a><?php
}
?>
</div>
</section>
<?php
} 
?>

我添加了

'hide_empty' => true

get terms数组 中的

并使if语句像这样:

if ( ! empty($term_children) )

代码如下:

<?php
$term_children = get_terms('onderwerp', array( 'parent' => get_queried_object_id(), 'hide_empty' => true ));
if ( ! empty($term_children) ) { ?>
<section>
<h2>Ik heb een vraag over:</h2>
<div class="onderwerp-links">
<?php
foreach ( $term_children as $child ) { ?>
<?php $img = get_term_meta( $term->term_id, 'ndftm_img', true ); ?>
<a href="<?php echo esc_url( get_term_link( $child ) ) ?>" style="background-image: url(<?php echo $img; ?>)">
<span><?php echo $child->name; ?></span>
</a><?php
}
?>
</div>
</section>
<?php
} 
?>

最新更新