1

I have a Structure with my entries in. How can I hide the parent entries that contain children? I still want to show the parents children.

For example I want all the below entries bar "About" and "Services"

- Home
- About
    - Sub
    - Sub
    - Sub
- Stuff
- Services
    - Sub
    - Sub
    - Sub
- Things 

So, this is my code right now

{# Set parameters for prev/next elements list #}
{% set params = craft.entries.section('pages') %}

{# Get the prev/next elements #}
{% set prevEntry = entry.getPrev(params) %}
{% set nextEntry = entry.getNext(params) %}

Do I need to modify the params to remove the entries that are parents?

1
  • Is this only being used to navigate between the previous and next entry? Commented Jul 19, 2016 at 14:26

1 Answer 1

3

You can use the hasDescendants() method to filter out any parent entry with children. You have to use an ElementCriteriaModel with .getPrev() and .getNext().

{% set entries = craft.entries.section( 'pages' ) %}
{% set entryIds = [] %}

{% for entry in entries %}
  {% if not entry.hasDescendants() %}
    {% set entryIds = entryIds|merge( [entry.id] ) %}
  {% endif %}
{% endfor %}

{% set criteria = craft.entries.id( entryIds ) %}

{% set prevEntry = entry.getPrev( criteria ) %}
{% set nextEntry = entry.getNext( criteria ) %}

You will also need to add logic to get the first and last entry is next or previous is null.

4
  • Hmm, that seems to make no difference at all. It still gives me the next entry as being a parent entry.
    – John
    Commented Jul 19, 2016 at 15:25
  • Updated answer. Commented Jul 19, 2016 at 15:43
  • Hmm, nope, now the prev next links don't show at all. No error though!
    – John
    Commented Jul 19, 2016 at 15:51
  • Ah no, you had changed the section name to test and i didn't pick that up! Works a treat, thank you!!!!
    – John
    Commented Jul 19, 2016 at 15:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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