1. PROMO Get 60% OFF Lifetime Pro Membership & access all our Pro Divi Extensions, Divi Layouts, Divi AI Generator, Divi Block, etc.VIEW PRICING
Dismiss Notice
UPDATE Custom Color Scheme + Premade Divi Layouts - A perfect combination to boost productivity. New 'Gadget Repair' layout added.Try our Custom Color Layouts
BEST Divi Block - A revolutionary drag & drop tool to easily mix & match 960+ premade blocks (Light & Dark) to kick start your Divi site design. Special module designs are included as well.
Learn More About Divi BlockFree Version ~ 340+ Free Blocks

Side Bar Categories

Discussion in 'Free Divi Community Forum' started by Karen Browning, Nov 8, 2023.

  1. Karen Browning

    Karen Browning New Member

    Hi,

    I have a side bar with product categories, which is going to be huge by the time I've added them all. How do I have it just showing the 1st tier for example Suppliers, Servo Motors, Stepper Motors. Then you click on the category to expand it?
    The 1st image is our wordpress site, the 2nd 2 are how it looks on the current site which is how I'd like it to work on the new wordpress site.

    Thanks
     

    Attached Files:

    1. PRO MEMBER PERKS Divi Block Pro - Premade logo carousel design for your Divi site without using any plugins:View Demo
  2. Divi.Help

    Divi.Help Administrator
    Staff Member

    That would require custom code.

    I roughly did the coding. Try the below code in WP Admin > Divi > Theme Options > Integration > Body Code & see if it works:
    Code:
    <style>
        .wc-block-product-categories-list--depth-1 {
            display: none;
        }
        .wc-block-product-categories-list--depth-0>li.open .wc-block-product-categories-list--depth-1 {
            display: block;
        }
    </style>
    <script>
        jQuery(document).ready(function($) {
            $('.wc-block-product-categories-list--depth-0>li').click(function(e) {
                if ($(this).find('li').length > 0) {
                    e.preventDefault();
                    $(this).toggleClass('open');
                }
            });
        });
    </script>
     
    Karen Browning likes this.
  3. Karen Browning

    Karen Browning New Member

    1. PRO MEMBER PERKS Divi Block Pro - Premade before / after image slider for your Divi site without using any plugins:View Demo
  4. Divi.Help

    Divi.Help Administrator
    Staff Member

    Try the below instead & see if it works:
    Code:
    <style>
        .wc-block-product-categories-list--depth-0, .wc-block-product-categories-list-item.open>ul{
            display: block!important;
        }
        .wc-block-product-categories-list {
            display: none;
        }
    </style>
    <script>
        jQuery(document).ready(function($) {
            $('.wc-block-product-categories-list-item>a').click(function(e) {
                if ($(this).parent().find('li').length > 0) {
                    e.preventDefault();
                    $(this).parent().toggleClass('open');
                }
            });
        });
    </script>
     
  5. Karen Browning

    Karen Browning New Member

    That looks good, would it be possible for when you click on a product for example under servo motors, if I select rotary
    https://www.servernetworks.uk/product-category/servo-motors/rotary/ the relevant category stays open?
    currently the side bar closes so just the 1st level categories are showing.
    The servo_rotary1.jpg is how it currently looks.
    I'd like it to look like servo_rotary2.jpg
     

    Attached Files:

    1. PRO MEMBER PERKS Divi Ultimate Archive Plugin - Replace the default archive "Older Entries" with beautiful pagination:Learn More
  6. Divi.Help

    Divi.Help Administrator
    Staff Member

    Try the below & see if it works:
    Code:
    <script>
    jQuery(function($) {
          // Get the text of .woocommerce-products-header__title
          var pageTitle = $('.woocommerce-products-header__title.page-title').text().trim();
    
          // Find matching elements in .wp-block-woocommerce-product-categories
          $('.wp-block-woocommerce-product-categories .wc-block-product-categories-list-item__name').each(function() {
            var categoryText = $(this).text().trim();
    
            // Check for text match
            if (pageTitle === categoryText) {
              // Find closest parents and add 'open' class
              $(this).closest('.wc-block-product-categories-list-item')
                .closest('.wc-block-product-categories-list-item')
                .addClass('open');
            }
          });
    });
    </script>
     
  7. Karen Browning

    Karen Browning New Member

    Hi,
    Thank you. This has all the categories open. I would like them just to open when you click on them. Also, to make it even more complicated, can you select more than 1 category?

    So, visitors to our site may want all the Rotary Servo Motors from Kollmorgen showing, is this possible? Maybe check/tick boxes?
     
    1. PRO MEMBER PERKS Custom Color Scheme + Premade Layouts - A perfect combination to boost productivity:Try Now
  8. Divi.Help

    Divi.Help Administrator
    Staff Member

    Try the below code in WP Admin > Divi > Theme Options > Integration > Body Code & see if it works:
    Code:
    <style>
        .wc-block-product-categories-list--depth-0, .wc-block-product-categories-list-item.open>ul{
            display: block!important;
        }
        .wc-block-product-categories-list {
            display: none;
        }
    </style>
    <script>
        jQuery(document).ready(function($) {
            $('.wc-block-product-categories-list-item>a').click(function(e) {
                if ($(this).parent().find('li').length > 0) {
                    e.preventDefault();
                    $(this).parent().toggleClass('open');
                }
            });
        });
    </script>
    <script>
    jQuery(function($) {
          // Get the text of .woocommerce-products-header__title
          var pageTitle = $('.woocommerce-products-header__title.page-title').text().trim();
    
          // Find matching elements in .wp-block-woocommerce-product-categories
          $('.wp-block-woocommerce-product-categories .wc-block-product-categories-list-item__name').each(function() {
            var categoryText = $(this).text().trim();
    
            // Check for text match
            if (pageTitle === categoryText) {
              // Find closest parents and add 'open' class
              $(this).closest('.wc-block-product-categories-list-item').addClass('open');
              $(this).closest('.wc-block-product-categories-list-item').closest('.wc-block-product-categories-list-item').addClass('open');
            }
          });
    });
    </script>
    You will need to look for plugins that do the job.