Retrieve Slug of Current Taxonomy Term on Archive Page

<?php $tax_slug = get_query_var( 'taxonomy_category' ); ?>

Comes in handy if you’re working with custom post types, or external plugin archive pages.

I’ve used this with Tribe Events Calendar to create custom menus to their category pages as follows:

<?php $tax_slug = get_query_var( 'tribe_events_cat' ); ?>

Setup menu:

<ul class="event_cats">
	<?php $args = array(
		'taxonomy' => 'tribe_events_cat',
		'orderby' => 'name',
		'order' => 'asc',
		'hide_empty' => 1,
	);
	
	$directory_cats = get_categories($args); 
	
	usort($directory_cats, function($a, $b) {
	   return get_field("order", "category_".$a->term_id) - get_field("order", "category_".$b->term_id);
	});
	
	foreach($directory_cats as $category) : setup_postdata($category); 
		$current = $category->slug; ?>
	
		<li class="<?php if($current === $tax_slug) { echo 'active'; } ?>"><a href="<?php bloginfo('url'); ?>/events/category/<?php echo $category->slug; ?>"><?php echo $category->name; ?></a></li>
	
	<?php endforeach; wp_reset_postdata(); ?>
</ul><!-- .event_cats -->

 

Leave a Reply

katherine as a flat graphic icon

About Me

I’m an African / Ojibwe First Nations Web Developer living in Winnipeg, Manitoba.

Visit the Tips and Blog to see what I’m working on.