【问题标题】:How to get the category name from category slug in wordpress?如何从 wordpress 中的类别 slug 中获取类别名称?
【发布时间】:2016-02-23 11:15:43
【问题描述】:

我有如下链接:

http://localhost/rajab/product-category/pvc-hose/

术语“pvc-hose”是类别塞子。我想从这个 slug 名称中派生类别名称。我想显示类别名称,因为 slug 名称之间有“-”。当我显示类别 slug 时,我不想要这个。这个怎么去掉?

【问题讨论】:

    标签: wordpress


    【解决方案1】:

    使用get_category_by_slug。例如

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

    【讨论】:

      【解决方案2】:
      $cat = get_term_by( 'slug', 'aeroplane', 'category');
      echo $cat->name;
      

      【讨论】:

        【解决方案3】:

        如果您想通过category namecategory slugcategory 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');
        

        Reference Link

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2015-11-15
          • 1970-01-01
          • 2015-03-19
          • 2016-06-25
          • 2017-08-02
          • 1970-01-01
          • 2017-12-24
          相关资源
          最近更新 更多