How To Get Category Slug By Category ID In WordPress

Hi Friends, Some days ago I was trying to find out that how can we get category slug by category ID, For example If I have one category ‘WordPress’  and its id is ‘2’ then I want to get slug of “WordPress” category by its id ‘2’. So below is explanation by which we can get category slug by its id.

Go to your theme folder and open functions.php file and put below function anywhere in file :

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

After putting this into functions.php, you can easily use this function anywhere in your theme to get category slug by category id like this :

<?php echo get_category_slug('2'); &nbsp;?>

It will echo “WordPress” category slug. So this is way to get category slug by category id.

I hope this post helpful for someone. If you have any question then you can ask me anytime or post it in comment.

Also I am WordPress Developer, Freelance PHP Developer having more than 3 years of experience, So if you want to do any projects then you can contact me.

Hostgator Hosting in 1 Cent Coupon Code – “1CENTHECOUPON” (Without Quotes).