最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Wordpress Custom Taxonomy - If not parent term

programmeradmin1浏览0评论

I have a custom taxonomy with two levels of terms.

  • Parent term
    • Child term
    • Child term
  • Parent term
    • Child term
    • Child term
  • Parent term (no children)

I'm using a custom archive.php template with some HTML that I only want to display on terms without child terms.

Here's what I've tried...

$taxonomy = 'custom_tax';
$term = get_queried_object();

$children = get_terms( $term->taxonomy, array( 'parent' => $term->term_id ) );

if(!$children) {
    echo '<p>HTML only terms without child terms</p>';
}

This works if it's a Child term but not on a Parent term with no children!

Any help please?

I have a custom taxonomy with two levels of terms.

  • Parent term
    • Child term
    • Child term
  • Parent term
    • Child term
    • Child term
  • Parent term (no children)

I'm using a custom archive.php template with some HTML that I only want to display on terms without child terms.

Here's what I've tried...

$taxonomy = 'custom_tax';
$term = get_queried_object();

$children = get_terms( $term->taxonomy, array( 'parent' => $term->term_id ) );

if(!$children) {
    echo '<p>HTML only terms without child terms</p>';
}

This works if it's a Child term but not on a Parent term with no children!

Any help please?

Share Improve this question asked Oct 23, 2019 at 6:42 ob80ob80 11 bronze badge
Add a comment  | 

1 Answer 1

Reset to default 0

You can use get_term_children for checking there is have any child term or not.

$taxonomy = 'custom_tax';
$term = get_queried_object();
if ( count( get_term_children( $term->term_id, $taxonomy ) ) === 0 ) {
     echo '<p>HTML only terms without child terms</p>';
}
else
{
    echo '<p>HTML only terms with child terms</p>';
}
发布评论

评论列表(0)

  1. 暂无评论