Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wordpress, get current level of taxonomy in an archive page

I'm creating a WordPress site to display a catalogue of items using a custom post type and a custom hierarchical taxonomy. I'd like to keep it simple and deal with the items in a single archive page, but I need help how to determine the level of taxonomy currently displayed. Basically, I need the following functionality:

if ($current_term_level = 0) {
    // show first drop-down
} else if ($current_term_level = 1) {
    // show second drop-down
} else {
    // show third drop-down
}

Can someone please explain how to get $current_term_level to output appropriate values?

like image 922
kamontander Avatar asked Mar 10 '14 17:03

kamontander


People also ask

How do I find the category of an archive page in WordPress?

You will need to go to your website's /wp-content/themes/parent-theme/ folder and find the category. php file. If your file doesn't include that file, then you will need to find archive.

Is taxonomy an archive page?

By default, WordPress creates archives which list all of your posts in reverse order. If users are looking for posts in a certain category or with a given taxonomy term, they'll have to visit the archive page for that category or term.

How do I find custom taxonomy value in WordPress?

In WordPress, you can create (or “register”) a new taxonomy by using the register_taxonomy() function. Each taxonomy option is documented in detail in the WordPress Codex. After adding this to your theme's functions. php file, you should see a new taxonomy under the “Posts” menu in the admin sidebar.


Video Answer


1 Answers

Try with get_ancestors() WP function :

function get_tax_level($id, $tax){
    $ancestors = get_ancestors($id, $tax);
    return count($ancestors)+1;
}

$current_term_level = get_tax_level(get_queried_object()->term_id, get_queried_object()->taxonomy);

if ($current_term_level = 0) {
    // show first drop-down
} else if ($current_term_level = 1) {
    // show second drop-down
} else {
    // show third drop-down
}
like image 128
William Ode Avatar answered Oct 29 '22 14:10

William Ode