PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, July 1, 2022

[FIXED] how to separate products from loop and put them in to division group by 2 in shopify

 July 01, 2022     shopify     No comments   

Issue

I have an products array {1,2,3,4,5,6,7,8}

I want to put them into div group by 2

I have a structure like this the problem is i got all products in one div I want to separate every two products

<div class="images">
images 1 to 8
</div>

I need structure like

 <div class="images">
images 1 and 2
images 3 and 4 images 5 and 6 images 7 and 8
{% for media in product.media %}
               
      
    <div class="image-block ">       
      

      <a  href="{{ media.preview_image | img_url: product_image_zoom_size, scale: product_image_scale }}"
         class=" product-single__thumbnail--{{ section.id }} even count-{{forloop.index}} "
         data-thumbnail-id="{{ section.id }}-{{ media.id }}">  <img class="product-single__thumbnail-image" src="{{ media.preview_image | img_url: 'large', scale: 2 }}" alt="{{ thumbnailAlt }}"></a>


    </div>
    
        {% endfor %}

Solution

use logic this into your loop to open and close and HMTL element according to your need.

{% for media in product.media %}
  {% assign data = forloop.index | modulo:2 %}
  {% if data != 0 %}<div class="wrapper">{% endif %}
    inner content goes here
  {% if data == 0 %}</div>{% endif %}
{% endfor %}

Here is the working example of the same code: enter image description here enter image description here



Answered By - Onkar
Answer Checked By - Willingham (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing