Home » Tips-n-Tricks » Get category slug using category ID in WordPress cms

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.

Array ( [0] => Tips-n-Tricks, Wordpress )

Leave a comment

0 Comments.

Leave a Reply

You must be logged in to post a comment.