nnnnnn
nnnnnn

Reputation: 133

How to get the category name from category slug in wordpress?

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

Answers (3)

Gufran Hasan
Gufran Hasan

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');

Reference Link

Upvotes: 4

user5933888
user5933888

Reputation:

$cat = get_term_by( 'slug', 'aeroplane', 'category');
echo $cat->name;

Upvotes: 7

Rob_jS
Rob_jS

Reputation: 441

Use get_category_by_slug. E.g.

<?php
$catObj = get_category_by_slug('category-slug'); 
$catName = $catObj->name;
?>

Upvotes: 14

Related Questions