Questions tagged [loop]

The tag has no usage guidance.

Filter by
Sorted by
Tagged with
9
votes
1answer
24k 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 ...
8
votes
1answer
838 views

How Can I Include a Conditional for First Type of Matrix

I want to include a conditional for when the 'Text' block type is returned for the first time in a loop. In css it would be first-of-type. Here's what I have at the moment: {% if type == 'text' and ...
7
votes
3answers
17k views

Remove trailing comma from an output loop?

I want to create a comma separated list of categories to which an entry belongs, but without the trailing slash on the last element... can't work out how to remove it! {% for category in entry....
6
votes
2answers
2k views

How can I create a simple loop through entries in a category?

I have a category set up and have added entries to it. How do I loop through these entries, and output just the title of each entry? Serious noob here but trying to learn, any help would be ...
5
votes
1answer
180 views

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

I am new to craftcms and did a look through on the documentation just recently. I am at the part where it explain about the Categories Field and found out that we can loop through the ...
4
votes
1answer
362 views

Better way to find a specific asset?

I'm currently using a route to get a specific asset for a gallery section of my site. URL is similiar to http://craft.dev/place/place-name/gallery/1 (Where 1 is the number in the gallery.) I'm ...
4
votes
1answer
759 views

How to list all existing values for a field across site

I have a huge set of entries which have a lot of fields. Say, the entry type is a 'book' and fields are 'keywords', 'year', 'authors', etc. Now I'm building a filter form which would allow user to ...
3
votes
2answers
4k views

How do I loop through entries for a specific entry type?

I would like to loop through entries in a channel but only the ones for a specific entry type. Is this Possible? I'm using the free version of craft on a test site, I have used the channel for my ...
3
votes
2answers
2k views

Loop divisible by issue

Code: {% for block in entry.testimonials %} <div class="col-24 {% if loop.last %}gutter-bottom{% else %}gutter-bottom--none{% endif %}"> <div class="section inner"> <div ...
3
votes
1answer
887 views

Getting the last X number of pages in pagination

I am working on a pagination for some articles. I am using a for loop to get the 3 pages after and before the current page .getPrevUrls(3) and .getNextUrls(3) So far it looks like this, where 35 is ...
3
votes
2answers
3k views

How to target first or last item in a nav children loop

I'm trying to use this code: {% nav category in categories %} {{ category.title }} {% children %} {% if not loop.last %} BLAH {% endif %} {% endnav %} It appears that the {% if not loop....
3
votes
1answer
2k views

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

I have this this code - {% for entry in craft.entries.section('news').first() %} <div class="container" id="featured-news"> <div class=col-md-6> <h2 class="news-...
3
votes
1answer
216 views

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

I'm trying to duplicate entries inside a macro, which will displays duplicated items inside a carousel. It's not possible to merge Element Queries. Even if it's possible to merge IDs, merging an ...
3
votes
1answer
724 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 ...
3
votes
1answer
303 views

The twig syntax for this for loop

I am trying to develop a loop in a craft template that adjust the numbers of rows and awards per row based on the total number of awards. The numbers are all coming back correct but I am struggling ...
2
votes
1answer
1k views

Matrix: get the index number of a certain block

I have a matrix set up with some blocks in it, and one of those blocks is to add "steps" to a page in a matrix. Now I'm trying to get the number of the step so that they only have to enter the content ...
2
votes
2answers
468 views

Using a loop in a loop, how do I check if child loop is empty to not display parent loop?

I'm trying to display a list of names alphabetically ordered. This is working fine, but I also want to check if the name is intoCollection or not (using a Lightswitch). If it is intoCollection, it ...
2
votes
2answers
64 views

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

I would like to retrieve a list of all commerce customer addresses except for one. When working with entries, this can be done with the {id: 'not ' ~ some.id } parameter. This doesn't seem to work ...
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
1answer
43 views

Getting entries excluding the first

I'm trying to set up having the first entry pull in and then later down the page pull in the remaining posts (as I need to break the loop with other content). I'm a little unsure how to set up pulling ...
2
votes
2answers
155 views

Creating entries loop with nth loops across unordered lists

I'm looking for some help with the most efficient and performant way to generate this loop. | COL 1 | COL 2 | COL 3 | | 1 | 2 | 3 | | 4 | 5 | 6 | | 7 | 8 | 9 | | ...
2
votes
1answer
205 views

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

I'm trying to start a loop at the 8th entry in a structure and then loop through the remaining entries in that structure and stop once I reach the end. How would I accomplish this in Craft?
2
votes
1answer
269 views

How do I group matrix blocks into batches of 4?

I am building a site which uses one content type as its primary holder for content. This is to make content editing easier on the content editor. All URL's are primarily accessing this one content ...
2
votes
1answer
206 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....
2
votes
1answer
531 views

Loop through category groups

I've looped through individual category groups to find each parent/child category, but is there a way to loop through and only show Category Group names?
2
votes
1answer
211 views

Loop through Matrix Block fields within a Switch Case and retain HTML structure

I'm struggling to get my head around the syntax to output information in a correct HTML structure. At the moment I have this: {% for block in entry.menuBuilder %} {% switch block.type %} {% case '...
2
votes
2answers
271 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 ...
2
votes
1answer
649 views

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

I have a navigation structure as follows: {% set page = entry ?? category ?? null %} <ul class="nav menu--main"> {% nav entry in entries %} <li class="menu__item{% if page and ...
2
votes
2answers
250 views

Is it possible to use a loop variable to target a specific field entry?

I'm looping through the rows of a table field and hoping to target a specific field entry based on the loop.index. Here's a simplified example of what that might look like: {% for row in entry.table %...
2
votes
1answer
99 views

Previous/Next button to next item in Matrix

I've made a projects structure with inside a couple of projects. On the homepage people can fill in a matrix with either a project or a textblock. I'm capable of looping through the detail pages in ...
2
votes
1answer
528 views

Display text field from Matrix of an Entry Field in Matrix

Question is similar to some others I've looked through but their solutions don't work as there are some differences. Currently this doesn't output any of the fields I try to access within the ...
2
votes
1answer
531 views

Craft redirect loop after logging in

I'm experiencing weird cookie/redirect issues after logging in the admin/cp. Keeps redirecting to / (even on /) untill I've deleted my cookies. Clean download/install, no errors occured. I have no ...
2
votes
1answer
268 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 ...
1
vote
2answers
1k views

Display entries by current entry author?

I have a blog section on my site and would like a "More posts by this author" list. How would I go about this, the logic is clear but the syntax to write for it is not forthcoming.
1
vote
3answers
154 views

Parse html on first and last occurrence of a matrix block type in a series of that same block type

I'd like to wrap a series of block.type (images and description) in a div. So at the first iteration of this block type it needs to parse the start of the div. In the last iteration of this block type ...
1
vote
2answers
94 views

Possible to paginate product variants in Craft Commerce 2?

I am currently in the process of building a commerce 2 site with a very detailed level of nesting in the product options so we're looking to list all variants for a product. (Sounds silly, but it ...
1
vote
1answer
614 views

Simplify Twig/JS loop code

Can any help simplify this js/twig code? The code links to some tabs that show floorplans of the property being sold. The specified aria-controls refers to a supertable field called floorLevel that ...
1
vote
1answer
596 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" %} {{ ...
1
vote
1answer
49 views

Display all entry titles on one line during FOR loop

I have a FOR loop that should be displaying all the entry titles on one line. But instead, it is outputting them on separate lines. I want to see: Developer: Jane Doe, Jim Doe Instead, I am ...
1
vote
2answers
343 views

Wrapping a matrix block

I'm outputting fields from a Matrix block, how do I then wrap all outputted fields from 'artworkFile' that are available with some HTML. Not individually wrapped, but all outputted fields. Current ...
1
vote
1answer
128 views

Count entries with condition

I would like to count comments that belong to a certain entry (product). I have this so far: {% set comments = craft.entries().section('productcomments').all() %} {% set counter = 0 %} {% for ...
1
vote
1answer
52 views

Entries from matrix should not appear in loop

I have a Matrix with an entry field. I have a Loop which is looping entries out of a section. Goal: The entries from the matrix should not appear in the loop of the section. So the Matrix defines ...
1
vote
1answer
29 views

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

I'm looking for shorter way of doing this: dog dog dog –––––––––––––––––––––––––– cat cat cat –––––––––––––––––––––––––– hamster hamster hamster Currently, I have to run 3 independent loops to ...
1
vote
1answer
243 views

How do I loop through two sections as one set

I'm trying to loop through two separate channels, but combined together. And then just check which channel the entry belongs to in the loop to display it correctly. This is how I had it done in EE, ...
1
vote
2answers
1k views

Check if current entry is in loop and add class

I'm super new to both Craft and Twig so, apologies in advance!! I'm looking to determine if the current entry I'm on is in a loop being called on that page. I have loop that goes through the entries ...
1
vote
1answer
117 views

Loop through assets field on detail/product page

I am trying to loop through an assets field called gallery on entries in a commerce products channel. This field can have multiple images in it. I can get this to work on a channel listing page ...
1
vote
1answer
14 views

Output loop position number

Good Evening, This is probably something really simple, but I need to output the position of the entries in a query within the loop {% set relatedEntries = craft.entries().section('memories').limit(10)...
1
vote
2answers
71 views

Getting blocks from matrix and injecting them throughout a page, NOT a single loop statement

I have a matrix field that allows the user to add little 'designed' features throughout the page. These are placed in certain positions on the page and so I want to loop through these but because they ...
1
vote
1answer
52 views

Reuse items from array with forloop

I have a loop of a list of regions on a project, this could be an infinite list of areas as my client's customer-base grows. The project branding has four key colours, I have these in an array: {% ...
1
vote
1answer
48 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&...