admin管理员组文章数量:1323157
I am tried to display all product category with bellow function .. getting a error Invalid taxonomy .. it's was working fine last 2 installation..
function be_woocommerce_category_id(){
$categories_array = array();
$categories_array[0] = esc_html__('Choose a Category', 'shopstore');
$args = array(
'orderby' => 'title',
'order' => 'ASC',
);
$categories = get_terms( 'product_cat', $args );
if( count($categories) > 0 ){
foreach( $categories as $category ){
$categories_array[$category->term_id] = $category->name;
}
}
return $categories_array;
}
I am tried to display all product category with bellow function .. getting a error Invalid taxonomy .. it's was working fine last 2 installation..
function be_woocommerce_category_id(){
$categories_array = array();
$categories_array[0] = esc_html__('Choose a Category', 'shopstore');
$args = array(
'orderby' => 'title',
'order' => 'ASC',
);
$categories = get_terms( 'product_cat', $args );
if( count($categories) > 0 ){
foreach( $categories as $category ){
$categories_array[$category->term_id] = $category->name;
}
}
return $categories_array;
}
Share
Improve this question
edited Feb 14, 2019 at 9:28
Tanmay Patel
8111 gold badge7 silver badges11 bronze badges
asked Feb 14, 2019 at 7:31
Saiful IslamSaiful Islam
758 bronze badges
3
- Where and when are you running this function? – Jacob Peattie Commented Feb 14, 2019 at 9:37
- it's default woocomerce hook – Saiful Islam Commented Feb 14, 2019 at 9:41
- Which hook? Specifically. – Jacob Peattie Commented Feb 14, 2019 at 9:48
3 Answers
Reset to default 1it's was working fine last 2 installation
Those two installation might still be on WordPress prior to version 4.5.0, and according to the reference:
Since 4.5.0, taxonomies should be passed via the ‘taxonomy’ argument in the
$args
array
So:
$args = array(
'orderby' => 'title',
'order' => 'ASC',
'taxonomy' => 'product_cat',
);
$categories = get_terms( $args );
If that doesn't work, then maybe on the new installation, your code is executed before the product_cat
taxonomy is registered.
I had the same 'problem' a few days ago. Like Sally CJ edited, this may be a registration issue.
When I called term_exists()
, the expected term_id was returned, but get_term()
returned the Invalid taxonomy error.
My solution was to call the register_taxonomy function in my function and everything was fine.
Edit:
I called it like that (with an empty array as $args):
register_taxonomy( 'taxonomy_name', array('the_custom_post_type'), array() );
I had the same problem. For Woocomerce you can be resolved by adding the below code in functions.php:
register_taxonomy( 'product_cat', array('product'), array() );
本文标签: Can39t display product categories on woocommerce getting Invalid taxonomy
版权声明:本文标题:Can't display product categories on woocommerce getting Invalid taxonomy? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742141314a2422591.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论