Quantcast
Channel: WordPress Experts
Viewing all articles
Browse latest Browse all 33

How to Get Category Slug Using Category ID!

$
0
0

imagesWhen styling your WordPress theme files, you may come across times when you may wish to display your WordPress Category slug ( category name ). We can easily achieve this task by using the Category ID.

In one of my recent works, I had to play a lot with categories. I was looking for an easy way to get 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); ?>

 

The above code will display the slug for the category with the Category ID of 3.

Thanks to Ken Rosaka for the tip!

If you enjoyed this article, please consider sharing it!!

The post How to Get Category Slug Using Category ID! appeared first on WordPress Experts.


Viewing all articles
Browse latest Browse all 33

Trending Articles