1

I am looking for a way to exclude certain pages from the navigation. I know it must be quite easy, but nothing seems to work for me at the moment.

So I've setup a lightswitch field "exclude from navigation" (cf_hideFromNav)

Problem is that I want the navigation to start only from a certain slug:

{% set topEntry = craft.entries.section('pages').depth(1).slug('club').first() %}
{% set descendants = topEntry.getDescendants()  %}              
<ul>
    {% nav entry in descendants %}
        <li>
            {{ entry.title }} 
            {% ifchildren %}<ul>{% children %}</ul>{% endifchildren %}
        </li>
    {% endnav %}
</ul>

how could I incorporate the cf_hideFromNav in this equation?

any help appreciated!
cheers
stefan

outline4
  • 595
  • 3
  • 14

1 Answers1

2

Use a descendantOf parameter with a new ElementCriteriaModel instead of the getDescendants method on your entry model and you can add additional parameters. In your case add cf_hideFromNav as a parameter:

{% set topEntry = craft.entries.section('pages').slug('club').first() %}
{% set navItems = craft.entries.section('pages').descendantOf(topEntry).cf_hideFromNav('0') %}

{% nav navItem in navItems %}
    ...
{% endnav %}

You could also use the parameters id or slug to exclude certain entries:

{% set topEntry = craft.entries.section('pages').slug('club').first() %}
{% set navItems = craft.entries.section('pages').descendantOf(topEntry).id('and, not 23, not 40, not 12') %}

The descendantOf parameter also takes IDs. This would allow you to do this all with just a single ElementCriteriaModel:

{% set navItems = craft.entries.section('pages').descendantOf('1').id('and, not 23, not 40, not 12') %}
carlcs
  • 36,220
  • 5
  • 62
  • 139
  • hi, that was exactly what I was after! but to my surprise it's not working! – outline4 Aug 27 '14 at 14:50
  • this works: {% set navItems = craft.entries.section('pages').descendantOf(topEntry).cf_hideFromNav('1') %} -> it only shows the navItems that should not be displayed... but the other way round it doesn't work... bug? – outline4 Aug 27 '14 at 14:51
  • 1
    but this works: {% set navItems = craft.entries.section('pages').descendantOf(topEntry).cf_hideFromNav('not 1') %} -> strange! – outline4 Aug 27 '14 at 14:52
  • 1
    Nice you got it working, @outline4! Lightswitches write 0 to the DB if not active, so it's in fact strange! – carlcs Aug 27 '14 at 15:05