Change the blog to 3 - 4 grid column

Change the blog to 3 - 4 grid column

In main-blog.liquid, change the entire code to the followings:

  1. <script
  2.   src="{{ 'blog-posts-list.js' | asset_url }}"
  3.   type="module"
  4.   fetchpriority="low"
  5. ></script>

  6. {% liquid
  7.   if section.settings.background_color != blank
  8.     render 'contrast-override', background_color: section.settings.background_color, section_id: section.id
  9.   endif
  10. %}

  11. <blog-posts-list section-id="{{ section.id }}">
  12.   <div
  13.     class="section-background{% if section.settings.background_color != blank %} color-custom-{{ section.id }}{% endif %}"
  14.   ></div>
  15.   <div
  16.     class="
  17.       section
  18.       {% if section.settings.background_color != blank %}color-custom-{{ section.id }}{% endif %}
  19.       blog-posts
  20.       spacing-style
  21.       size-style
  22.     "
  23.     data-testid="blog-posts"
  24.     style="{% render 'spacing-style', settings: section.settings %}"
  25.   >
  26.     {% content_for 'blocks' %}

  27.     <div
  28.       ref="grid"
  29.       class="blog-posts-container"
  30.       style="--desktop-columns: {{ section.settings.columns }};"
  31.     >
  32.       {% liquid
  33.         assign image_index = 0

  34.         # The first two images reach the initial mobile viewport; later images use native lazy-loading.
  35.         assign eager_image_count = 2

  36.         # These sizes mirror --narrow-page-width in theme-styles-variables.liquid,
  37.         # --page-margin in base.css, and --columns-gap below.
  38.         # Desktop content uses a 40px page margin on each side, with 36px column gaps.
  39.         # Desktop: 3 or 4 columns (setting). Tablet (750px-1023px): 3 columns. Mobile: 1 column.
  40.         if section.settings.columns == '4'
  41.           assign image_sizes = '(min-width: 1520px) 333px, (min-width: 1024px) calc(25vw - 47px), (min-width: 750px) calc(33.33vw - 51px), calc(100vw - 32px)'
  42.         else
  43.           assign image_sizes = '(min-width: 1520px) 456px, (min-width: 750px) calc(33.33vw - 51px), calc(100vw - 32px)'
  44.         endif
  45.       %}
  46.       {% for article in blog.articles %}
  47.         {% liquid
  48.           assign scale = 0.6
  49.           assign is_horizontal = false

  50.           assign image_loading = 'lazy'
  51.           assign image_fetch_priority = 'auto'
  52.           if article.image
  53.             assign image_index = image_index | plus: 1

  54.             if image_index <= eager_image_count
  55.               assign image_loading = 'eager'
  56.             endif

  57.             if image_index == 1
  58.               assign image_fetch_priority = 'high'
  59.             endif
  60.           endif
  61.         %}
  62.         {% # Keep the mobile breakpoint in the style block below in sync with the responsive image sizes above. %}
  63.         {% style %}
  64.           [data-blog-index="{{ section.id }}-{{ forloop.index }}"] {
  65.             --blog-post-card-scale: {{ scale }};
  66.           }

  67.           @media screen and (max-width: 749px) {
  68.             [data-blog-index="{{ section.id }}-{{ forloop.index }}"] {
  69.               --blog-post-card-scale: 0.5;
  70.             }
  71.           }
  72.         {% endstyle %}
  73.         <div
  74.           class="blog-post-item"
  75.           data-testid="blog-post-item"
  76.           data-blog-index="{{ section.id }}-{{ forloop.index }}"
  77.         >
  78.           {% content_for 'block',
  79.             id: 'static-blog-post-card',
  80.             type: '_blog-post-card',
  81.             article: article,
  82.             image_sizes: image_sizes,
  83.             image_loading: image_loading,
  84.             image_fetch_priority: image_fetch_priority,
  85.             is_horizontal: is_horizontal
  86.           %}
  87.         </div>
  88.       {% endfor %}
  89.     </div>
  90.   </div>
  91. </blog-posts-list>

  92. {% stylesheet %}
  93.   /**
  94.    * Blog posts page layout
  95.    */
  96.   .blog-posts {
  97.     --page-content-width: var(--narrow-page-width);
  98.     --page-width: calc(var(--page-content-width) + (var(--page-margin) * 2));

  99.     /* Keep the responsive image sizes above in sync with this gap. */
  100.     --columns-gap: 36px;
  101.     --rows-gap: 36px;
  102.   }

  103.   /**
  104.    * Grid: 3 or 4 columns on desktop (section setting), 3 on tablet, 1 on mobile.
  105.    */
  106.   .blog-posts-container {
  107.     display: grid;
  108.     grid-template-columns: repeat(var(--desktop-columns, 3), 1fr);
  109.     gap: 1rem;
  110.     width: 100%;
  111.     column-gap: var(--columns-gap);
  112.     row-gap: var(--rows-gap);
  113.   }

  114.   /* Tablet: always 3 columns */
  115.   @media screen and (max-width: 1023px) {
  116.     .blog-posts-container {
  117.       grid-template-columns: repeat(3, 1fr);
  118.     }
  119.   }

  120.   /* Mobile: 1 column */
  121.   @media screen and (max-width: 749px) {
  122.     .blog-posts-container {
  123.       grid-template-columns: 1fr;
  124.     }
  125.   }

  126.   .blog-post-item {
  127.     grid-column: auto;
  128.     background-color: var(--color-background);
  129.   }

  130.   /**
  131.    * When there's no image, the blog post item has a border.
  132.    */
  133.   .blog-post-item {
  134.     border: 1px solid rgb(var(--color-foreground-rgb) / var(--opacity-20));
  135.     padding: 0 1rem 1rem;
  136.   }

  137.   .blog-post-item:has(.blog-post-card__image-container) {
  138.     border: none;
  139.     padding: 0;
  140.   }

  141.   /**
  142.    * Square featured images at every screen size.
  143.    */
  144.   .blog-post-item .blog-post-card__image-container {
  145.     aspect-ratio: 1 / 1 !important;
  146.     overflow: hidden;
  147.   }

  148.   .blog-post-item .blog-post-card__image-container img {
  149.     width: 100%;
  150.     height: 100% !important;
  151.     aspect-ratio: 1 / 1 !important;
  152.     object-fit: cover;
  153.   }
  154. {% endstylesheet %}

  155. {% schema %}
  156. {
  157.   "name": "t:names.blog_posts",
  158.   "blocks": [
  159.     {
  160.       "type": "@theme"
  161.     },
  162.     {
  163.       "type": "@app"
  164.     },
  165.     {
  166.       "type": "text"
  167.     },
  168.     {
  169.       "type": "icon"
  170.     },
  171.     {
  172.       "type": "image"
  173.     },
  174.     {
  175.       "type": "button"
  176.     },
  177.     {
  178.       "type": "video"
  179.     },
  180.     {
  181.       "type": "group"
  182.     },
  183.     {
  184.       "type": "spacer"
  185.     },
  186.     {
  187.       "type": "_divider"
  188.     }
  189.   ],
  190.   "disabled_on": {
  191.     "groups": ["header"]
  192.   },
  193.   "settings": [
  194.     {
  195.       "type": "select",
  196.       "id": "columns",
  197.       "label": "Columns (desktop)",
  198.       "options": [
  199.         { "value": "3", "label": "3 columns" },
  200.         { "value": "4", "label": "4 columns" }
  201.       ],
  202.       "default": "3"
  203.     },
  204.     {
  205.       "type": "color",
  206.       "id": "background_color",
  207.       "label": "t:settings.background_color",
  208.       "placeholder": "t:settings.default"
  209.     },
  210.     {
  211.       "type": "header",
  212.       "content": "t:content.padding"
  213.     },
  214.     {
  215.       "type": "range",
  216.       "id": "padding-block-start",
  217.       "label": "t:settings.top",
  218.       "min": 0,
  219.       "max": 100,
  220.       "step": 1,
  221.       "unit": "px",
  222.       "default": 0
  223.     },
  224.     {
  225.       "type": "range",
  226.       "id": "padding-block-end",
  227.       "label": "t:settings.bottom",
  228.       "min": 0,
  229.       "max": 100,
  230.       "step": 1,
  231.       "unit": "px",
  232.       "default": 0
  233.     }
  234.   ]
  235. }
  236. {% endschema %}