Reputation: 133
I have the link like below :
http://localhost/rajab/product-category/pvc-hose/
the term "pvc-hose" is the category slug. I wanted to derive the category name from this slug name. I wanted to display the category name becaus the slug name is having the "-" in between.I dont want this when i am displaying the category slug. How to remove this ?
Upvotes: 6
Views: 29822
Reputation: 9373
If you want to get category details by category name
, category slug
, and category ID
then you should use get_term_by()
.
// Get term by name ''news'' in Categories taxonomy.
$category = get_term_by('name', 'news', 'category')
// Get term by name ''news'' in Tags taxonomy.
$tag = get_term_by('name', 'news', 'post_tag')
// Get term by name ''news'' in Custom taxonomy.
$term = get_term_by('name', 'news', 'my_custom_taxonomy')
// Get term by name ''Default Menu'' from theme's nav menus.
// (Alternative to using wp_get_nav_menu_items)
$menu = get_term_by('name', 'Default Menu', 'nav_menu');
Upvotes: 4
Reputation:
$cat = get_term_by( 'slug', 'aeroplane', 'category');
echo $cat->name;
Upvotes: 7
Reputation: 441
Use get_category_by_slug. E.g.
<?php
$catObj = get_category_by_slug('category-slug');
$catName = $catObj->name;
?>
Upvotes: 14