How to Get Category Slug in WordPress

Using category slug is the best way to use in wordpress custom query code as the category name does not always work. Rather use ‘category-slug’ instead, in this tutorial I’ll show you how to get WordPress slug in Category or in single page view.

For category view you can use this code snippet.

For single view or post view

Still hungry?

If in the case you only want to get category slug using category ID don’t worry folks, I’ve provided a solution on it too.

Step 1:

In your function.php file, put this function.

Step 2:

Once done, you can use the function like the below code.

Don’t know how to get WordPress Category ID?

Visit this link Get Single Taxonomy ID to learn more.

That’s it happy blogging, you can share your idea below.

by Ryan Sutana

  • how’s life ? blogger discovered your blog via yahoo but it was hard to find and I see you could have more visitors because there are not so many comments yet. I have found website which offer to dramatically increase traffic to your website they claim they managed to get close to 1000 visitors/day using their services you could also get lot more targeted traffic from search engines as you have now. I used their services and got significantly more visitors to my site. Hope this helps 🙂 They offer best backlink service Take care. Jason

  • Hayden Hill

    Hi Ryan,

    I wonder if you could help me.

    I’m using a theme which has ‘portfolio’ as a custom post type. The portfolio items can be categorised in a similar way to standard posts.

    Currently, theme outputs portfolio URLs in the following format:

    …where ‘portfolio-name’ is simply the post title. the middle section of the URL remains static.

    What I would like to achieve is something like this:

    …where the ‘portfolio-category’ proportion of the URL is taken from the portfolio category.

    Each portfolio post will only be assigned to a single portfolio category at any one time.

    I found your post when searching for an answer to my query and you seem like a guy who knows his php and wordpress!

    If you have the time would you consider helping me out with this? I can send code examples across if this makes it easier for you to understand what i’m trying to achieve.

    Thanks in advance