How To Get Category Slug By Category ID In WordPress

Posted by Ankur Gandhi in Wordpress



Wordpress Development

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).

You can follow any responses to this entry through the RSS 2.0 You can leave a response, or trackback.

  • http://gurushala.net/download-panda-cloud-antivirus-1-9-1-beta-free/ suhana

    thanks a lot fo rthis great blog ankur!!

    • http://izmirescortbayan.com/ izmir escort

      thanks..

  • hasan

    very interesting article and very useful for all readers. good post

  • http://gurushala.net/today%E2%80%99s-google-doodle118th-birthday-of-albert-szent-google-doodle/ reehan

    nice writeup buddy!

  • http://www.mgtdesign.co.uk Web Design

    Nice post.Well written article.I appreciate your writing skills.Nice tutor.You have done a great job by sharing this post with us.I like this .Keep sharing with us in future too.

  • Pingback: Abamba

  • http://www.webhostings.in/ web hosting companies

    I am happy when reading your blog with updated information

  • Pingback: consumer reviews