How to display content only on the homepage?

Hi Shopify Experts,

If “Show on home page only” is selected then show on home page only. Otherwise show on all the pages including home page. How can we achieve it?

{%- if section.settings.show_announcement == true -%}
  {%- if section.settings.show_announcement_home == true -%}
    {%- if template == 'index' -%}
      Home
    {%- endif -%}
  {%- endif -%}
{%- endif -%}
{% schema %}
{
  "name": "t:sections.announcement-bar.name",
  "settings": [
    {
      "type": "checkbox",
      "id": "show_announcement",
      "default": true,
      "label": "Show announcement"
    },
    {
      "type": "checkbox",
      "id": "show_announcement_home",
      "default": false,
      "label": "Show on home page only"
    }
  ]
}
{% endschema %}

Hi @ahmedmobin ,

You can try below code:

{%- if section.settings.show_announcement == true -%}
  {%- if section.settings.show_announcement_home == true -%}
    {%- if template contains 'index' -%} 
      Home
    {%- endif -%}
  {%- else -%}
    Content All page
  {%- endif -%}
{%- endif -%}

If you feel like my answer is helpful, please mark it as a SOLUTION. Let me know if you have any further questions.

1 Like

If you need to show the same stuff on homepage and other pages, then it’s rather like this:

{% if section.settings.show_announcement %}
  {% if section.settings.show_announcement_home == false or template.name == 'index' %}
     show the announcement
  {% endif %}
{% endif %}
1 Like