4th level of sub What do i Need to change or add to bring in the 4th level of sub cats?

AbbasAbbas Member Posts: 54

I need to return the next 4th lvl of sub cats?

This gets the 3rd but I dont know if this whole code needs re adding with subCat.subs changing to something like subsubCat.subs or maybe a small part of code changed?

With the correct code category images would return on this page as small thumbs

Just like this

Thanks in advance for any advice or suggestions


{% set collectionTitle = collection.internal.url | split('/') %}

 {% if collectionTitle[1] %}

 {% for category in shop.categories %}

 {% for subCat in category.subs %}

 {% if subCat.id == collection.category_id and subCat.subs %}

 <div class="parenter grid-cell mb3 {% if category.url == exhibitions %}exhibitionsGrid{% endif %}">

   <div class="grid column-orient">

     <div class="grid-cell">

       <div class="grid wrap col-filtered relative">

       {% for sub in subCat.subs %}

       {% if sub.image %}

       <div class="grid-cell grow0 col-products_product already-visible come-in">

         <div class="subcatimage">

           <a href="{{ sub.url | url }}">

             <img class="image" src="{{ sub.image | url_image() }}">

             <div>{{ sub.title | t }}</div>

           </a>

         </div>

       </div>

       {% endif %}

       {% endfor %}

       </div>

     </div>

   </div>

 </div>   

 {% endif %}

 {% endfor %}

 {% endfor %}

 {% endif %}

3 comments

  • MilesbdMilesbd Moderator, Lightspeed Staff Posts: 25 moderator

    Hey Abbas!


    Great question,

    Generally what is done is to simply add another nested for loop, inside the 3rd-level category loop, to access any subcategories that exist as 4th-level subcategories


    For example, a loop like this would allow you to reach up to the 6th level deep categories:

    <categories>
    
       {% for category1 in product.categories %}
    
        <Cat1>
    
        <title>{{ category1.title | cdata }}</title>
    
        </Cat1>
    
          {% for subcategory2 in category1.subs %}
    
          <Cat2>
    
          <title2>{{ subcategory2.title | cdata }}</title2>
    
          </Cat2>
    
            {% for subcategory3 in subcategory2.subs %}
    
            <Cat3>
    
            <title3>{{ subcategory3.title | cdata }}</title3>
    
            </Cat3>
    
              {% for subcategory4 in subcategory3.subs %}
    
              <Cat4>
    
                <title4>{{ subcategory4.title | cdata }}</title4>
    
                </Cat4>
    
                  {% for subcategory5 in subcategory4.subs %}
    
                  <Cat5>
    
                  <title5>{{ subcategory5.title | cdata }}</title5>
    
                  </Cat5>
    
                    {% for subcategory6 in subcategory5.subs %}
    
                    <Cat6>
    
                    <title6>{{ subcategory6.title | cdata }}</title6>
    
                    </Cat6>
    
                  {% endfor %}
    
                {% endfor %}
    
              {% endfor %}
    
            {% endfor %}
    
          {% endfor %}
    
        {% endfor %}
    
        </categories>
    


    Take not that each loop to access subcategories needs to be nested within the parent category's for loop.


    Happy coding!

  • AbbasAbbas Member Posts: 54

    Hey Miles,


    Thankyou for your help,

    Using the code you added to you reply would it mean that I need to target these 2 parts of my code?


     {% for subCat in category.subs %}

     {% if subCat.id == collection.category_id and subCat.subs %}

     <div class="parenter grid-cell mb3 {% if category.url == exhibitions %}exhibitionsGrid{% endif %}">

       <div class="grid column-orient">

         <div class="grid-cell">

           <div class="grid wrap col-filtered relative">

           {% for sub in subCat.subs %}



    instead of subcategory5 in subcategory4.subs

    this

    for sub-subCat in subcategory.subs

    and

    {% for sub-sub in sub-subCat.subs %}


    ??

    Thankyou for your advice

  • AbbasAbbas Member Posts: 54

    This is the entire code that takes me to the 3rd lvl

    I dont code so any advice or pointers would be greatly appreciated! 😉

    waterstreetgallery.co.uk/en/exhibitions/past/2019/AUGUST

    August is there but invisible as are many other categories without a solution.

    {% for category in shop.categories %}

       {% if category.id == collection.category_id and category.subs %}

       <div class="parenter grid-cell mb3 {% if category.url == exhibitions %}exhibitionsGrid{% endif %}">

         <div class="grid column-orient">

           <div class="grid-cell">

             <div class="grid wrap col-filtered relative">

             {% for sub in category.subs %}

             {% if sub.image %}

             <div class="grid-cell grow0 col-products_product already-visible come-in">

               <div class="subcatimage">

                 <a href="{{ sub.url | url }}">

                   <img class="image" src="{{ sub.image | url_image() }}">

                   <div>{{ sub.title | t }}</div>

                 </a>

               </div>

             </div>

             {% endif %}

             {% endfor %}

             </div>

           </div>

         </div>

       </div>        

       {% endif %}

     {% endfor %}


     {% set collectionTitle = collection.internal.url | split('/') %}

     {% if collectionTitle[1] %}

     {% for category in shop.categories %}

     {% for subCat in category.subs %}

     {% if subCat.id == collection.category_id and subCat.subs %}

     <div class="parenter grid-cell mb3 {% if category.url == exhibitions %}exhibitionsGrid{% endif %}">

       <div class="grid column-orient">

         <div class="grid-cell">

           <div class="grid wrap col-filtered relative">

           {% for sub in subCat.subs %}

           {% if sub.image %}

           <div class="grid-cell grow0 col-products_product already-visible come-in">

             <div class="subcatimage">

               <a href="{{ sub.url | url }}">

                 <img class="image" src="{{ sub.image | url_image() }}">

                 <div>{{ sub.title | t }}</div>

               </a>

             </div>

           </div>

           {% endif %}

           {% endfor %}

           </div>

         </div>

       </div>

     </div>   

     {% endif %}

     {% endfor %}

     {% endfor %}

     {% endif %}

Sign In or Register to comment.