Skip to main content
13 votes
Accepted

Total sum within loop

To do math in twig, you will need to use the set tag to do assignments, and the + operator to do addition. When doing math inside a for loop, you will need to keep in mind that loops are scoped in ...
Marion Newlevant's user avatar
11 votes
Accepted

Display entries if Field Date is past Now

You can combine a for and if statement in twig, so you don't necessarily need to use search. Something like this might get you a bit closer: {% for entry in craft.entries.section('event') if entry....
Steve Holland's user avatar
6 votes
Accepted

Check and remove repeat entries from loop

I'd probably try to filter those out in your initial query, but if you've already got them, something like this should work: {% set existingIds = [] %} {% for event in events %} {% if event.id ...
Brad Bell's user avatar
  • 67.5k
6 votes
Accepted

Load More Entries Button

The Sprig plugin will save you a lot of trouble, and dynamically loading more entries is a perfect use-case for it. If you prefer to use jQuery, then by all means do so, but keep in mind that it is ...
Ben Croker's user avatar
  • 7,536
5 votes

Set dynamic class in for Loop

I can't comment yet to reply directly to Mats Mikkel Rummelhoff's comment above, but just in case someone comes across this and is looking to alternate styles instead of basing it on the loop.index ...
MalTek's user avatar
  • 116
5 votes
Accepted

Is there a better way than stacked if else

There is certainly a lot of repeated code here so we can definitely improve on that! There are so many ways you could handle your Twig templates but this is how I would do it: <div id="slider-with-...
Jamie Wade's user avatar
  • 3,644
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
Accepted

For loop does not display all entries in section

Craft has a default limit of a 100 elements returned via an ElementCriteriaModel. To pull all elements, you'll need to explicitly set limit to null: {% for post in craft.entries.section('posts')....
Mats Mikkel Rummelhoff's user avatar
4 votes
Accepted

Image(r) transform and for loop

When using the for loop you don't need to set image as it is set in the for loop. You will also need to define transformedImage on every iteration, so move it within the for loop. Give the below a go,...
Fyrebase's user avatar
  • 735
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

Load More Entries Button

Here's a cut-down version of what I'm using on https://cld.agency/showcase The quick example below uses jQuery, but I have a more complete updated version using vanilla JS here: https://github.com/cld-...
James Smith's user avatar
  • 5,344
3 votes

Display entries if Field Date is past Now

Craft has changed since the above answer was given. The now object must be a part of the string, rather than concatenated as an object. so instead of: {% set futureEntries = craft.entries.section('...
ToddPadwick's user avatar
3 votes
Accepted

How do I make my variable available inside an assets loop?

Your problem is a scoping issue. You've defined theUrl inside the scope of a for loop, so it only ever lives inside of that loop. You can do this to have it exist outside of the loop, too. {% set ...
Brad Bell's user avatar
  • 67.5k
3 votes
Accepted

Streamlining a loop with a bunch of conditions

I'm not sure if this is better or not but one thing I'd do here is to set some defaults in my include. This also means you don't need to worry about checking whether it's defined or not. You're just ...
Jamie Pittock's user avatar
3 votes

Get future multi-day event entries with custom date field

I think the simplest solution would be to make the eventEndDate field required even for single day events. Then you could do something like this: {% for event in craft.entries({ section:'events', ...
Alex Roper's user avatar
  • 2,642
3 votes

Sort entry titles alphabetically excluding 'The'?

According to SUBSTRING, the first character starts at 1. So if you wanted to start at the position immediate after The (with one space), you'd need to start at position 5. Just did a test locally ...
Brad Bell's user avatar
  • 67.5k
3 votes
Accepted

loop.index is not returning correct number inside nested loop with inline if conditional

I'm guessing you want to display the loop index of the outer for loop. To do that, Twig provides a loop.parent context. {% for order in orders %} {% for refund in order.transactions if refund....
Brad Bell's user avatar
  • 67.5k
3 votes
Accepted

Add conditional in a for loop

{% for type in entry.buildType %} {{ type }} {% if not loop.last %}/{% endif %} {% endfor %} See: https://twig.symfony.com/doc/2.x/tags/for.html#the-loop-variable
Luke Holder's user avatar
  • 6,837
3 votes

Alternating Forloop items across Content Blocks

In the second inner loop, you need to add the number of items in the first panel to the current index to get a consistent count. Might be an off-by-one-error in the following example, make sure to ...
MoritzLost's user avatar
  • 11.5k
2 votes

Can't get checkboxfield values to show

If you want a comma-separated list of selected options, try this. {% for option in offer.offerMinimumPrice.options|filter(v => v.selected) %} {{ option.label }}{% if loop.last==false %}, {% ...
Critical Mash's user avatar
2 votes

Total sum within loop

You could use some twig math, found at the bottom of the page here: Twig Templating Not sure without seeing the rest of your code, but you could do something like this: {% set totals = [3,4]%} {% ...
AbbeyDesign's user avatar
  • 1,527
2 votes
Accepted

Can't access data in Matrix field

You're close! The relatedContent field is an ElementCriteriaModel, so you need to loop over it (or otherwise treat it somewhat like an array) to get to the entries. The matrix field is also an array ...
Marion Newlevant's user avatar
2 votes
Accepted

Error in case of nested for loops

It's probably your second-from-last line: {% endfor % } should be: {% endfor %}
Mike Pepper's user avatar
  • 4,391
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
2 votes
Accepted

Comparing current date to multiple entry matrix date fields

I think you've just got a logic problem. {% if entryStart >= present and entryEnd <= present %} The only way this will ever evaluate to true is if the entryStart and entryEnd DateTime fields ...
Brad Bell's user avatar
  • 67.5k
2 votes
Accepted

loop trough whole array inside a field

You can by using this: {{ include(template_from_string(entry.fieldName)|raw) }} You could also try it with just |raw But that probably only works for just HTML This works on both a Plain Textfield ...
Justin Dekkers's user avatar
2 votes
Accepted

Making an entry grid with Batch 2,3 (2 in row then 3 in row)

With some help from HendrikEng's answer I figured out something that does work. I had to make the batch bigger to an amount of 5 making the "row's" bigger. Also I had to set the position of the ...
Justin Dekkers'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,642
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 omit similar content in a for loop based on the first letter in title?

If you need only unique letters, you can build a new array and only merge first letters that are not already in the array like this: {% set letterArray = [] %} {% for alpha in craft.entries.section('...
Alex Roper's user avatar
  • 2,642

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