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
2
votes
2answers
5k views

Total sum within loop

I have simple loop: {% for total in thing %} {{ total.seatsKids }} {{ total.seatsParents }} {% endfor %} This results in a string of numbers eg. 1 3 4. I want the sum of all numbers. So how do I ...
1
vote
1answer
157 views

Can't access data in Matrix field

Stuck on a problem at the moment… trying to output a Rich Text field (copy) from an entry. The copy is contained in a matrix field (so: Entry -> Matrix Field -> Copy Field) but I can’t access it no ...
1
vote
1answer
59 views

Error in case of nested for loops

I have a comment section with comments and subcomments. A comment relates directly to the blog post while a subcomment relates to a comment. Now, I have the following problem on the entry page: ...
7
votes
1answer
3k views

Display entries if Field Date is past Now

I'm trying to make a upcoming and past event page. I can make an if statement for displaying the past or upcoming events but the problem is if I limit the amount of entries to be displayed I realised ...
1
vote
1answer
59 views

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

I have an entries field called 'customerLogoBarUrl'. It contains one entry. So I can do this: {% for item in entry.customerLogoBarUrl.limit(1) %} {% set theUrl = item.url %} {% endfor %} But then ...
2
votes
1answer
428 views

Is there a better way than stacked if else

i am using a slider in my template, and depending on if the user filled out values in the craft matrix (slides can have up to 4 rows) i want to have twig compile only the necessary code. I am not sure ...
3
votes
1answer
1k views

Check and remove repeat entries from loop

Is it possible to loop through the list of entries and filter out any that have the same ID as any of the other entries (duplicates)? I have a specific case where this is an issue. I want to keep the ...
1
vote
1answer
547 views

for loop: get the title of the first element of an entries field

I want to get the title of the first element from a entries field in a for loop. I think its easier to understand with a code example: {% set programs = craft.entries.section('program_point').block....
1
vote
1answer
275 views

Streamlining a loop with a bunch of conditions

I'm sure there must be a tidier and more efficient way of doing this so thought I'd throw it out there for some opinions. Building a site that has the usual lists (in grid format) for Blog, Gallery, ...
0
votes
1answer
2k views

Issue with twig loop.first loop.last contained in nested for statement

I'm trying to pull out selected categories in a front-end user profile. The cats have 2 levels and need to be pulled out into a nested list with only the selected options. I've tried different ways of ...
2
votes
1answer
123 views

Looping out a lot of entries.

Ok, so I'm looping through a lot of entries. It's a large request, and I had to up php memory limit to 512M. But I'm sure there is a less memory consuming way to do this. Also, I want to cache the ...
2
votes
1answer
174 views

Combing an If Statement with Pagination

I'm trying to pull all the entries in a category that have a multi-selection selected and then paginate it. If I just do the below, it works perfectly fine. {% set entriesRed = craft.entries....
1
vote
1answer
352 views

How can I loop through a Channel entries asset folder?

Basically, for every entry that I add to my 'Portfolio' channel, it creates a sub-folder inside assets/portfolio. For example I have: assets/portfolio/entry-one assets/portfolio/entry-two On my ...
2
votes
2answers
335 views

Reverse count with paginated entries?

I have this loop {% paginate craft.entries.section('sermons').limit('10') as pageInfo, entries %} {% for sermon in entries %} ... {{ loop.revindex }} ... {% ...
1
vote
1answer
420 views

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

I'm using preparse field plugin to limit the amount of tags a user can add to his account. I've modified the code slightly so the tags are processed as a comma, separated, list. The plan is to ...
1
vote
1answer
117 views

How to add users to the search results?

I'm trying to display three different sets of data in search results. The first two, News and Services I can easily concatenate by the following: {% set searchSections = craft.entries.section(['...
1
vote
1answer
716 views

Create a new row every 3 items in a matrix

I need to output a row every three items in a matrix. Here is what I have so far. The problem is that this outputs a row every three items but it duplicates each matrix item three times and puts them ...
1
vote
2answers
950 views

Only display entries based on a field conditional

Hey I am trying to get 4 entries, but only want 4 that have a body length > 100. How would I accomplish this? {% for entry in craft.entries.section('artists').order('RAND()').limit(4) %} // only ...
1
vote
3answers
493 views

Can't get checkboxfield values to show

I'm trying to build a filter and for that I want my client to be able to select a few options, which I use as classes for in my filter. I chose for a checkbox field and set it up in the backend. Now ...
2
votes
1answer
1k views

Sort / Order Entries on select or button click

I have a page on my website were I display alot of entries. Now I want those entries to be sorted by Date, Title etc. I know how to change that hard coded but I want to make it dynamic with a <...
4
votes
2answers
1k views

Is it possible to change a loop if mobile browser is detected?

I have a loop to go through my products which go in batches because they need to show detail info every third entry. When the Website is viewed from a mobile device, Tablet or Mobile I want it to loop ...
1
vote
1answer
677 views

Filtering entries by multiple categories with multiple filters

I'm trying to build a category filter on a page that lists houses that are for sale. The idea of the filter is to make it possible to select houses which, for example, are for sale or rent, have a ...
3
votes
2answers
165 views

Help developing point system for sports league (w/o plugins)

I'm in the middle of a very long project developing a league site for the National Collegiate Wrestling Association in my spare time (once finished, could be a great showcase for Craft). Currently, I'...
10
votes
1answer
4k views

Advanced query/filter based on date and values

I have a channel of events with a field ‘eventDate’ for the date/time of the event. Within that there are ‘types’ of events ‘eventType’. What would be the most efficient way of outputting the NEXT (...
1
vote
1answer
91 views

Home page loop once, other pages loop through all

I only want one block on the home page, other pages can loop through all of them. Is there a more efficient way to write this? {% if craft.request.getSegment(1) is empty %} {% for block in ...
1
vote
1answer
120 views

Looping through returns nothing after using dynamic routes to get the entry type and category

I'll explain a bit of the structure of the content and then proceed to the issue. So i have a website with a shop in it with the url mysite.com/shop/, it is an channel section, and its template is ...
7
votes
2answers
667 views

Add ordinal suffix to loop.index

I'd like to add an ordinal suffix to the loop.index variable within a for loop. For example: 1st 2nd 3rd
2
votes
1answer
130 views

SET versus FIND

As I understand, I have (at least) the following two possibilities to list entries of a channel: Possibility 1: {% for entry in craft.entries.section('example').find() %} Possibility 2: {% set ...
4
votes
2answers
10k views

twig for loop put every 2 elements in a new container

I have this loop: {% for div in myHandleHere %} <div> {{ block.text }} </div> {% endfor %} That actually outputs something like: <div> one </div> <div> two </...
2
votes
1answer
582 views

Nesting level too deep when looping through matrix blocks

I'm running into a nesting level error when looping through matrix blocks, and I can't quite figure out what the problem is. The problem arises when I use a variable to track the type of the previous ...
2
votes
1answer
235 views

Next / Prev and looping/cycling entries instead of reaching the end of the selected results

Is there a way to be able to use entry.getNext(params) to return back to the first entry when you're viewing the last entry in the parameters? With the result of looping/cycling through all entries ...
0
votes
1answer
52 views

retrieve assets from an entries

Im looking up the last 2 entries like this {% set entries = craft.entries.section('report').limit(2) %} My entry got an assets field called 'portfolio'. I would like to retrieve 8 images in random ...
1
vote
2answers
1k views

Matrix loop nesting issue using switch

Anyone know if it's possible to check what the current field is while in a loop of a matrix block? Running into an issue with a matrix block in my loop for how I want to output the content. {% case '...
2
votes
2answers
410 views

How Do I Select the second instance of a certain block type

We use a matrix field for the article content on our site. We have different blocks for different content (text, images, embed etc..) and we have optional fields that are outside of the matrix that ...
1
vote
1answer
156 views

Prevent queries based on condition

I have a set of parameters that need to be checked before outputting an entry in a particular section of a website. The code for that looks somewhat like this: {% for slide in craft.entries.section('...
1
vote
2answers
269 views

Loop based on date (with math)

I would like to loop through entries only if the current date/time is before 5am on the following day from when the entry's eventDate (custom field mind you)is. In other words I may have an event at ...
1
vote
2answers
997 views

Get last row in table field for loop

How can I determine if the row being returned from a table field in a for loop is the last row?
2
votes
1answer
163 views

calculating average ratings from a series of review fields

I've setup a section for reviews with several fields as numbers for a rating 0-5, I'm trying to figure out how to get the total of all reviews and sum of all reviews to provide a single average for an ...
1
vote
1answer
1k views

Creating JS slideshow with Twig “for” loop

I have an entry with a matrix field called quantification containing plain text and asset field. Im looping through the matrix field and for each asset quantification I have a new gallery. Each div ...
2
votes
1answer
2k views

Why are variables persisting inside of my for loop?

Is anyone able to tell me why variables which I've defined inside of a for loop are persisting and retaining their values on into consecutive iterations? In order to overcome this, I've had to set ...
2
votes
2answers
1k views

Set dynamic class in for Loop

I have this simple element model and I'm trying to add dynamic class within loop. So every time a model outputs, It outputs dynamic class. Can't go for another loop within loop, in case I could, ...
7
votes
2answers
53k views

How do I set a variable in Twig if a value exists more than once in a loop?

How do I loop through entries and set a variable if there are any duplicates in the data I'm checking? In my case, I need to check if there are more than one entry per year. The variable I'm setting ...
5
votes
1answer
1k views

Group looped entries into blocks and wrap in markup every x number of loops

I am trying to loop through all the entries in a section and "group" them into items which are in blocks of 9 then wrap them with some html markup so there will be the following code: {% set ...
9
votes
1answer
21k views

Use loop index number in a Twig variable

Let's say I have 5 Craft fields named "item1" thru "item5." Instead of writing out each variable... {{ entry.item1 }} {{ entry.item2 }} {{ entry.item3 }} {{ entry.item4 }} {{ entry.item5 }} How can ...
1
vote
2answers
7k views

Target second (or nth) element in a for loop

I'm trying to show content for only the second entry in a for loop, but can't find any info on how to do specifically this. I'd like to do something like the below. {% for relatedImage in entry....
1
vote
1answer
520 views

How to order categories in key/value for loop?

This is what I'm trying to accomplish: All categories displayed on one page, with entries returned from each. Only six entries shown in each category. Categories need to be in a specific order. I've ...
0
votes
1answer
2k views

if/else loop with (almost) daily events

Building further on this problem, I try to make a loop that exits when the next opening hours or in other case events are found. There is an event on all days except monday. I added a couple of extra ...
1
vote
1answer
371 views

How can i output my content in pairs?

how can i output my content in pairs? like this: div.row > entry 1 + entry 2 div.row > entry 3 + entry 4 div.row > entry 5 + entry 6 so far i have the following {% for entry in ...
3
votes
2answers
3k views

Outputting Matrix Loop in Template

I have looked at documentation and I still can't figure out how to output the matrix to my template. The Field handle name is: threeHomepageImages The Block type handle name is: images The Fields ...
1
vote
1answer
1k views

More efficient matrix count method

I have some logic in a template where I want to count the number of items that have a lightswitch set to yes, and a radio button set to another value, currently I'm doing this via a series of {% set %}...