Sign up ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

How can you get the category ID of "category-name" from a nice URL (e.g.

I need to retrieve the category ID from a URL of wordpress because this function will be used in wp_nav_menu() since the design of the site is to have a dropdown of latest post under the category, so if it is not a category of wordpress (link to home page or about page), then there should be on dropdown.

I found this site( to be an example of the problem :)

share|improve this question

migrated from May 29 '13 at 4:27

This question came from our site for professional and enthusiast programmers.

You mean, from the URL? Or from within the page? – Pekka 웃 May 28 '13 at 15:26
From within a page, but need to get the category ID based from a URL. To why, I'm placing the category links to Wordpress's built in Navigation. – Earvin Bryan S. Co May 28 '13 at 15:29

2 Answers 2

Try this,

$cat = get_category_by_path('')


share|improve this answer

You will want a couple of steps:

  1. get_category_by_slug( $slug ) - Call this with the URL fragment (the slug) as an arg, and it'll give you a category object.
  2. $category->cat_ID - Call this on the $category object to return the ID of the category.

A one-liner would look like: $slug_ID = get_category_by_slug($slug)->cat_ID;

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.