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?
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.
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.
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.
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
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With