Answers.Body: <p>quite similar solution with the <em>one</em> function, <em>first</em> function fails with following error :  Calling unknown method: craft\elements\db\EntryQuery::first()</p>
  <pre><code>{% set rootEntry = entry.ancestors.level(1).one()    %}
          {% if entry.level ==1  %}{%  set rootEntry=entry  %} {% endif %}
          {% if rootEntry %}
              &lt;h2&gt; &lt;b&gt; &lt;a href=&quot;{{ rootEntry.url }}&quot;&gt; {{ rootEntry.title }}&lt;/a&gt;&lt;/b&gt;&lt;/h2&gt;
              {%  set pages = craft.entries().level('&gt;=2').descendantOf(rootEntry.id).all() %}
  
              &lt;ul&gt;
                  {% nav page in pages %}
                      {% set expanded = entry is defined and (page.isAncestorOf(entry) ) %}
  
                      &lt;li &gt;
                          {#     {{ page.getLink() }} {{ entry.id }}{{ page.id }}#}
                          &lt;a href=&quot;{{ page.url }}&quot; {% if expanded or page.id == entry.id %} class=&quot;font-bold&quot;{% else %} class=&quot;font-light&quot;{% endif %}&gt;{{ page.title }}&lt;/a&gt;
                          {% ifchildren %}
                              &lt;ul&gt;
                                  {% children %}
                              &lt;/ul&gt;
                          {% endifchildren %}
                      &lt;/li&gt;
                  {% endnav %}
              &lt;/ul&gt;
          {% endif %}
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-05-10T12:43:00.687Z
Answers.LastActivityDate: 2023-05-10T12:43:00.687Z
Answers.UserDisplayName: erwanpia
Answers.Body: <p>Ran into the same issues you had. Pulled this from a working site I recently built.</p>
  
  <p>Basically you have to get the topmost page. I found <code>entry.getAncestors().level(1).first()</code> works for that no matter what level you're in.</p>
  
  <p>From there, you just get everything that's underneath it. But first check to see if there's actually any children using <code>{% if topLevel.hasDescendants %}</code></p>
  
  <pre><code>       &lt;! -- local navigation --&gt;
  
          {% set topLevel = (entry.level == 1) ? entry : entry.getAncestors().level(1).first()  %}
  
          {# don't show the tree if nothing's under it #}
  
          {% if topLevel.hasDescendants %}
  
              &lt;h2&gt;{{topLevel}}&lt;/h2&gt;
              &lt;ul&gt;
              {% set subnav =  topLevel.getDescendants()  %}
  
              {% nav page in subnav  %}
              {% if page.id != entry.id %}&lt;li&gt;{{ page.getLink() }}&lt;/li&gt;{%else%}&lt;li class="current-page"&gt;{{page}}&lt;/li&gt;{%endif%}
  
                  {% ifchildren %}
                      &lt;ul class="sub-nav"&gt;
                              {% children %}
                          &lt;/ul&gt;
                  {% endifchildren %}
  
              {% endnav %}
  
              &lt;/ul&gt;
  
         {% endif %}
  
       &lt;!-- // local navigation --&gt;
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-09-14T07:37:01.583Z
Answers.Text: Thanks for the help. I had to amend the last endif in my code, there's a space in there causing an internal error. Works well.
Answers.UserDisplayName: swcarrey
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-09-14T00:20:07.217Z
Answers.LastActivityDate: 2015-10-12T17:28:45.423Z
Answers.LastEditDate: 2015-10-12T17:28:45.423Z
Answers.UserDisplayName: RitterKnight
Body: <p>I have checked the official documentation and also these pages:</p>
  
  <p><a href="https://craftcms.stackexchange.com/questions/590/how-do-you-only-show-children-of-active-parent-in-a-structure-sections-nav">How do you only show children of &quot;active&quot; parent in a Structure section&#39;s nav?</a></p>
  
  <p><a href="https://craftcms.stackexchange.com/questions/2060/how-to-output-a-structure-sub-navigation-of-current-page">How to output a structure sub-navigation of current page</a></p>
  
  <p>I still can't get this to work though. I have tried various code and currently have this (from official docs):</p>
  
  <pre><code>&lt;ul id="sub-menu"&gt;
  
  {% set pages = craft.entries.section('pages').level('&gt;1') %}
  {% nav entry in pages %}
  &lt;li&gt;
      &lt;a href="{{ entry.url }}"&gt;{{ entry.title }}&lt;/a&gt;
          {% ifchildren %}
              &lt;ul&gt;
                 {% children %}
              &lt;/ul&gt;
           {% endifchildren %}
  &lt;/li&gt;
  {% endnav %}
  
  &lt;/ul&gt;
  </code></pre>
  
  <p>This outputs all the children whenever I'm on any top level page. My structure is a little different though, like so (shortened for brevity):</p>
  
  <pre><code>Home
  About (structure: 'pages', entry type: 'aboutPage')
    - Aims (structure: 'pages', entry type: 'aboutPage')
    - Governance (structure: 'pages', entry type: 'aboutPage')
  Services (structure: 'pages', entry type: 'servicesPage')
    - Residential (structure: 'pages', entry type: 'servicesPage')
    - Homecare (structure: 'pages', entry type: 'servicesPage')
  Resources (structure: 'pages', entry type: 'resourcePage')
    - Health (structure: 'pages', entry type: 'resourcePage')
    - External (structure: 'pages', entry type: 'resourcePage')
  </code></pre>
  
  <p>You can see I have a tree which uses a single 'pages' structure to keep everything obvious in the admin section. I then just create an entry, choose the parent and choose the entry type to add content.</p>
  
  <p>I use a structure 'mainMenu' to add links manually to the main menu using a related entry field. So I have:</p>
  
  <p>Home - About - Services - Resources</p>
  
  <p>As top level pages. When I'm on a top level page, I want to show children of this current page and to hide the unordered list element when there are no children of the current page, and I'm stuck.</p>
  
  <p>Can anyone point me in the right direction? Thanks in advance.</p>
  
Tags: <navigation><children>
Title: Show only children of current page in submenu
ThreadText: # Show only children of current page in submenu
  
  (Asked by: username_0 on 2015-09-13)
  
  I have checked the official documentation and also these pages:
  
  [How do you only show children of "active" parent in a Structure section's nav?](https://craftcms.stackexchange.com/questions/590/how-do-you-only-show-children-of-active-parent-in-a-structure-sections-nav)
  
  [How to output a structure sub-navigation of current page](https://craftcms.stackexchange.com/questions/2060/how-to-output-a-structure-sub-navigation-of-current-page)
  
  I still can't get this to work though. I have tried various code and currently have this (from official docs):
  
  ```
  <ul id="sub-menu">
  
  {% set pages = craft.entries.section('pages').level('>1') %}
  {% nav entry in pages %}
  <li>
      <a href="{{ entry.url }}">{{ entry.title }}</a>
          {% ifchildren %}
              <ul>
                 {% children %}
              </ul>
           {% endifchildren %}
  </li>
  {% endnav %}
  
  </ul>
  
  ```
  
  This outputs all the children whenever I'm on any top level page. My structure is a little different though, like so (shortened for brevity):
  
  ```
  Home
  About (structure: 'pages', entry type: 'aboutPage')
    - Aims (structure: 'pages', entry type: 'aboutPage')
    - Governance (structure: 'pages', entry type: 'aboutPage')
  Services (structure: 'pages', entry type: 'servicesPage')
    - Residential (structure: 'pages', entry type: 'servicesPage')
    - Homecare (structure: 'pages', entry type: 'servicesPage')
  Resources (structure: 'pages', entry type: 'resourcePage')
    - Health (structure: 'pages', entry type: 'resourcePage')
    - External (structure: 'pages', entry type: 'resourcePage')
  
  ```
  
  You can see I have a tree which uses a single 'pages' structure to keep everything obvious in the admin section. I then just create an entry, choose the parent and choose the entry type to add content.
  
  I use a structure 'mainMenu' to add links manually to the main menu using a related entry field. So I have:
  
  Home - About - Services - Resources
  
  As top level pages. When I'm on a top level page, I want to show children of this current page and to hide the unordered list element when there are no children of the current page, and I'm stuck.
  
  Can anyone point me in the right direction? Thanks in advance.
  
  **Tags:** navigation, children
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_2 on 2015-09-14. Score: 6 | Accepted answer
  
  Ran into the same issues you had. Pulled this from a working site I recently built.
  
  Basically you have to get the topmost page. I found `entry.getAncestors().level(1).first()` works for that no matter what level you're in.
  
  From there, you just get everything that's underneath it. But first check to see if there's actually any children using `{% if topLevel.hasDescendants %}`
  
  ```
         <! -- local navigation -->
  
          {% set topLevel = (entry.level == 1) ? entry : entry.getAncestors().level(1).first()  %}
  
          {# don't show the tree if nothing's under it #}
  
          {% if topLevel.hasDescendants %}
  
              <h2>{{topLevel}}</h2>
              <ul>
              {% set subnav =  topLevel.getDescendants()  %}
  
              {% nav page in subnav  %}
              {% if page.id != entry.id %}<li>{{ page.getLink() }}</li>{%else%}<li class="current-page">{{page}}</li>{%endif%}
  
                  {% ifchildren %}
                      <ul class="sub-nav">
                              {% children %}
                          </ul>
                  {% endifchildren %}
  
              {% endnav %}
  
              </ul>
  
         {% endif %}
  
       <!-- // local navigation -->
  
  ```
  
  > **Comments:**
  > **username_0**: Thanks for the help. I had to amend the last endif in my code, there's a space in there causing an internal error. Works well.  
  
  ---
  
  ### Answer by username_1 on 2023-05-10. Score: 1
  
  quite similar solution with the *one* function, *first* function fails with following error : Calling unknown method: craft\elements\db\EntryQuery::first()
  
  ```
  {% set rootEntry = entry.ancestors.level(1).one()    %}
          {% if entry.level ==1  %}{%  set rootEntry=entry  %} {% endif %}
          {% if rootEntry %}
              <h2> <b> <a href="{{ rootEntry.url }}"> {{ rootEntry.title }}</a></b></h2>
              {%  set pages = craft.entries().level('>=2').descendantOf(rootEntry.id).all() %}
  
              <ul>
                  {% nav page in pages %}
                      {% set expanded = entry is defined and (page.isAncestorOf(entry) ) %}
  
                      <li >
                          {#     {{ page.getLink() }} {{ entry.id }}{{ page.id }}#}
                          <a href="{{ page.url }}" {% if expanded or page.id == entry.id %} class="font-bold"{% else %} class="font-light"{% endif %}>{{ page.title }}</a>
                          {% ifchildren %}
                              <ul>
                                  {% children %}
                              </ul>
                          {% endifchildren %}
                      </li>
                  {% endnav %}
              </ul>
          {% endif %}
  
  ```
  
  
  ---
  
  