简体   繁体   English

在父类别页面上显示 woocommerce 个子类别

[英]Display woocommerce sub-categories when on a parent category page

I am trying to display all of the child product categories, while the parent category page is selected.我试图显示所有子产品类别,同时选择了父类别页面。

At the moment, within the wordpress customiser settings I have the 'Shop page display' to show categories, and the 'Category display' to show subcategories.目前,在 wordpress 定制器设置中,我有“商店页面显示”来显示类别,“类别显示”来显示子类别。

When I select the store page, I am presented with the list of categories - lets say one of them is music... great, When I then select music, I would expect to see a set of new child categories, such as Rock, Dance.当我 select 商店页面时,我会看到类别列表 - 假设其中一个是音乐......太棒了,当我然后 select 音乐时,我希望看到一组新的子类别,例如摇滚,舞蹈。 Pop ect.流行等。 However at the moment I am just presented with all of the individual products.然而,目前我只看到了所有单独的产品。

How to fix this?如何解决这个问题?

You can make your own category template. 您可以制作自己的类别模板。

The file should be taxonomy-product_cat.php Also look at the archive-product.php 该文件应为taxonomy-product_cat.php,同时查看archive-product.php

Your custom code should go around the woocommerce_product_loop_start 您的自定义代码应围绕woocommerce_product_loop_start

Also check the woocommerce codex 还要检查woocommerce Codex


And to show categories on shop page - 并在商店页面上显示类别-

It's easy, just go to 'WooCommerce -> Settings' link from side bar admin menu & select 'Catalog' tab & then tick 2 check boxes "Show subcategories on the shop page" & "When showing subcategories, hide product" -> click 'Save Changes' button at bottom. 很简单,只需从侧栏管理菜单中转到'WooCommerce -> Settings'链接,然后选择'Catalog'选项卡,然后勾选2个复选框"Show subcategories on the shop page""When showing subcategories, hide product" -> click 'Save Changes'底部的"When showing subcategories, hide product" -> click 'Save Changes'按钮。 That's it! 而已! You are Done! 你做完了!

Now visit 'Shop' page you'll be able to see categories. 现在访问“商店”页面,您将能够看到类别。

Hope this helps. 希望这可以帮助。

On the shop page you can display only categories, categories with products or only products.在商店页面上,您可以只显示类别、有产品的类别或仅显示产品。 If you want to display product categories on your Shop page instead of just products, follow these steps:如果您想在您的商店页面上显示产品类别而不仅仅是产品,请按照以下步骤操作:

  1. Click on Appearance > Customize单击“外观”>“自定义”
  2. Then go to WooCommerce > Product Catalog然后转到 WooCommerce > 产品目录
  3. Select “Show categories” from Shop Page Display从商店页面显示中选择“显示类别”
  4. Click on Save Changes单击保存更改

Show WooCommerce Sub Categories on Shop Page Categories – Subcategories – Products.在商店页面类别 - 子类别 - 产品上显示 WooCommerce 子类别。 If you want your shop page to display in that order.如果您希望商店页面按该顺序显示。 Follow these steps:按着这些次序:

  1. Click on Appearance > Customize单击“外观”>“自定义”
  2. Then go to WooCommerce > Product Catalog然后转到 WooCommerce > 产品目录
  3. Select “show subcategories” from Category Display从类别显示中选择“显示子类别”
  4. Click on Save Changes单击保存更改

Also Check : https://www.businessbloomer.com/woocommerce-categories/另请检查: https : //www.businessbloomer.com/woocommerce-categories/

Add this in your function.php将此添加到您的 function.php

add_action( 'display_product_subcategories', 'product_subcategories', 1 );
function product_subcategories( $args = array() ) {

    $parentid = get_queried_object_id();
    $args = array(
        'parent' => $parentid
    );
    
    $terms = get_terms( 'product_cat', $args );
    
    if ( $terms ) {
        echo '<div>';
    
        foreach ( $terms as $term ) {
            echo '<li class="category">';                 
                woocommerce_subcategory_thumbnail( $term );
                echo '<h2>';
                    echo '<a href="' .  esc_url( get_term_link( $term ) ) . '" class="' . $term->slug . '">';
                        echo $term->name;
                    echo '</a>';
                echo '</h2>';
            echo '</li>';
        }
    
        echo '</ul>';
    }
}

Then summon the function in your template page eg archive-product.php然后在您的模板页面中调用 function,例如 archive-product.php

<?php do_action( 'display_product_subcategories' ); ?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM