Skip to content

ニュースの動的出力

カテゴリーごとの表示方法1

{% assign count = 0 %}
{% for category in information_categories %}
  {% if category.slug == 'news' %}  // ニュースカテゴリー管理のスラッグ名を指定
    {% assign informations_sort = category.informations | sort: 'published_at' | reverse %}
    {% for information in informations_sort | limit: 3 %} // ここで最大表示数を指定
      <li>
        {{ information.published_at | date: '%Y年%m月%d日' }}<br>
        {{ information.title }}<br>
        <a href="/shop/information/{{ information.slug }}">詳細をみる</a>
      </li>
      {% assign count = count | plus: 1 %}
    {% endfor%}
  {% endif %}
{% endfor %}
{% if count == 0 %}
<li>
  現在のお知らせはありません。
</li>
{% endif %}

カテゴリーごとの表示方法2

{% assign news_num = 0 %}
{% assign news_cat = '' %}
{% for category in information_categories %}
  {% if category.slug == 'ニュースのカテゴリー名' %}
    {% assign news_cat = category %}
    {% for information in category.informations %}
      {% unless information.state == 'inactive' %}
        {% assign news_num = news_num | plus: 1 %}
      {% endunless %}
    {% endfor %}
  {% endif %}
{% endfor %}
{% if news_num > 0 %}
<h2>
  ニュース一覧
</h2>
{% assign show_count = 0 %}
<ul>
  {% assign _informations = news_cat.informations | reverse %}
  {% for information in _informations %}
    {% unless information.state == 'inactive' %}
      {% assign show_count = show_count | plus: 1 %}
      <li>
        <a href="/shop/information/{{ information.slug }}">
          <p>{{ information.published_at | date: '%Y/%m/%d' }}</p>
          <p>{{ information.title }}</p>
        </a>
      </li>
    {% endunless %}
    {% if show_count >= 3 %}
      {% break %}
    {% endif %}
  {% endfor %}
  {% if show_count >= 3 %}
    <div>
      <a href="/shop/information_categories/ニュースのカテゴリー名" class="bl_info_button_target">お知らせ一覧へ</a>
    </div>
  {% endif %}
</ul>
{% endif %}