DISCLAIMER: this post is older than one year and may not be up to date with latest WordPress version.

In one of my recent freelance works, I had to play a lot with categories. I was looking for an easy way to get a category slug using an ID, and I found this great function that I’d like to share with you today.

First, put the following function in your functions.php file:

function get_cat_slug($cat_id) {
	$cat_id = (int) $cat_id;
	$category = &get_category($cat_id);
	return $category->slug;

Once done, you can call the function as shown below:

<?php echo get_cat_slug(3); ?>

This will display the slug for the category with the ID 3.

Thanks to Ken Rosaka for the tip!


  1. How would you use this on a category archive page where the category ID is a variable?

  2. thanks for the code. it was a great help.

  3. Hi, how could I get the current category slug for an archive page?

Leave a Comment

Your email address will not be published. Required fields are marked *