admin管理员组

文章数量:1326641

I want to target all pages (product categories) that are displaying the products themselves.

My woocommerce settings set to show only categories/subcategories (unless there are none) and then by default the products grid is displayed on the page.

is_product_category() function doesn't help since it also targets the parent categories which don't have direct products in them.

Most of my categories that don't any subcategories in them are grandchildren categories if it matters for the answer.

How can I achieve it and to use it in woocommerce hooks?

I want to target all pages (product categories) that are displaying the products themselves.

My woocommerce settings set to show only categories/subcategories (unless there are none) and then by default the products grid is displayed on the page.

is_product_category() function doesn't help since it also targets the parent categories which don't have direct products in them.

Most of my categories that don't any subcategories in them are grandchildren categories if it matters for the answer.

How can I achieve it and to use it in woocommerce hooks?

Share Improve this question asked Aug 6, 2020 at 10:48 VictorVictor 254 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

You can use the woocommerce_products_will_display(). This function returns true if the current shop page is going to display products. This will be the case if your shop pages are set to display products or subcategories and products, but it will be false if the shop pages are set to display subcategories only, and the current category has subcategories.

if ( woocommerce_products_will_display() ) {
    // Products are showing.
} else {
    // Products are not showing.
}

The woocommerce_get_loop_display_mode() is similar, but can let you know whether subcategories are also displaying:

switch ( woocommerce_get_loop_display_mode() ) {
    case 'products':
        // Products are displaying.
        break;
    case 'subcategories':
        // Subcategories are displaying.
        break;
    case 'both':
        // Products and subcategories are displaying.
        break;
}

本文标签: How to target all woocommerce categories that don39t have any subcategories in them