首页 文章

Wordpress用slug获取分类法名称

提问于
浏览
4

如何只使用分类标本获取分类标识或名称?

我想我正在寻找相当于get_term_by()但是对于分类法 .

编辑:我必须指定我正在尝试获取WooCommerce产品属性的税号 .

谢谢

4 回答

  • 0
    <?php 
        $term = get_term_by('slug', $slug, 'category'); 
        $name = $term->name; 
        $id = $term->term_id;
    ?>
    
  • -3

    WordPress提供了一个函数来从其slug中获取分类信息 .

    $taxonomy_details = get_taxonomy( $slug );
    

    这将作为对象返回分类法详细信息,其中包括分类法的各种标签 . 例如,这里是调用标准类别分类法时返回的对象,例如 get_taxonomy( 'category' );

    stdClass Object
    (
        [labels] => stdClass Object
            (
                [name] => Categories
                [singular_name] => Category
                [search_items] => Search Categories
                [popular_items] => 
                [all_items] => All Categories
                [parent_item] => Parent Category
                [parent_item_colon] => Parent Category:
                [edit_item] => Edit Category
                [view_item] => View Category
                [update_item] => Update Category
                [add_new_item] => Add New Category
                [new_item_name] => New Category Name
                [separate_items_with_commas] => 
                [add_or_remove_items] => 
                [choose_from_most_used] => 
                [not_found] => No categories found.
                [menu_name] => Categories
                [name_admin_bar] => category
            )
    
        [description] => 
        [public] => 1
        [hierarchical] => 1
        [show_ui] => 1
        [show_in_menu] => 1
        [show_in_nav_menus] => 1
        [show_tagcloud] => 1
        [show_in_quick_edit] => 1
        [show_admin_column] => 1
        [meta_box_cb] => post_categories_meta_box
        [rewrite] => Array
            (
                [hierarchical] => 1
                [slug] => category
                [with_front] => 1
                [ep_mask] => 512
            )
    
        [query_var] => category_name
        [update_count_callback] => 
        [_builtin] => 1
        [cap] => stdClass Object
            (
                [manage_terms] => manage_categories
                [edit_terms] => manage_categories
                [delete_terms] => manage_categories
                [assign_terms] => edit_posts
            )
    
        [name] => category
        [object_type] => Array
            (
                [0] => post
            )
    
        [label] => Categories
    )
    

    资料来源:https://codex.wordpress.org/Function_Reference/get_taxonomy

  • 1

    由于接受的答案没有回答这个问题,我在这里提供了答案,即使这个问题很老 .

    get_term_by() 的第三个(必需的)参数是分类法本身的名称,因此不能使用此函数 .

    get_taxonomies() 可以't be used either because then you would have to match the entire rewrite array, which you probably don' t可以访问 .

    所以我找到的唯一方法是使用私有数组 $wp_taxonomies

    function get_tax_name_from_slug($slug){
      foreach ($wp_taxonomies as $key => $value) {
        if ($value->rewrite['slug'] === $slug){
            return $key;
        }
      }
    }
    

    我真的希望Wordpress能够提供一种方法来实现这一目标,而无需访问其内部数据结构 .

  • 10
    $args = array(
                        'post_type' => 'awards',
                        'post_status' => 'publish',
                        'posts_per_page' => 4,
                         'orderby' => 'ID',
                         'order' => 'DESC',
                        'tax_query' => array(
                            'relation' => 'AND',
                            array(
                                'taxonomy' => 'awards_categories',
                                'field' => 'slug',
                                'terms' => $award_solution
                            ),
                            array(
                                'taxonomy' => 'year',
                                'field' => 'slug',
                                'terms' => $yearvalue
                            ),
                        )
                    );
    

    我们如何使用wp select查询获取此内容

相关问题