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:
1 2 3 4 5 | 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:
1 | <?php echo get_cat_slug(3); ?> |
This will display the slug for the category with the ID 3.
0 Comments.