[英]Target WooCommerce product category archive pages from a child category term
I have a question regarding the product category.我对产品类别有疑问。 I have a category like that:
我有一个这样的类别:
-electronic
-- laptop
-- mobile
then I want to create a logic for all product under electronic
, I use is_product_category( 'electronic' )
, but it doesn't work for electronic, it only works when URL is mywebsite.com/product-category/electronic
when I use mywebsite.com/product-category/electronic/mobile/
it doesn't work.然后我想为
electronic
下的所有产品创建一个逻辑,我使用is_product_category( 'electronic' )
,但它不适用于 electronic,只有当我使用mywebsite.com/product-category/electronic/mobile/
时 URL 是mywebsite.com/product-category/electronic
时它才有效mywebsite.com/product-category/electronic/mobile/
它不起作用。 Should I use the following codes or there is another option:我应该使用以下代码还是有其他选择:
is_product_category( ‘laptop’ )
is_product_category( ‘mobile’ )
You can use term_is_ancestor_of()
to check if the current term (product category) being viewed belongs to a parent term.您可以使用
term_is_ancestor_of()
来检查正在查看的当前术语(产品类别)是否属于父术语。
I've written a simple helper function:我写了一个简单的助手 function:
/**
* @param int|object $parent ID or object term object to check.
* @return bool Whether the product category being viewed is a child of the given parent category.
*/
function wpse_is_child_product_category( $parent ) {
if ( ! is_product_category() ) {
return false;
}
return term_is_ancestor_of( $parent, get_queried_object(), 'product_category' );
}
Usage:用法:
if ( is_product_category( 5 ) || wpse_is_child_product_category( 5 ) ) {
// . . .
You can create a custom conditional function that handle also any children product category on product category archives like (handle term name, term slug or term id) :您可以创建一个自定义条件 function 来处理产品类别档案中的任何子产品类别,例如(处理术语名称、术语 slug 或术语 id) :
/**
* Determines whether the query is for an existing product category archive page or for an ancestors product category archive page.
*
* @param int|string term ID, term slug or term name to check.
* @return bool
*/
function is_maybe_child_product_category( $category ){
if( is_product_category( $category ) ) {
return true;
}
$object = get_queried_object();
if( ! is_a( $object, 'WP_Term') ) {
return false;
}
$taxonomy = $object->taxonomy;
$children = get_term_children( $object->term_id, $taxonomy );
$result = (array) term_exists( $category, $taxonomy );
if( ! empty( $result ) ) {
return false;
}
return in_array( reset($result), $children );
}
Code goes in functions.php file of the active child theme (or active theme).代码进入活动子主题(或活动主题)的 functions.php 文件。 Tested and works.
测试和工作。
USAGE:用法:
if ( is_maybe_child_product_category( 'laptop' ) ) {
// Do something
}
I have a question regarding the product category.我有一个关于产品类别的问题。 I have a category like that:
我有一个这样的类别:
-electronic
-- laptop
-- mobile
then I want to create a logic for all product under electronic
, I use is_product_category( 'electronic' )
, but it doesn't work for electronic, it only works when URL is mywebsite.com/product-category/electronic
when I use mywebsite.com/product-category/electronic/mobile/
it doesn't work.然后我想为
electronic
下的所有产品创建一个逻辑,我使用is_product_category( 'electronic' )
,但它不适用于电子,它仅在 URL 是mywebsite.com/product-category/electronic
时才有效,当我使用mywebsite.com/product-category/electronic/mobile/
它不起作用。 Should I use the following codes or there is another option:我应该使用以下代码还是有其他选择:
is_product_category( ‘laptop’ )
is_product_category( ‘mobile’ )
I'm still learning php and I'm trying to do something close to your question.我仍在学习 php 并且我正在尝试做一些接近你问题的事情。
The shop is selling pictures and each is classified by its artist.这家商店出售图片,每张图片都按其艺术家分类。
CAT : ARTIST
SUB-CAT : ARTIST A
I need each product on the archive page to display its SUB-CAT.我需要存档页面上的每个产品都显示其 SUB-CAT。
I don't know how to target the subcategory, and I'm stuck at this point:我不知道如何定位子类别,我被困在这一点上:
add_action( 'woocommerce_shop_loop_item_title','add_artist_name', 9);
function add_artist_name() {
global $product;
if (is_product_category( 'artistes' )){
echo // ADD SUBCATEGORY NAME
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.