How To Get Existing Category Slug In WordPress

Hi, friends if you try to get existing category Slug, Name, ID, or Description? Here you have a solution for that, there is various way to get the category slug to display on the post pages. Now we going to use one of the easiest ways to display the category slug or anything which you like.
The main advantage of the below code you can use this code anywhere on your post page and you don’t need to loop this. What we doing is getting hole primary category data then based on that we get the slug variable $cat_slug to display.
1 2 3 4 5 6 7 8 9 10 11 12 |
<?php if (is_single()) { $cats = get_the_category(); $cat = $cats[0]; } else { $cat = get_category(get_query_var('cat')); } $cat_slug = $cat->slug; ?> |
Display Slug
1 |
Category Slug: <?php echo $cat_slug; ?> |
Get Existing Category Name, ID, and Description
As same like getting existing Slug, we going to get other details like Category Name, ID, and Description. In following code we going to get the category variables $cat_name, $cat_id, $cat_description to display in the post.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<?php if (is_single()) { $cats = get_the_category(); $cat = $cats[0]; } else { $cat = get_category(get_query_var('cat')); } $cat_name = $cat->cat_ID; $cat_id = $cat->name; $cat_description = $cat->description; ?> |
Display Variables
1 2 3 |
Category Name: <?php echo $cat_name; ?> Category ID: <?php echo $cat_id; ?> Category Description: <?php echo $cat_description; ?> |
Output:
Conclusion:
I hope this article will help you to get your existing category name, slug, id, and description. If you have any doubts about this section let me know in the comment box. If you like this share it with your friends.
Category SlugWithout Pluginwordpress
Mraj
Creative Designer & Developer specialist by the spirit and a loving blogger by thoughts. If you have any questions let me drop an email with the article name to the following email id: [email protected]