Forum
Targetting the 3rd item
OK, so I have some albums set up with the Gallery app. I am spreading these across two content pages, so have added a category id so that I can filter by category for each page.
This works fine. However, on one of the pages, I want to target the 3rd item (it is a list of albums) on the page. If I use perch_item_index eq 3 then it doesn't work, because the item_index of the items on the page actually starts at 8, because it is the 8th album in total in the gallery app.
I need to be able to target the third item that is outputted to the page, not the 3rd album of all albums.... does that make sense?
Whatever you're trying to do, can you do it in the template? You could use <perch:every nth-child="3"></perch:every>
https://docs.grabaperch.com/docs/templates/conditionals/every
Am trying to do it in the template. Don't want every third one, just the first!
If you add a class or id to the parent container, you can just target the 3rd child (album) using #parent-id:nth-child(3) in the CSS itself.
I know, but I need to do stuff via Perch css won't suffice. I need to apply a background image that has been generated by perch, hence it has a dynamic file name and cannot do that via css
As Shane says perch:every is what you need.
If that isn't working we need to see your template to help.
But what if I don't want to target every 3rd item, just the first (3rd!) one. Surely perch:every would target every 3rd item
no, it uses nth-child selectors you'd have to do 3n for every third, have you tried the suggestion?
Ohhhhhh. Will try, thanks. The 'every' threw me off!
Hmm, OK so <perch:every nth-child="3"> does nowt while <perch:every nth-child="3n"> affects every 3rd item. So the every statement is working, but just not for nth-child="3" rather than 3n
Why aren't you using
perch_item_index
? That starts at 1 for each set being displayed.Well as I explained before, it seems to not start at 1 for the album list I am displaying:
It seems odd that this is the case, but I printed the item index for the items in question to test and they started at 8 not 1!
Can you give an example? I can't think how that could be.
Ahhh, I was just going to post my code and I saw the error of my ways! I was filtering for the category at the template level and hence the index was including all items not just for the relevant category.
Changed to filter at the page level now and all working fine! No idea why I didn't do it that way in the first place!
Sorry to waste your time! :/