admin管理员组文章数量:1418103
In WooCommerce, archive-product is the default template for all product categories. I want to create a new product category template for one parent category and all of its children.
I've created a new template archive-foo.php
Now I need to change the code inside taxonomy-product_cat.php and tell it to use archive-foo.php "if it's my parent category or any of its children".
Here is my code inside taxonomy-product_cat.php:
if (is_product_category( 'foo-category' ) || cat_is_ancestor_of(19, get_queried_object()->term_id )){ wc_get_template( 'archive-foo.php' );
} else { wc_get_template( 'archive-product.php' ); }
Currently, it's working for the parent category, but none of its children. Any help would be greatly appreciated.
In WooCommerce, archive-product is the default template for all product categories. I want to create a new product category template for one parent category and all of its children.
I've created a new template archive-foo.php
Now I need to change the code inside taxonomy-product_cat.php and tell it to use archive-foo.php "if it's my parent category or any of its children".
Here is my code inside taxonomy-product_cat.php:
if (is_product_category( 'foo-category' ) || cat_is_ancestor_of(19, get_queried_object()->term_id )){ wc_get_template( 'archive-foo.php' );
} else { wc_get_template( 'archive-product.php' ); }
Currently, it's working for the parent category, but none of its children. Any help would be greatly appreciated.
Share Improve this question edited Aug 9, 2019 at 16:51 LoicTheAztec 3,39117 silver badges24 bronze badges asked Aug 9, 2019 at 15:07 combatdevcombatdev 135 bronze badges1 Answer
Reset to default 1the WordPress function cat_is_ancestor_of()
is made for WordPress categories, but not for WooCommerce product categories which is a custom taxonomy product_cat
.
For Custom taxonomies you can use WordPress term_is_ancestor_of()
function like:
term_is_ancestor_of( 19, get_queried_object_id(), 'product_cat' )
So in your code:
if ( is_product_category( 'foo-category' ) || term_is_ancestor_of( 19, get_queried_object_id(), 'product_cat' ) ) {
wc_get_template( 'archive-foo.php' );
} else {
wc_get_template( 'archive-product.php' );
}
This time it should work.
本文标签: custom taxonomyHow to target parent product category and its children using WooCommerce
版权声明:本文标题:custom taxonomy - How to target parent product category and its children using WooCommerce? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745254223a2650004.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论