【发布时间】:2016-02-23 11:15:43
【问题描述】:
我有如下链接:
http://localhost/rajab/product-category/pvc-hose/
术语“pvc-hose”是类别塞子。我想从这个 slug 名称中派生类别名称。我想显示类别名称,因为 slug 名称之间有“-”。当我显示类别 slug 时,我不想要这个。这个怎么去掉?
【问题讨论】:
标签: wordpress
我有如下链接:
http://localhost/rajab/product-category/pvc-hose/
术语“pvc-hose”是类别塞子。我想从这个 slug 名称中派生类别名称。我想显示类别名称,因为 slug 名称之间有“-”。当我显示类别 slug 时,我不想要这个。这个怎么去掉?
【问题讨论】:
标签: wordpress
使用get_category_by_slug。例如
<?php
$catObj = get_category_by_slug('category-slug');
$catName = $catObj->name;
?>
【讨论】:
$cat = get_term_by( 'slug', 'aeroplane', 'category');
echo $cat->name;
【讨论】:
如果您想通过category name、category slug 和category ID 获取类别详细信息,则应使用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');
【讨论】: