Skip to content
Grav 2.0 is officially stable. Read the announcement →
Archive

Selecting First first child in page collection

Started by Muut Archive 10 years ago · 1 replies · 658 views
10 years ago

Howdy,

I am working on a blog currently and would like to style a featured post (which will be the most recent) differently than the previous posts on the page. I have been toying around with how to select the first child with an if statement and then moves into a for loop over the other children posts. I have the for loop working perfectly, but I can't get it to select the first child, it just ignores it.

10 years ago

You have a couple of options.. you could use the CSS selector :first-child to select the first 'blog post' in a collection. Or you could add a class, or use a different template even to render the first blog post by:

  1. Looping over all the posts
  2. In the loop check the loop.first twig variable and see if it is true. if so, do something different. - http://twig.sensiolabs.org/doc/tags/for.html#the-loop-variable

Suggested topics

Topic Participants Replies Views Activity
Archive · by Deleted User, 9 years ago
0 1361 9 years ago
Archive · by Muut Archive, 9 years ago
2 939 9 years ago
Archive · by Muut Archive, 9 years ago
2 4069 9 years ago
Archive · by Muut Archive, 9 years ago
1 2958 9 years ago
Archive · by Muut Archive, 9 years ago
3 1123 9 years ago