I'm trying to set up a sub nav from a Structure Section on my site.

Current code is:

{% set mainEntry = craft.entries.slug(craft.request.getSegment(1)).first() %}
{% set subnav = craft.entries.descendantOf(mainEntry).level(2) %}

{{ subnav.total() }}

But my when loop through sub nav below it returns nothing. I am outputting subnav.total above but it is returning 0, but there is a lot of entries in the CMS.

mainEntry gets loaded with the correct info ok, but I don't get anything in subnav.

  • Your code look right to me, mmc. What happens if you remove .level(2)? Are you not getting descendants at all?
    – carlcs
    Commented Aug 14, 2014 at 18:08
  • No I'm getting nothing at all - when I remove .level(2) {{ subnav.total() }} still output's 0
    – mmc501
    Commented Aug 15, 2014 at 5:48

1 Answer 1


Is there a chance that you're getting the another entry with the the same slug as your mainEntry? I'd try to further filter it down by adding section() for example.

As I wrote in the comments, try to loosen the second ElementCriteriaModel by removing the level parameter.

Another idea would be to use the dump() function on your mainEntry (only works with dev-mode turned on).

  • Cheers - adding section() resolved the issue - must have been another slug affecting it.
    – mmc501
    Commented Aug 15, 2014 at 9:46

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.