Skip to main content
OverflowAI is here! AI power for your Stack Overflow for Teams knowledge community. Learn more
12 votes

Loop last error getting entries

Updated answer: make sure your for loop is calling .all() and {{loop.last}} should work. You'll need to use {% if loop|last %}. This has changed in Twig 3 (which i guess Craft 4 is now using). See: ...
alexr's user avatar
  • 618
7 votes
Accepted

Matrix: get the index number of a certain block

You can use a custom counter instead of using the loop.index value. {% set step = 0 %} {% for block in entry.matrixBlocks %} {% if block.type == 'stepBlockType' %} {% set step = step + ...
carlcs's user avatar
  • 36.3k
7 votes
Accepted

What is the difference between 'nav' and 'for' for looping?

for is part of the native Twig language. for is for general usage... It's the basic, general looping structure that you'll use 99% of the time. You'll find that it closely mirrors for and foreach in ...
Lindsey D's user avatar
  • 24k
6 votes
Accepted

Getting the last X number of pages in pagination

As I already wrote in my comment 3 days ago something like this should work {% paginate craft.entries.section('mySection').limit(10) as pageInfo, pageEntries %} {% for page, url in pageInfo....
Robin Schambach's user avatar
5 votes

Remove trailing comma from an output loop?

{% for category in entry.mediaType %} <b>{{ category.title }}</b>{{ "," if not loop.last }} {% endfor %}
Chuanwen Chen's user avatar
5 votes

How do I get just the first entry with a 'for' loop?

As you'll only ever be working with one entry I wouldn't bother with the for loop for the first part of your question. Just set a variable with your featured news article. Something like this (not ...
Steve Holland's user avatar
5 votes

Loop divisible by issue

Forgive me if I'm wrong... by using loop.index0 is divisible by(2) are you trying to determine if the instance is odd or even? 1st - returns false 2nd - returns true 3rd - returns false 4th - returns ...
Jamie Wade's user avatar
  • 3,644
5 votes
Accepted

How to duplicate entries and loop them without getting a Twig or deprecation error?

Instead of duplicating the entries, you could run your entries loop twice (or as many times as you want) maybe? {% for i in 0..1 %} {% for entry in entries %} {{ entry.title }} <br&...
Oli's user avatar
  • 7,495
4 votes
Accepted

Display all entry titles on one line during FOR loop

This is because you're using an html list <li> element, which by default puts each element on a new line. You could style away the newline I suppose, but why use it at all...there are a bunch ...
Jeremy Daalder's user avatar
4 votes

Loop divisible by issue

What you want is not the loop variable of the inner loop around the image, but the loop variable of the outer loop. To get to the outer loop, use loop.parent. Try this: {% if loop.parent.loop.index0 ...
Marion Newlevant's user avatar
4 votes
Accepted

Next block inside of a loop

Try doing something like: {% set nextBlock = block.getNext() %} https://craftcms.com/docs/templating/matrixblockmodel#getNext
andrew.welch's user avatar
  • 11.6k
4 votes
Accepted

Simplify Twig/JS loop code

You can select elements by class and give your html elements custom data attributes. When this is your clickable html element with the corresponding div <button data-id="dimsTarget4" class="...
Robin Schambach's user avatar
3 votes
Accepted

Check if current entry is in loop and add class

Firstly, from the Craft docs on routing: If the URI matches an entry’s or category’s URI, the section’s/category group’s template will get loaded, and the matched element will be made available ...
Steve Rowling's user avatar
3 votes
Accepted

Start a Loop at the nth Entry in a Structure and Loop to the End?

Using the offset parameter you can achieve this: {% for entry in craft.entries.section('structureSection').offset(8) %}
Siebird's user avatar
  • 373
3 votes

Loop through assets field on detail/product page

product.gallery returns an ElementCriteriaModel, which you can think of as a list of elements. product.gallery.first() will return the first element in this list. What you want is to loop over all ...
Marion Newlevant's user avatar
3 votes
Accepted

How do I group matrix blocks into batches of 4?

You could use your own variable to count the matrix blocks in your loop. The code could look like this: {% set rows = 0 %} <div class="row"> {% for event in events %} {# loop through all ...
Tom Bauer's user avatar
  • 1,327
3 votes

Can I exclude a commerce address by ID when retrieving a list of addresses?

I still haven't found a succinct way to exclude a particular address by ID within the commerce customer addresses tags, but I did come up with the following as a way to get a nice clean set of ...
jthomas's user avatar
  • 373
3 votes
Accepted

How do I loop through two sections as one set

According to the Craft docs, the section parameter takes a "section handle, an array of section handles, or a SectionModel object". So this should work: {% set entries = craft.entries.section(['press'...
Jake Dohm's user avatar
  • 766
3 votes
Accepted

Loop through all entry types, separate each group type by line rule (hr)

You can use the group filter {% set allEntries = craft.entries.section('pets').all() %} {% set allEntriesByType = allEntries|group('type') %} {% for type, pets in allEntriesByType %} All Pets ...
Robin Schambach's user avatar
3 votes

Entries from matrix should not appear in loop

You are on the right track, to “collect” the entry IDs you have to use the merge filter in your Matrix loop though. {% set highlightProfileSingleIds = [] %} {% for profile in highlightProfiles %} ...
carlcs's user avatar
  • 36.3k
3 votes
Accepted

Possible to paginate product variants in Craft Commerce 2?

product.variants returns the variants for the product as an array only. It is not like a element relationship field which returns an element query. If you want to use a element query of variants for ...
Luke Holder's user avatar
  • 6,827
3 votes

Count entries with condition

Assuming that your products are entries, and comments are related to a product through an entries field, you can use the following: {% set comments = craft.entries.section('productcomments')....
Jamie Wade's user avatar
  • 3,644
3 votes
Accepted

Getting entries excluding the first

"Measure twice, cut once" goes the saying... (Kind of the opposite here, but the same principle!) - Fetch your data once, then cut it up how you like using Twig filters. In your case you can ...
James Smith's user avatar
  • 5,284
3 votes
Accepted

`loop.last` throws 'variable is not defined when looping with a condition' error

Just to provide a slight improvement to @Matanya's solution: Instead of filtering the array of Neo blocks, you can limit your query to the blocks you actually want. This way, you're not loading blocks ...
MoritzLost's user avatar
  • 11.4k
2 votes

The twig syntax for this for loop

The code below ended up solving the issue for me. {% for row in 0..rows %}
Rockwell Rice's user avatar
2 votes

loop.last of a certain block type

To test if the current block is the last in the loop and of a certain type, you could do something like: {% if loop.last and block.type == "yourBlockType" %}yes{% endif %} or shorthand {{ loop.last ...
AbbeyDesign's user avatar
  • 1,527
2 votes
Accepted

How to get first child URL within Craft/Twig Nav tags

In that case you can put this in the href value: {{ entry.descendants.type('page').first.url ?? '' }} (Replace page with the other entry type handle(s).)
Brandon Kelly's user avatar
2 votes
Accepted

How Do I Restart an Entries Loop Once It Hits the Last Entry

You can repeat a loop with another for loop. For example, here's a loop that repeats 15 times: {% for i in 1..15 %} Loop: {{ i }}<br> {% for entry in block.homeClientQuote %} ... ...
Alex Roper's user avatar
  • 2,632
2 votes
Accepted

divisible by strange issue

You have some other bug somewhere else. Check the code pulling out entry details to be sure that it doesn't modify loop or the list you are looping on, or the current item. This code works: {% for ...
Marion Newlevant's user avatar
2 votes
Accepted

Loop through all possible options of a Dropdown-field

{% for option in entry.dropdownFieldHandle.options %} <li>{{ option }}</li> {% endfor %} That should be it :) {% for entry in craft.entries.section('sectionname') %} {% for option ...
mike-orez's user avatar
  • 111

Only top scored, non community-wiki answers of a minimum length are eligible