📄 ページテンプレート中級
ホームページのカテゴリグリッド
メタオブジェクトで管理されたカテゴリ階層をホームページに表示するセクション。カテゴリ名の区切り文字(>)以降の部分を抽出し、サムネイル付きグリッドレイアウトで描画する。
用途
ホームページにカテゴリ一覧を階層化して表示し、ユーザーが視覚的にカテゴリを選択して遷移できるようにしたいとき。
設置場所
sections/homepage.liquid に配置し、ホームページテンプレート(templates/index.liquid)内で `{% section 'homepage' %}` として呼び出すか、テーマカスタマイザーからセクションを追加して使う。
注意点
メタオブジェクト「category_metaobject」が Shopify 管理画面で事前に定義されており、各カテゴリレコードに image フィールドと name フィールド(「親 > 子」形式)が必須。name に「>」が2つ以上含まれると子カテゴリが表示されないため、階層は1階層のみに制限する。child.image が空の場合は image_tag がエラーになるので、メタオブジェクト作成時に画像を必ず登録する。
コード
119 行 / liquid<style>
.small_grid {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(5rem, 1fr));
gap: 0.5rem;
list-style: none;
padding-left: 0;
}
.small_grid img {
border: 1px solid lightgrey;
max-width: 100%;
object-fit: cover;
}
.small_grid a {
text-decoration: none;
color: black;
}
.small_grid a:hover {
text-decoration: underline;
}
.small_grid a {
display: flex;
flex-direction: column;
align-items: center;
gap: 0.5rem;
text-align: center;
font-size: 0.8rem;
}
.category {
padding: 0.5rem;
}
.small_grid_title {
font-size: 1.25rem;
font-weight: normal;
color: #285028;
}
</style>
<h1 class="sticky-header">All Categories</h1>
{%- for block in section.blocks -%}
<div class="category">
<h1 class="small_grid_title" href="{{ block.settings.link }}">
{{ block.settings.title | split: ' > ' | last }}
</h1>
<ul class="small_grid">
{%- for child in block.settings.children_categories -%}
{%- assign child_title_parts = child.name | split: ' > ' -%}
{%- if child_title_parts.size == 2 -%}
<!-- Check if there is only one ">" -->
<li>
<a href="{{ child.system.url }}" class="prefetch">
{{ child.image | image_url: width: 100 | image_tag: alt: child.title }}
<div>{{ child_title_parts[1] }}</div>
<!-- Use the second part after the first " > " -->
</a>
</li>
{%- endif -%}
{%- endfor -%}
</ul>
</div>
{%- endfor -%}
{% schema %}
{
"name": "Homepage",
"class": "section",
"tag": "section",
"disabled_on": {
"groups": ["header", "footer"]
},
"blocks": [
{
"type": "column",
"name": "Column",
"settings": [
{
"type": "image_picker",
"id": "image",
"label": "image"
},
{
"type": "inline_richtext",
"id": "title",
"label": "title"
},
{
"type": "richtext",
"id": "description",
"label": "description text"
},
{
"type": "text",
"id": "link_label",
"label": "link label"
},
{
"type": "url",
"id": "link",
"label": "link url"
},
{
"type": "metaobject_list",
"metaobject_type": "category_metaobject",
"id": "children_categories",
"label": "children categories"
}
]
}
]
}
{% endschema %}
出典・ライセンス
- Repository:
- https://github.com/Shopify/mcliquid-theme
- License:
- MIT
このコードは Shopify 著作の MIT ライセンスソースです。 原本の著作権は Shopify が保有します。日本語訳は ALSEL によるものです。
関連項目
📄 ページテンプレート初級
ページテンプレート
Shopify の汎用ページテンプレート。ページのタイトルと本文コンテンツを Shopify 管理画面から取得して表示する基本的な構造。
📁 liquid·MIT·5 行
📄 ページテンプレート初級
リダイレクト
ヘッドレステーマで Shopify ストアフロントへのリダイレクト機能を提供するセクション。管理画面に「リダイレクト」という名称で登場し、ページから別 URL への誘導を実装する基盤となる。
📁 shopify-headless-theme·MIT·7 行
📄 ページテンプレート初級
トップページの基本テンプレート
Shopify Liquid の最小限のテンプレート例。HTML に日付変数を埋め込み、商品ページへのリンクを配置する基本構造を示す。
📁 liquid·MIT·7 行
📄 ページテンプレート初級
固定ページのテンプレート
Shopify 管理画面で作成した固定ページ(利用規約、プライバシーポリシーなど)を表示するテンプレート。ページタイトルと本文コンテンツを描画する。
📁 mcliquid-theme·MIT·8 行
📄 ページテンプレート初級
ページの基本テンプレート
固定ページ(About など)の基本レイアウト。ページタイトルとコンテンツを HTML の article 要素でマークアップし、管理画面で編集したテキストを表示する。
📁 liquid·MIT·9 行
📄 ページテンプレート初級
固定ページのテンプレート
固定ページ(ポリシー、会社概要、利用規約など)の基本レイアウト。ページタイトルと本文を中央揃えで表示する。
📁 ks-bootshop·MIT·10 行