Menu item active when the page is unpublished/deleted

Hello,

I’d like to ask whether it’s possible to automatically remove or hide a menu item when the page it links to either doesn’t exist or is set to hidden.

To give more context: when adding a menu item via Content > Menus > [Menu Name] > Add menu item and linking it to a specific page, everything works as expected while the page is active. However, if that page is later deleted or set to hidden, the menu item remains visible and leads to an empty or broken link.

Would it be possible to detect the page’s status and prevent the menu item from appearing on the storefront when the page is unavailable?

Hey @Jonida_Mersinllari, if you’re familiar with Liquid, you should be able to do this using the link object for deleted pages: Liquid objects: link

You’d want to use something like this in your header.liquid file:

{% for link in linklists.main-menu.links %}
  {% if link.type == 'http_link' or link.object != blank %}
    <a href="{{ link.url }}">{{ link.title | escape }}</a>
  {% endif %}
{% endfor %}

For hidden pages, I don’t believe there is a Liquid-native way to detect that status, so those menu items would still need to be removed manually from the Admin for now. Let me know if that works or if I can clarify anything more on our end here. Hope this helps!

you can try this. find and update this in header.liquid

{% for link in linklists.main-menu.links %}
{% assign skip = false %}

{% if link.type == ‘page_link’ %}
{% assign linked_page = pages[link.object.handle] %}
{% if linked_page == blank or linked_page.published_at == blank %}
{% assign skip = true %}
{% endif %}
{% endif %}

{% unless skip %}
{{ link.title }}
{% endunless %}
{% endfor %}