Get WordPress Category ID from a URL string

How can you get the category ID of “category-name” from a nice URL (e.g. http://www.example.com/category/category-name/)?

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.

Read More

I found this site(http://ditlo.com/) to be an example of the problem 🙂

Related posts

2 comments

  1. 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;

Comments are closed.