Hot answers tagged

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.2k
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,266
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
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
4 votes
Accepted

Create a new row every 3 items in a matrix

To my understanding this should work for a matrix field (called imageLinkBoxes) that contains only one type of block (imageLinkBox) that has 3 fields (imageLinkBox.linkText, imageLinkBox.linkUrl and ...
Ovi Savescu's user avatar
4 votes
Accepted

Only display entries based on a field conditional

I highly recommend to use the fantastic Preparse plugin to save to length of your body text in a hidden field. Create and add a new "bodyLength" Preparse field to your "Artists" entry type. Use this ...
carlcs's user avatar
  • 36.2k
4 votes
Accepted

Reverse count with paginated entries?

You already pass the array keys into the loop as a variable pageInfo. There's a couple of properties you can access from it, see the Craft docs: Templating Reference → {% paginate %} Of interest for ...
carlcs's user avatar
  • 36.2k
4 votes
Accepted

Looping out a lot of entries.

There isn't really a good way to extract large chunks of Craft entries using the core features in Twig. Every entry you fetch is a EntryModel and those take up some memory due to relations and other ...
naboovalley's user avatar
  • 2,834
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

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

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.4k
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
  • 4,924
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.2k
3 votes
Accepted

Combing an If Statement with Pagination

To get the pagination to work as expected, you need to incorporate the if into your craft.entries query. There may be a more elegant solution to do this, but looking at the database, the selected ...
André Elvan's user avatar
  • 7,278
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
Accepted

Filtering entries by multiple categories with multiple filters

Passing the category as a comma delineated string and using craft.request.getParam('category') the second time will not grab the second variable 4rooms. First you need to parse that string into an ...
a-am's user avatar
  • 2,857
3 votes
Accepted

Correct way to merge craft.user field arrays and use outside the loop?

It looks like you have a typo in your merge, and the structure of the merge is incorrect. {% set merged = specialisms|merge(specialisms) %} This is saying set merged to the array specialisms merged ...
kcolls's user avatar
  • 608
3 votes

Only display entries based on a field conditional

You could do something like this: {# Grab all entries with a body > 100 %} {% for entry in craft.entries.section('artists').order('RAND()').find() if entry.body|length > 100 %} {# Limit output ...
Brad Bell's user avatar
  • 67.2k
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,622
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.2k
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.2k
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,767
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
  • 10.7k
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
Accepted

Can't get checkboxfield values to show

The problem was that I used this code within an entry, so I had to use the tag of that entry (offer) instead of entry. The code that works now is: {% for option in offer.offerMinimumPrice.options %}...
sanderfish'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

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