Skip to main content
Share Your Experience: Take the 2024 Developer Survey

Questions tagged [forloop]

For loop is a statement executing some code repeatedly (forever or till some condition exists it)

Filter by
Sorted by
Tagged with
1 vote
1 answer
48 views

Excluding current entry on related entries

I'm trying to exclude the current entry for a set of related entries. I am targeting an entry field to grab the set of related entries. Here is the code I have: {% if block.relatedPageEntries | ...
glider's user avatar
  • 647
0 votes
1 answer
121 views

Add an Object key and value to an exsiting object

I am looping over an array of objects and trying to add a new Key and Value. I keep getting a host of errors so was hoping someone could help a little with formatting this loop. {{ set = transforms: [{...
Wally's user avatar
  • 343
1 vote
1 answer
199 views

How can I output only half the entries in a loop?

I'm building a Craft website on Bootstrap 5. I want to split the output of a loop evenly across 2 seperate columns. There is an image column in the middle so the loop can't be continuos. The amount of ...
DieselHashtag's user avatar
1 vote
1 answer
46 views

Alternating Forloop items across Content Blocks

I have a parent forloop that cycles through my content blocks and multiple child forloops that cycle through list items. I'm trying to alternate the left and right alignment for my child forloop items ...
Dan Lee's user avatar
  • 1,107
1 vote
1 answer
58 views

For loop to dynamically output field names

I have a set of fields: fieldName1 fieldName2 fieldName3 fieldName4 fieldName5 and I want to be able to create a for loop something like this: {% for i in 1..5 %} {% if entry.fieldName ~ i|length ...
Tyssen's user avatar
  • 653
1 vote
1 answer
118 views

Loop when removing existing ID's

I'm using this code to remove duplicate entries from my loop: {% set existingIds = [] %} {% for entry in entries %} {% if entry.id not in existingIds %} <div class="column is-4">...
Lukas Snarskis's user avatar
1 vote
2 answers
228 views

Why does getting a single block in NEO prevent getting the remainder of the blocks?

I have blocks set up for nutritional values: Calories, Total Fat, Carbohydrates, etc. These are set up in NEO blocks. I can successfully loop through all of the NEO blocks (about 20 rows total) and ...
Vaughn D. Taylor's user avatar
1 vote
1 answer
344 views

Simple way to create a slideshow with Twig and a for loop?

I have this code, which is basically a loop in a Twig template where I show opinions: <section class="opinions"> <h2 class="opinions__title heading2"> ...
Miss Sunshine's user avatar
1 vote
2 answers
48 views

How can I ensure certain items always display at the end of a list?

I have a website where I want to show Power Coat (-PC) options at the end of a list. The data goes into Craft in an unpredictable order, from a third-party service, so I am responsible for organizing ...
Priti Rathod's user avatar
1 vote
2 answers
359 views

Sort Entries by Value from Dropdown

I have a list of students and can sort them by name, sport, and school. The name and sport portions are easy, I'm grabbing the values without issue. Where I'm struggling is the school value is from a ...
Cynder Gray's user avatar
1 vote
1 answer
35 views

Looping on the User Field added to Entry

I am fetching the fields of an entry in a block but can not draw through a user field named as eventHost. I get error 'Variable "name" does not exist.' What is wrong with my loop? {% set ...
Dan Lee's user avatar
  • 1,107
1 vote
1 answer
38 views

Checking if related entries are live in a for loop and setting a variable based on the results

So I have a section called children and these are related to various other entries include users. I am fetching all the children for the currentUser like so and eager-loading all the other ...
Terry Upton's user avatar
  • 1,894
1 vote
2 answers
2k views

Load More Entries Button

I am trying to make my entry loop have a load more button, and not sure how to do it. I tried the method from http://craftsnippets.com/articles/infinite-scrolling-and-lazy-loading-with-craft-cms but I ...
Justin Kendall's user avatar
1 vote
1 answer
105 views

How to get a list of unique related entries from within a related entries loop

Bear with me as I am not sure the best way to explain this. But to try and summarise... how can I loop through related entries (locations) inside a a related entries loop (exams -> related to the ...
Terry Upton's user avatar
  • 1,894
1 vote
1 answer
80 views

Outputting image assets with flickity

I'm trying to use flickity to create a draggable gallery on a single entry template, but I'm having issues with the output. In the backend it looks like this. A field using multiple assets. Without ...
Maggie's user avatar
  • 53
1 vote
2 answers
85 views

Correct Limit for Entries with IF statements

I have content on my site that can't be shown in some countries and requires a basic level of geo-targeting, based on the users profile and not by any form of IP geo-targeting. I have set up an ...
Chrisl2310's user avatar
1 vote
1 answer
107 views

How to loop through a table and set variables based on the existence (or not) of data in those tables?

I am using a table field in Craft (price) limited to one row only, used to manage price setting in different currencies on an entry. The three cols each contain number fields with the handles dkk, ...
kerns's user avatar
  • 33
1 vote
1 answer
39 views

Need help with filling matrix images into CSS grid

I want to fill in shuffled images into a CSS grid as a project overview. The images come from a matrix and have 4 different formats. So I want to fill in a format A image into the fitting grid cell (...
analog junkie's user avatar
1 vote
1 answer
205 views

Create a dynamic select element

I want to build a dynamic html select element that includes the prior five years, given the current year. So, since it is now 2020, the result would be: <select> <option value="2015">2015&...
Roi Agneta's user avatar
  • 1,463
1 vote
1 answer
146 views

A for loop within a for loop that is split

Possibly a bad question title...sorry but wasn't sure how to describe it. Basically I have a for loop that every 4 items I want to pull in a different item from another loop. This is a series of Q&...
Terry Upton's user avatar
  • 1,894
1 vote
1 answer
47 views

Ordering Looped Results by Field's Numerical Value

I’m looping through some text fields on the form results page and need to order the numerical output from low to high. How would I do this using twig? {% for field in submission %} {% submission[...
Dan Lee's user avatar
  • 1,107
0 votes
0 answers
216 views

How to create an array of items and fetch them with .all()?

Is there a way to create an array of items, fetch them with .all() and loop through them? For example: {% set array = array|merge([block]) %} {% for block in gallery.all() %} {% endfor %}
nmsdvid's user avatar
  • 305
1 vote
1 answer
441 views

Looping Related Entries with a Super Table

I need to loop related entries. I am using a SuperTable field with the handle "related" with an entry field with the handle "contentEntry" and two text fields for alternative text and descriptions. ...
Dan Lee's user avatar
  • 1,107
0 votes
1 answer
202 views

For loop through custom field in JavaScript

I'm using a script to animate four sets of numbers on a page. I would like the numbers to be editable from within the admin (a SuperTable field). Currently I have this: <ul class="counters"> ...
Martin's user avatar
  • 1,203
1 vote
0 answers
236 views

Error while sending QUERY packet. PID=881 when using cache key

If I do: {% set cacheKey = block.id %} {% cache using key cacheKey if craft.app.config.general.cache %} {% for item in items %} <div class="swiper-slide ais-hits--item" data-id="{{ ...
Jorge Anzola's user avatar
2 votes
3 answers
87 views

Keeping order of blocks like in backend

Fairly new user here. I am kinda confused how to template. I want to rearrange the order of blocks in my backend and show them in the right order in the front. Here is how i am currently writing a ...
Evolis's user avatar
  • 31
2 votes
2 answers
404 views

Loop index inside a switch content builder

How would one construct this? I have built a little content builder and wish now to change the layout HTML on the fly when there are more than one matrix block. In the example below, this is just one ...
moondawg's user avatar
  • 333
1 vote
1 answer
247 views

Add conditional in a for loop

I need to add a separator if there are a multiple of types. How can this be done in a for loop? {% for type in entry.buildType %}{{ type }}{% endfor %} The outcome is similar to a breadcrumb nav, ...
Nutmeg's user avatar
  • 598
1 vote
1 answer
183 views

.limit(1) on asset field loop is effecting another loop on same page

I have an Asset field where the first image is used as a big 'feature' and then the rest are used as support further down the page. When I add .limit(1) to the for loop for the 'feature' image it ...
Martin's user avatar
  • 1,203
0 votes
0 answers
123 views

How do I create an index page that loop through the table fields of its entries?

Every entry in a section has a table. The table depicts sales over 7 years. I want to create an index page that lists the title of each entry and the sales for each entry in the section. I know ...
Hellyar's user avatar
  • 921
1 vote
1 answer
253 views

Match Matrix Fields Before Looping Through Results

I hope someone has an example or something similar they have done in Craft before. Her is what I have. Structure: Section -> Flight Company Matrix Name -> Plane Details Matrix Block: Plane Model ...
Vin's user avatar
  • 383
0 votes
1 answer
42 views

My `for` loop has fallen...and it can't get up

I want my <ul> to show a single tag representing all "active" entries ( "active" using a custom lightswitch field in the entry: fellowStatus. Each tag is a year (e.g. 2014, 2010, etc) E.g. if ...
Andrew Hahn's user avatar
1 vote
1 answer
45 views

Omitting repeated content in a for loop

I'm trying to use this slightly differently: How do I omit similar content in a for loop based on the first letter in title? I have a for loop with several entries. One field is a drop-down with a ...
afc's user avatar
  • 67
1 vote
1 answer
38 views

Allowing the user to update a CraftCMS pagination limit with listing filter options

I am building a pagination layout limiting the content to 10 because, well, it makes sense to break down large amounts of content to smaller chunks. But as there is so much, a user might want to ...
Jay's user avatar
  • 1,042
1 vote
1 answer
904 views

Use Twig's batch filter with conditional

I am trying to make a grid of blocks using Twig. I have different types of blocks so I am using a conditional to test the block type and render different components per block type. The problem I am ...
mrseanbaines's user avatar
1 vote
1 answer
312 views

Dynamic Navigation with Condition to check slug

I'm trying to achieve one pretty simple thing, but putting it into my dynamic nav is making it a little difficult. I want to have a little number of the total amount of entries in my channel "listings"...
mateostabio's user avatar
1 vote
1 answer
233 views

Changing first matrix block

I'm trying to target the first matrix block and changing the structure of the HTML, but nesting an IF statement within the loop but it's actually not doing anything, instead it's just displaying both ...
JMKelley's user avatar
  • 1,406
1 vote
1 answer
25 views

error when trying to loop through grouped entries according to dropdown field

I was trying to follow this example from stack overflow to loop through a list of entries and output them by a custom dropdown field {% set allEntries = craft.entries.section('resource').find() %} {% ...
mheavers's user avatar
  • 125
1 vote
1 answer
42 views

Archive Not Returning Correct Date Variable

I'm using the technique for creating an archive that's in the Craft docs: https://craftcms.com/support/entry-archive I'm archiving by day, so my code looks like this: {% set agendaEntries = craft....
kmgdev's user avatar
  • 1,199
1 vote
1 answer
800 views

Next block inside of a loop

I’m struggling with getting the next block inside of a loop of a Matrix-Field. I’ve tried this: {% set nextBlock = entry.articleContent.offset(1).limit(1) %} {% if nextBlock.type == "textBox" %} {{ ...
Malte's user avatar
  • 215
0 votes
2 answers
28 views

How to disable loops from automatically wrapping non-marked up content?

So I've got a completely clean install of Craft 3: RC 2. I've installed Redactor, and nothing else. In the templates/index.twig file; this is all I have: {% set test = ['one', 'two', 'three'] %} {...
Mark Notton's user avatar
  • 2,337
1 vote
1 answer
5k views

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

I'm getting incorrect loop.index results with the following code. It returns "1" for each entry returned. Is there a different way this needs to be written? {% for order in orders %} {% for ...
Anna_MediaGirl's user avatar
1 vote
2 answers
64 views

How to pull a single podcast source, using the Matrix field and loop?

{% if block.type == "podcastBlock" %} {% for category in block.podcastChannel %} {{category.title}} <audio class="mp3" controls> {% for entry in block....
TRIM's user avatar
  • 314
1 vote
1 answer
129 views

Looping through Entries based on MultiSelect Field Selection

I’ve got a people entry type that has a peopleCategory multiselect field. I’m wanting to loop through all the people and grab the ones that have a a specific item selected in the multiselect field (in ...
Jon Yetter's user avatar
1 vote
1 answer
102 views

Starting & Ending Loops with Eager Loaded Elements

Basically, I have two sections of a page. In one section of the page, I want to output the first 8 entries in a loop. In the next section of a page, I want to output from the 9th entry to the end of ...
dpayne's user avatar
  • 659
1 vote
1 answer
193 views

Filtering out section entries

I'm quite new to Craft CMS and have takeen over an application (using Twig templates). I'm trying to run a filter but not having much joy so maybe I might get some help! So, there is a people ...
devwest's user avatar
  • 11
2 votes
1 answer
254 views

Load entries based on category field value while multiple categories are selected

I have an activity calendar (more like a weekly schedule) which filters per day and per category. Because there are so many activities I can't just load them all in at once so I use some sort of Ajax ...
Justin Dekkers's user avatar
3 votes
2 answers
1k views

How do you use a for loop to output to JS?

I have a map generated in JS. I need to set the country-id and url for each country. Both 'country-idandurlare fields in my Craft categories groupcountries`. { "country-id": "FR", "url": "...
Hellyar's user avatar
  • 921
1 vote
1 answer
436 views

Sort entry titles alphabetically excluding 'The'?

I need to list the names of all of my entries in alphabetical order excluding 'the' (which could only be at the beginning). I've looked around online, but I was only able to find a code segment (which ...
Nick pappas's user avatar
1 vote
1 answer
72 views

Search then loop through files that were uploaded via FTP

Is it possible to search a server folder containing image and video files that were previously uploaded via ftp and then loop through those files in the entry template? Each entry has a unique Log ID ...
Lori's user avatar
  • 866