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?
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.