0

I have setup a few different navigation lists through the Menus 0.9 plugin by Familiar. Within my website, I have setup several different site sections all through the same structure called, "General". They all share the same page template.

I would like to run a check to see if the page is "About" or is a child of "About" (all within the "General Structure"). If it is "About" or a child of "About", I would like to server it a specific sub-nav.

Here's the code to run that subnav:

{% nav node in craft.menus.getNodes('aboutSubnav') %}
<li><a href="{{node.link}}">{{ node.title }}</a></li>
{% endnav %}

How do I do the lookup to see what page it is? I tried the following code, but it did not work:

{% if entry in craft.entries.section('general').descendantOf(42) %}
 {% nav node in craft.menus.getNodes('aboutSubnav') %}
 <li><a href="{{node.link}}">{{ node.title }}</a></li>
 {% endnav %}
{% endif %}
2

The containment operator in does have problems working with whole element models. I'd try and modify your conditional to compare entry IDs instead:

{% if entry.id in craft.entries.section('general').descendantOf(42).ids() %}
    ...
{% endif %}

To include that parent page ID to that array you would do that with the merge filter:

{% set aboutPagesIds = craft.entries.section('general').descendantOf(42).ids() %}
{% set aboutPagesIds = aboutPagesIds|merge([42]) %}
  • Thank you, this worked, but is there a way to check for the parent as well as the descendant in the same if statement? It would include {% if entry.id == 174 %} and {% if entry.id in craft.entries.section('general').descendantOf(42).ids() %} together? – jsilton May 11 '15 at 11:06
  • @jsilton I updated my answer. You could also combine the conditionals with or, but I like the above method better. – carlcs May 11 '15 at 11:16
1

I was able to make some progress here. Below is the proper navigation aligned for the parent entries, but not the children.

{% if entry.id == 174 %}
 {% nav node in craft.menus.getNodes('ticketsSubnav') %}
 <li><a href="{{node.link}}">{{ node.title }}</a></li>
 {% endnav %}
{% endif %}

{% if entry.id == 53 %}
 {% nav node in craft.menus.getNodes('visitSubnav') %}
 <li><a href="{{node.link}}">{{ node.title }}</a></li>
 {% endnav %}
{% endif %}

{% if entry.id == 42 %}
 {% nav node in craft.menus.getNodes('aboutSubnav') %}
 <li><a href="{{node.link}}">{{ node.title }}</a></li>
 {% endnav %}
{% endif %}

{% if entry.id == 57 %}
 {% nav node in craft.menus.getNodes('supportSubnav') %}
 <li><a href="{{node.link}}">{{ node.title }}</a></li>
 {% endnav %}
{% endif %}

It's still not the answer yet, but closer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.