How can I get a taxonomy id or name with only the taxonomy slug ?
I guess I'm looking for the equivalent of get_term_by()
but for taxonomies.
Edit : I must specify that I'm trying to get the tax ID of a WooCommerce product attribute.
Thanks
Creating Taxonomy Archive Templates in WordPress Themes For instance, you can simply create a category. php template in your theme and WordPress will then use it to display your category archive pages. Similarly, you can create a template for any custom taxonomy by naming it in the taxonomy-{taxonomy}-{term}.
You can use $term = get_term_by('id', $id) see get_term_by() codex page. Then access slug using $term->slug . It means that get_term_by did not found any result. Are you sure a term with the ID you provide exists?
The key: get_queried_object()$obj = get_queried_object(); $cat_slug = $obj->slug; This outputs the first post category when called on any archive view.
WordPress does provide a function to get the taxonomy information from its 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' );
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With