最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to target all woocommerce categories that don't have any subcategories in them?

programmeradmin3浏览0评论

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;
}
发布评论

评论列表(0)

  1. 暂无评论