1

I'm trying to load an entry based on the following url:

/artists/nameOfArtist/nameOfCategory/myEntry/thumb

This URL follows the structure of my entries: Artist > Category > Entry

So, I'm thinking that something like this should work:

{% set project = craft.request.getSegment(-2) %} 

- This gets me the variable from 'myEntry' segment.

{% set artistParent = craft.request.getSegment(-4) %} 

- This gets me the variable from 'nameOfArtist' segment.

Then, I want to find the 'project' entry that lives under 'nameOfArtist'.

{% set entry = craft.entries.slug(project).descendantOf(artistParent).first %}

but this is not working. What am I missing? Or is there a better way of doing this?

  • I take it the section is a structure (not a channel) called "Artists"? And the category is a category and not just another structure entry? – Ryan Sep 15 '17 at 0:14
  • Both, Artist and Category are structure entries. Category is child of Artist. – SoHo Sep 16 '17 at 3:45
  • Ok cool, then does my answer below work for you? – Ryan Sep 16 '17 at 6:15
  • @Ryan I still need to figure this out. Will post an update. – SoHo Sep 19 '17 at 13:23
1

I think it's because you're trying to cram too much into your entry variable. For one, .slug() is going to return an entry with that slug, so I think that's a bit of a full stop. For two, .descendantOf() accepts an EntryModal object, but you're feeding it a uri/slug (ie. name-of-artist).

Try something like this:

{% set artistEntry = craft.entries.slug(artistParent).first %}
{% set projectEntry = craft.entries.descendantOf(artistEntry).first %}

Keep in mind that .descendantOf() only works with structures (not channels), so I'm assuming that's what you're working with. Also, you may need to specify how far down the lineage to go with .descendantDist().

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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