• Resolved gleenk

    (@gleenk)


    How can I check, into taxonomy-term.php, if I am in a top level taxonomy term or not?

    if(top level term)
        show description
        show list of child terms
    else
        show description
        list posts within this terms
Viewing 2 replies - 1 through 2 (of 2 total)
  • I cannot completely answer your question, but perhaps this will help.

    If you have the term slug, you can retrieve the term info like this:

    $term = get_term_by('slug', 'sold', 'home-status');
    if ( $term->parent == 0 ) {
       // This is a top level
    } else {
       // This is a child
    }

    where the field to compare is ‘slug’, the slug is ‘sold’, and the taxonomy is ‘home-status’.

    You can also use the term id or name by supplying the proper field value.

    Thread Starter gleenk

    (@gleenk)

    Hi, no this is not my problem. I need to check terms dynamically. I solved in this way.

    Into functions.php:

    function get_top_parent_terms( $tassonomia, $termine ){
    
    	$topTerms = get_terms( $tassonomia, array('parent' => 0) );
    	$termini = array();
    	if ( $topTerms ) :
    	foreach ( $topTerms as $typeTerm ) :
    
    		$termini[] =  $typeTerm->term_id;
    
    	endforeach;
    	endif;
    
        return in_array( $termine->term_id, $termini);
    
     }

    Into a taxonomy-name.php:

    $term = $wp_query->queried_object;
    if ( !get_top_parent_terms ( 'categorie-servizi', $term ) ) :

    And this works. Hope that helps

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Determining whether a taxonomy is top-level or not’ is closed to new replies.