WordPress trick: Get category slug using category ID

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!

Looking for WordPress hosting? Try WP Web Host. Prices starts at $5/month and you can try it for free!

WordPress trick: Get category slug using category ID

Leave a Reply

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