简体   繁体   中英

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.

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. 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

Your custom code should go around the woocommerce_product_loop_start

Also check the 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. 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
  3. Select “Show categories” from Shop Page Display
  4. Click on Save Changes

Show WooCommerce Sub Categories on Shop Page Categories – Subcategories – Products. 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
  3. Select “show subcategories” from Category Display
  4. Click on Save Changes

Also Check : https://www.businessbloomer.com/woocommerce-categories/

Add this in your 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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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