How to catch the grandfather of a term in taxonomy in Wordpress?

Asked

Viewed 208 times

1

How to catch the grandfather of a term in taxonomy in Wordpress? Consider the following tree:

-Cidade
  -Farmacias
     -farmacia central
     -farmacia São José

I want to take the name of the city from the central pharmacy, for example.

  • 1

    You want to pick up posts from this category, that’s it?

  • I need to get the name of the town that the "such" pharmacy belongs to.

1 answer

2


You can do something similar, which I took from a reply Stackexchange’s own Wordpress:

// determine the topmost parent of a term
function get_term_top_most_parent($term_id, $taxonomy){
    // começa do atual
    $parent  = get_term_by( 'id', $term_id, $taxonomy);
    // subir a árvore até encontrar um termo com parent = '0'
    while ($parent->parent != '0'){
        $term_id = $parent->parent;

        $parent  = get_term_by( 'id', $term_id, $taxonomy);
    }
    return $parent;
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.