Wordpress 使用 slug 获取分类名称

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12548353/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 19:01:35  来源:igfitidea点击:

Wordpress get taxonomy name with slug

wordpresstaxonomy

提问by Sebastien

How can I get a taxonomy id or name with only the taxonomy slug ?

如何仅使用分类法 slug 获得分类法 id 或名称?

I guess I'm looking for the equivalent of get_term_by()but for taxonomies.

我想我正在寻找相当于get_term_by()但分类法的东西。

Edit : I must specify that I'm trying to get the tax ID of a WooCommerce product attribute.

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

Thanks

谢谢

采纳答案by Ozerich

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

回答by Lee Willis

WordPress does provide a function to get the taxonomy information from its slug.

WordPress 确实提供了从其 slug 中获取分类信息的功能。

$taxonomy_details = get_taxonomy( $slug );

This will return the taxonomy details as an object, which includes the various labels for the taxonomy. For example here's the returned object when called for the standard Category taxonomy, e.g. get_taxonomy( 'category' );

这会将分类详细信息作为对象返回,其中包括分类的各种标签。例如,这是为标准类别分类法调用时返回的对象,例如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
)

Source: https://codex.wordpress.org/Function_Reference/get_taxonomy

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

回答by Hjalmar

As the accepted answer does not answer the question, I provide an answer here even though the question is very old.

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

The third (required) argument to get_term_by()is the name of the taxonomy itself, and so this function can not be used.

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

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

get_taxonomies()也不能使用,因为这样您就必须匹配整个重写数组,而您可能无权访问。

So the only way I found was to use the private array $wp_taxonomies:

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

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

I really hope Wordpress will provide a way to do this without accessing their internal data structures.

我真的希望 Wordpress 能够提供一种无需访问其内部数据结构的方法。

回答by harsimer

$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
                        ),
                    )
                );

how we fetch this with wp select query

我们如何使用 wp select 查询来获取它