codebloo
codebloo

Reputation: 85

How can I check if a term has a parent and display that in addition to the current term on taxonomy.php?

I would like to display the following on my taxonomy.php page

If current term has parent
    <h2>Archive ParentTerm</h2>
    <h3><?php single_term_title(); ?></h3>

//else
<h2>Archive <?php single_term_title(); ?></h2>

but I cannot for the life of me figure out how to get the parent term on a taxonomy.php archive page (using the custom taxonomy "types")

thanks.

Upvotes: 2

Views: 12052

Answers (1)

user4055330
user4055330

Reputation:

You can try this:

<?php
$term = get_queried_object();
$parent = ( isset( $term->parent ) ) ? get_term_by( 'id', $term->parent, 'types' ) : false;
?>

<?php if( $parent ): ?>
    <h2>Archive ParentTerm</h2>
    <h3><?php echo $parent->name; ?></h3>
<?php else:?>
    <h2>Archive <?php single_term_title(); ?></h2>
<?php endif; ?>

Upvotes: 7

Related Questions