{# use a namespace so that we can set stuff within a for loop #} {% set backlink = namespace(target=None) %} {% if page.is_index %} {% if page.parent.parent %} {# section homepages go to their grandparent #} {% set backlink.target = page.parent.parent %} {% else %} {# top-level section homepages go to the site home #} {% set backlink.target = nav.homepage %} {% endif %} {% else %} {% if page.parent %} {# regular pages go to their parent #} {% set backlink.target = page.parent %} {% else %} {# top-level pages go to the site home #} {% set backlink.target = nav.homepage %} {% endif %} {% endif %} {# if backlink has an index page, target that instead #} {% if backlink.target.is_section %} {% for backlink_sibling in backlink.target.children if backlink_sibling.is_index %} {% set backlink.target = backlink_sibling %} {% endfor %} {% endif %} 🡐 {{ backlink.target.title }}