If you’re working with WordPress and you want to display content from a specific category on a custom taxonomy template, you may be wondering how to get the slug of the current category. Fortunately, it’s relatively easy to do.
First, let’s define what we mean by “slug” and “taxonomy template”. In WordPress, a slug is a unique identifier for a post or page. It’s typically a URL-friendly version of the title, and it’s used to identify the post or page in the database.
A taxonomy template, on the other hand, is a custom template that’s used to display content from a specific taxonomy (e.g. categories, tags, etc.).
Now that we’ve defined these terms, let’s get to the main topic: how to get the slug of the current category in a taxonomy template. Here are the steps:
1) Determine the name of your taxonomy. This will typically be either “category” or “tag”, but it could also be a custom taxonomy that you’ve created.
2) Use the get_query_var()
function to retrieve the current taxonomy term. This function takes the name of the taxonomy as an argument, so you’ll need to pass it in as a string.
$current_term = get_query_var( 'taxonomy' );
3) Use the get_term_by() function to retrieve the term object for the current taxonomy term. This function takes three arguments: the field you want to search by (in this case, “slug”), the value of the field you’re searching for (i.e. the slug), and the taxonomy you’re searching in.
$term_obj = get_term_by( 'slug', $current_term, 'taxonomy' );
4) Retrieve the slug from the term object. The slug is stored in the slug field of the term object, so you can access it like this:
$slug = $term_obj->slug;
That’s it! You now have the slug of the current category in your taxonomy template. You can use this slug to query for content from the category or to display it in the template.