Hot answers tagged

15 votes
Accepted

How Can I Include a Conditional for First Type of Matrix

You could first set a variable to test against and then change it the first time the block type appears. {% set firstTime = true %} {% for block in entry.matrixField %} {% if block.type == "...
user avatar
  • 601
15 votes
Accepted

Remove trailing comma from an output loop?

There is a special tag inside for loops you can use for this {% for category in entry.mediaType %} <b>{{ category.title }}</b>, {% if loop.last %} <b>{{ category....
user avatar
14 votes
Accepted

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

{% set entries = craft.entries.section('pages').type('blogPost') %} {% for entry in entries %} {{ entry.title }} {% endfor %} Is what I have cobbled together... any pointers on improvements would ...
user avatar
14 votes

Remove trailing comma from an output loop?

While your conditional is complete personally preference I would do the following as I find it slightly easier to read. Note the use of the {%- if -%} to trim whitespace: {% for category in entry....
user avatar
  • 806
8 votes
Accepted

Use loop index number in a Twig variable

I found the answer in the Twig documentation: http://twig.sensiolabs.org/doc/templates.html#variables You can use a dot (.) to access attributes of a variable (methods or properties of a PHP object,...
user avatar
  • 2,573
7 votes
Accepted

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

for is part of the native Twig language. for is for general usage... It's the basic, general looping structure that you'll use 99% of the time. You'll find that it closely mirrors for and foreach in ...
user avatar
  • 23.6k
7 votes
Accepted

Matrix: get the index number of a certain block

You can use a custom counter instead of using the loop.index value. {% set step = 0 %} {% for block in entry.matrixBlocks %} {% if block.type == 'stepBlockType' %} {% set step = step + ...
user avatar
  • 35.9k
6 votes

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

Here's another good example for working with categories that may be useful to you. This will loop through all categories (that have entries) and show their entries: {% set entries = craft.entries....
user avatar
  • 2,985
6 votes
Accepted

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

This example assumes you have your Category Group setup to use custom URLs. In the template used to display your categories, the category variable will be available and represent your CategoryModel ...
user avatar
  • 13.3k
6 votes
Accepted

Getting the last X number of pages in pagination

As I already wrote in my comment 3 days ago something like this should work {% paginate craft.entries.section('mySection').limit(10) as pageInfo, pageEntries %} {% for page, url in pageInfo....
user avatar
5 votes

Display entries by current entry author?

In order to display all other entries by an author except the current one, you will need to perform another step — either by filtering out the current entry from the query or adding a conditional. {% ...
user avatar
5 votes

Remove trailing comma from an output loop?

{% for category in entry.mediaType %} <b>{{ category.title }}</b>{{ "," if not loop.last }} {% endfor %}
user avatar
5 votes

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

Update Sorry, I originally thought you were building nav and just showing simplified code. If you just want to list the categories then this should work: {% nav category in categories %} {{- ...
user avatar
5 votes
Accepted

Why are variables persisting inside of my for loop?

This is totally normal and expected behavior. In fact, it's critical for some looping situations, and pretty standard across programming languages. Wouldn't you expect to find this same behavior in ...
user avatar
  • 23.6k
5 votes
Accepted

Better way to find a specific asset?

That’s definitely the way to do it! I would set the URL segments to their own variables, to clean it up a little bit. {% set entrySlug = craft.request.getSegment(2) %} {% set imageOffset = craft....
user avatar
5 votes

Loop divisible by issue

Forgive me if I'm wrong... by using loop.index0 is divisible by(2) are you trying to determine if the instance is odd or even? 1st - returns false 2nd - returns true 3rd - returns false 4th - returns ...
user avatar
  • 3,604
5 votes

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

As you'll only ever be working with one entry I wouldn't bother with the for loop for the first part of your question. Just set a variable with your featured news article. Something like this (not ...
user avatar
5 votes
Accepted

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

Instead of duplicating the entries, you could run your entries loop twice (or as many times as you want) maybe? {% for i in 0..1 %} {% for entry in entries %} {{ entry.title }} <br&...
user avatar
  • 7,200
4 votes
Accepted

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

You'd further filter your returned artist entries down like this: {% set artists = craft.entries.section('artists').intoCollection('1') %} Then remove all the unnecessary "intoCollection" ...
user avatar
  • 35.9k
4 votes

Loop divisible by issue

What you want is not the loop variable of the inner loop around the image, but the loop variable of the outer loop. To get to the outer loop, use loop.parent. Try this: {% if loop.parent.loop.index0 ...
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 ...
user avatar
4 votes
Accepted

Next block inside of a loop

Try doing something like: {% set nextBlock = block.getNext() %} https://craftcms.com/docs/templating/matrixblockmodel#getNext
user avatar
4 votes
Accepted

Simplify Twig/JS loop code

You can select elements by class and give your html elements custom data attributes. When this is your clickable html element with the corresponding div <button data-id="dimsTarget4" class="...
user avatar
3 votes
Accepted

Nesting level too deep when looping through matrix blocks

element.type is not a simple string (add {{dump(element.type)}} to your loop to see this). What you should use is element.type.handle. So the assignment is: {% set prevElementType = element.type....
user avatar
3 votes
Accepted

Variable Not Available Outside of Loop

Your dump from outside the loop is being dumped BEFORE the variable is set within the loop. Move it to after the loop and try it again.
user avatar
  • 566
3 votes
Accepted

pulling in an image from another section within a matrix loop

I'm not sure what {% for image in craft.assets.kind('image') %} is all about in your code since the image variable isn't being used anywhere. But here's the basic structure on how you could loop ...
user avatar
  • 66.5k
3 votes
Accepted

Loop through category groups

I'm not seeing any obvious ways to access groups directly in Twig. There may be a more direct way, idk — but this should work. {% set categoryGroupNames = [] %} {% for category in craft.categories %} ...
user avatar
3 votes

Wrapping a matrix block

Without understanding your use case or having the context of your full template and/or Matrix loop, it's very difficult to provide a good answer. If you only want to "wrap all outputted blocks" in a ...
user avatar
3 votes
Accepted

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

You need to define the block previous/next variables outside of your for loop. Also you should check for the last element in the loop. You can find a great working example on the happy lager demo ...
user avatar
  • 1,317
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 ...
user avatar
  • 7,248

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