简体   繁体   English

从子类别术语中定位 WooCommerce 产品类别存档页面

[英]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.

相关问题 在Woocommerce产品类别存档页面中获取当前术语名称 - Get current term name in Woocommerce product category archive pages 根据 Woocommerce 存档页面中的产品类别更改产品按钮 - Change product button based on product category in Woocommerce archive pages 在 woocommerce 存档中添加产品类别 - Add Product category in woocommerce archive WooCommerce 产品类别页面,带有上一期和下一期链接 - WooCommerce product category pages with previous and next term links Woocommerce中产品类别归档页面和相关单品的条件逻辑 - Conditional logic for product category archive pages and related single products in Woocommerce 更改特定 Woocommerce 产品类别存档页面的默认排序 - Change default sorting for specific Woocommerce product category archive pages 在 WooCommerce 类别存档页面上以特定分隔符显示产品标题 - Show product titles to specific delimiter on WooCommerce category archive pages 在Woocommerce产品类别归档页面上禁用“添加到购物车”按钮 - Disable add to cart button on Woocommerce product category archive pages 为 WooCommerce 类别存档页面添加产品标题标签 - Add product title heading tag for WooCommerce category archive pages 来自当前父类别的Woocommerce产品子类别 - Woocommerce Product child category from current parent category
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM