Using WordPress ‘is_taxonomy_hierarchical()’ PHP function

The is_taxonomy_hierarchical() WordPress PHP function determines whether a taxonomy object is hierarchical.

Usage

is_taxonomy_hierarchical( $taxonomy );

Example:

$taxonomy = 'category';
$is_hierarchical = is_taxonomy_hierarchical( $taxonomy );
echo $is_hierarchical; // Output: true

Parameters

  • $taxonomy (string) (Required) – Name of the taxonomy object.

More information

See WordPress Developer Resources: is_taxonomy_hierarchical()

Examples

Check if a Custom Taxonomy is Hierarchical

$taxonomy = 'custom_taxonomy';
$is_hierarchical = is_taxonomy_hierarchical( $taxonomy );

if ( $is_hierarchical ) {
    echo 'This taxonomy is hierarchical.';
} else {
    echo 'This taxonomy is not hierarchical.';
}

Display Hierarchical Taxonomies

$taxonomies = get_taxonomies();

foreach ( $taxonomies as $taxonomy ) {
    if ( is_taxonomy_hierarchical( $taxonomy ) ) {
        echo $taxonomy . ' is hierarchical.<br>';
    }
}

Add Custom Styling for Hierarchical Taxonomies

$taxonomy = 'custom_taxonomy';
$style = '';

if ( is_taxonomy_hierarchical( $taxonomy ) ) {
    $style = 'hierarchical-style';
}

echo '<div class="' . $style . '">Taxonomy Content</div>';

Show Parent Terms Only for Hierarchical Taxonomies

$taxonomy = 'custom_taxonomy';

if ( is_taxonomy_hierarchical( $taxonomy ) ) {
    $terms = get_terms( array(
        'taxonomy'   => $taxonomy,
        'parent'     => 0,
    ) );

    foreach ( $terms as $term ) {
        echo $term->name . '<br>';
    }
}

Check if Post Type Supports Hierarchical Taxonomies

$post_type = 'post';
$taxonomies = get_object_taxonomies( $post_type );
$hierarchical_taxonomies = array();

foreach ( $taxonomies as $taxonomy ) {
    if ( is_taxonomy_hierarchical( $taxonomy ) ) {
        $hierarchical_taxonomies[] = $taxonomy;
    }
}

if ( !empty( $hierarchical_taxonomies ) ) {
    echo 'The ' . $post_type . ' post type supports the following hierarchical taxonomies:';
    echo implode( ', ', $hierarchical_taxonomies );
} else {
    echo 'The ' . $post_type . ' post type does not support any hierarchical taxonomies.';
}