How can I add code to the header of the homepage?

Topic summary

Goal: Add a piece of HTML to the header only on the Shopify homepage (not site‑wide).

Proposed solutions:

  • Wrap the header code in a Liquid conditional that targets the homepage (the index template). Example approach: use a condition checking if the current template is the homepage (e.g., template contains ‘index’).
  • Alternative placement: put the code in layout/theme.liquid with a condition that the template name equals “index” to ensure it runs only on the homepage.

Notes:

  • In Shopify Liquid, the template or template.name variable indicates which page template is rendering; “index” corresponds to the homepage.

Outcome: The original poster applied the suggestion successfully and confirmed it worked. The issue is resolved; no further questions or disagreements remain.

Summarized with AI on February 2. AI used: gpt-5.

Hello @NQuan088

You can add code to the header.liquid like this

{% if template contains 'index' %}
//Your code here
{% endif %}

I hope the above is useful to you.

Kind & Best regards,
GemPages Support Team

1 Like