10

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

我想我正在寻找get_term_by()分类法的等价物。

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

谢谢

4

4 回答 4

18

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

于 2015-05-10T14:22:07.527 回答
3

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

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

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

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

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

我真的希望 Wordpress 能够提供一种方法来做到这一点,而无需访问其内部数据结构。

于 2015-03-04T16:29:44.353 回答
0
$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 查询来获取它

于 2015-09-07T13:33:51.390 回答
-4
<?php 
    $term = get_term_by('slug', $slug, 'category'); 
    $name = $term->name; 
    $id = $term->term_id;
?>
于 2012-09-22T22:58:18.940 回答