Custom content in themes

Read a store's custom content entries, let merchants pick them in sections, declare the types your theme needs, and design entry pages.

Custom content (metaobjects) are a store's own content types — a Designer, a Size chart, an FAQ — each with fields of any metafield type, and entries of them. An entry is written once and linked from products, collections, pages and other entries through custom data (metafields of type metaobject_reference). Only active entries of active types reach a theme: a draft or deleted entry reads as null and is left out of lists. Types an app owns reach a theme only when the app declares them storefront visible; entries of any other app type read as null and are left out, and they have no web pages.

Read an entry#

An entry reads as one map wherever it appears: its own facts and each field of its type by key, typed like a metafield value of that type (rich text as HTML, files as file maps, references as the referenced item).

Property

Meaning

id, handle

The entry's id and handle (unique within its type).

type

The type's handle.

display_name

The display field's value in the shopper's language, else the handle.

url

The entry's page in the shopper's language and country, or null when the type has no web pages.

seo.title, seo.description

The entry's SEO text, or null.

fields

Every field of the type in order, as {key, name, type, value}: for showing an entry generically.

updated_at_iso

When the entry last changed (RFC 3339).

<field key>

That field's value, null when empty.

vascula
{% assign designer = product.metafields.custom.designer %}
{% if designer %}
  <aside class="designer">
    {% if designer.photo %}<img src="{{ designer.photo.url | image_url: width: 240 }}" alt="{{ designer.name }}">{% endif %}
    <h3>{% if designer.url %}<a href="{{ designer.url }}">{{ designer.display_name }}</a>{% else %}{{ designer.display_name }}{% endif %}</h3>
    {{ designer.bio }}
  </aside>
{% endif %}

References inside entries (a Lookbook pointing at Designers) resolve at most three levels deep from where the theme starts reading; deeper references read as null. All references a page reads are fetched together in one read, and a page resolves at most 256 distinct entries.

Field keys id, handle, type, url, display_name, seo, fields and updated_at are reserved for the entry's own facts.

List a type's entries#

metaobjects.<type handle> (ctx.metaobjects) is a type's first 50 active entries by display name, with has_more and total, on any page. Declare metaobjects in the section's ctx_needs. Each type is read only when the template first uses it.

vascula
{% assign faq = metaobjects.faq %}
{% for item in faq.entries %}
  <details>
    <summary>{{ item.question }}</summary>
    {{ item.answer }}
  </details>
{% endfor %}
{% if faq.has_more and faq.url %}<a href="{{ faq.url }}">{{ "faq.all" | t }}</a>{% endif %}

Let merchants pick entries#

Two section and block setting types pick custom content, pinned to one type with metaobject_type (a type the theme declares): metaobject (one entry) and metaobject_list (several, in order; max from 1 to 50, default 50). The stored value is the entry id (a list of ids); the setting reads as the entry map (null for a draft or deleted entry; such entries drop out of a list). These settings take no default, and a theme's templates never carry picks: entries belong to each store.

json
{"id": "designers", "type": "metaobject_list", "metaobject_type": "designer", "max": 12,
 "editor": {"label": "designers", "group": "content"}}

Declare the types your theme needs#

Declare every custom content type your sections read in config/metaobjects.json, beside config/metafields.json (theme contract rule metaobjects.declared). The upload and Theme Store checks refuse a theme whose sections read ctx.metaobjects.<type>, pick with a metaobject_type, or read a field on the entries of a for loop over ctx.metaobjects.<type>.entries, that the file does not declare.

json
[
  {
    "handle": "size_chart",
    "name": "Size chart",
    "display_field": "name",
    "fields": [
      {"key": "name", "name": "Name", "type": "string", "required": true},
      {"key": "table", "name": "Table", "type": "rich_text", "translatable": true},
      {"key": "fit_note", "name": "Fit note", "type": "multi_line_text", "translatable": true}
    ],
    "sample_entries": [
      {"handle": "regular-fit", "fields": {"name": "Regular fit", "table": "<table><tr><th>Size</th><th>Chest</th></tr><tr><td>M</td><td>96–101 cm</td></tr></table>"}}
    ]
  },
  {
    "handle": "designer", "name": "Designer", "display_field": "name",
    "web_pages": true, "address": "designer", "schema_kind": "person",
    "fields": [
      {"key": "name", "name": "Name", "type": "string", "required": true},
      {"key": "bio", "name": "Bio", "type": "rich_text", "translatable": true},
      {"key": "mentor", "name": "Mentor", "type": "metaobject_reference", "metaobject_type": "designer"}
    ]
  }
]

Field

Meaning

handle

The type's handle: lowercase letters, numbers, hyphens and underscores, starting with a letter, at most 64 characters.

name, description

Shown to merchants.

display_field

A single line text field used as each entry's name.

fields

1 to 40 fields: key, name, type (any metafield type), required, choices for selects, translatable, searchable, and metaobject_type for a metaobject_reference field (a type declared in the same file).

web_pages, address, schema_kind

Whether the type gets entry pages, their address (made from the handle when omitted) and their structured data kind (person, place, faq_page or none). If the address is already used by a page, another type or the store's permalinks, or a redirect already starts there, the type is created (or kept) without web pages, shows requested_address and address_conflict, and Store Admin asks the merchant to choose an address; the same applies when a theme update first declares web pages for an existing type. Once a type has an address, the merchant owns it: later theme versions never change or remove the address or the web page setting, and removing or reinstalling the theme keeps them.

sample_entries

Up to 10 entries per type (handle, fields) without references or files.

A theme declares up to 20 types. Installing or updating the theme creates the types, or adopts a store type with the same handle whose fields are compatible (every declared field it has is of the declared type; missing fields are added, and the fields it has take the declared translatable and searchable settings). An update adds newly declared fields and never removes one. A conflicting field, or a type an app owns, stops the installation and names it. The theme then manages the types' fields and handles; the merchant can change a theme type's name, description, display field and web pages (address included), and the entries are the merchant's. When the theme is removed its types stay and become the merchant's; installing it again takes them back.

To link entries from products or other items, declare the custom data field in config/metafields.json with type metaobject_reference (one entry) or list.metaobject_reference and metaobject_type, the handle of the type it points to: a type in config/metaobjects.json, or one the store already has. Installing the theme creates the types first, then the field pinned to its type; an existing field with the same address and type that points to the same type is adopted. Sections then read it like any custom data, for example product.metafields.custom.size_chart.table.

json
[
  { "owner_type": "product", "namespace": "custom", "key": "size_chart",
    "type": "metaobject_reference", "metaobject_type": "size_chart", "name": "Size chart" }
]

Sample entries show in the theme editor preview while a type has no active entries, and the merchant can add them to the store in one click (Store API: Add a theme's sample entries). Nothing is written to the store without that click.

Entry pages and list pages#

A type with web pages gives each active entry a page at /<type address>/<entry handle> and the type a list page at /<type address>, with the store's language and country prefixes; the address can differ per language. An entry page renders with the template metaobject__<type handle> when the theme has one, else metaobject; a list page with metaobject_list__<type handle>, else metaobject_list. A theme with neither gets the platform's own layout: the entry's name and each field under its name (tf-metaobject classes), or a linked list of entries (tf-metaobject-list).

On an entry page metaobject is the entry; on a list page metaobject_list is {handle, name, url, entries, pagination} with 24 entries a page. Declare metaobject or metaobject_list in ctx_needs. The platform sets the page title and description (the entry's SEO text, else its first text field), canonical and hreflang links, the sitemap entries, breadcrumbs (ctx.breadcrumbs and BreadcrumbList) and the structured data the type picks: a Person, a Place, an FAQPage (the entry's name as the question, its first text as the answer) or none. A draft or deleted entry, a type without web pages and a list page past the last answer with the theme's 404 page once redirects are checked.

Menu items of type metaobject (an entry) and metaobject_list (a type's list page) carry the current URL in the shopper's language and target = {id, slug, metaobject_type}. An item to a draft or deleted entry, or to a type without web pages, is left out, or kept as a heading when it has children.