Discuss and resolve questions on Liquid, JavaScript, themes, sales channels, and site speed enhancements.
Hey y'all!
I'm working on a tweak to our theme and I need to combine the main nav menu Linklist with another menu Linklist, depending on if the customer is logged in or not. Basically:
or
Combining them into a single Linklist would solve an awful lot of headaches as far as the rest of the logic and menu-building goes. Is there any way to do this? I can successfully generate a conditional list of handles, but I'm stuck on this one bit. TIA
Solved! Go to the solution
This is an accepted solution.
Find the file where your main navigation is coded, usually located in Sections/header.liquid or Snippets/navigation.liquid.
This is some idea, you can customize it for your needs.
{% assign main_menu = linklists['main-menu'] %}
{% assign logged = linklists['logged'] %}
{% assign guest = linklists['guest'] %}
<nav class="main-nav">
<ul>
<!-- Loop through main menu items -->
{% for link in main_menu.links %}
<li><a href="{{ link.url }}">{{ link.title }}</a></li>
{% endfor %}
<!-- Check if the customer is logged in -->
{% if customer %}
<!-- Loop through extra menu items if customer is logged in -->
{% for link in logged.links %}
<li><a href="{{ link.url }}">{{ link.title }}</a></li>
{% endfor %}
{% else %}
<!-- Loop through guest menu items if customer is not logged in -->
{% for link in guest.links %}
<li><a href="{{ link.url }}">{{ link.title }}</a></li>
{% endfor %}
{% endif %}
</ul>
</nav>
This is an accepted solution.
Find the file where your main navigation is coded, usually located in Sections/header.liquid or Snippets/navigation.liquid.
This is some idea, you can customize it for your needs.
{% assign main_menu = linklists['main-menu'] %}
{% assign logged = linklists['logged'] %}
{% assign guest = linklists['guest'] %}
<nav class="main-nav">
<ul>
<!-- Loop through main menu items -->
{% for link in main_menu.links %}
<li><a href="{{ link.url }}">{{ link.title }}</a></li>
{% endfor %}
<!-- Check if the customer is logged in -->
{% if customer %}
<!-- Loop through extra menu items if customer is logged in -->
{% for link in logged.links %}
<li><a href="{{ link.url }}">{{ link.title }}</a></li>
{% endfor %}
{% else %}
<!-- Loop through guest menu items if customer is not logged in -->
{% for link in guest.links %}
<li><a href="{{ link.url }}">{{ link.title }}</a></li>
{% endfor %}
{% endif %}
</ul>
</nav>
Thanks!!
I'm actually doing exactly that on portions of the code already, but the theme I'm working with (Pursuit from Mile High Themes) has many, many expansive "for" statements in header.liquid to build various versions of the menus. To duplicate that logic with every statement will balloon the size of the file (not to mention being a major PITA), so I'm hoping for a way to combine the menus into a single Linklist before the rest of the page renders.
I was able to apply the above code without too much fuss -- I saw all the instances of a Linklist variable in the code and balked, but in reality it wasn't painful at all. I appreciate your help, @Tal19 !
Hey Community! As we jump into 2025, we want to give a big shout-out to all of you wh...
By JasonH Jan 7, 2025Hey Community! As the holiday season unfolds, we want to extend heartfelt thanks to a...
By JasonH Dec 6, 2024Dropshipping, a high-growth, $226 billion-dollar industry, remains a highly dynamic bus...
By JasonH Nov 27, 2024