WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Determining whether a taxonomy is top-level or not (3 posts)

  1. gleenk
    Member
    Posted 1 year ago #

    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
  2. vtxyzzy
    Member
    Posted 1 year ago #

    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.

  3. gleenk
    Member
    Posted 1 year ago #

    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

Topic Closed

This topic has been closed to new replies.

About this Topic