First of all I have looked through various similar questions/answers, but don't think any of them address my specific problem.

I am building a website for an artist, and I have a Category which I've called called Body of Work (bodyOfWork). Within that category, there is a matrix field called "artworkImages". This matrix allows the adding of "rows", within which multiple entries can be selected. The reason for this is to determine the number of columns in each row on the category page, so basically for layout purposes. So in the category, you have multiple matrix fields, within which are multiple entries. I have attached a screenshot to show how this looks in the admin:

Screenshot of category layout in admin

At present I have set up previous/next buttons, but these are looping through the artwork entries themselves, not the entries selected inside the matrix fields, so the order is wrong. The code I'm using is very basic, and obviously wrong, as it is based on the section, not the matrix field:

{% set criteria = {section: 'artwork'} %}
{% set prevEntry = entry.getPrev(criteria) %}
{% set nextEntry = entry.getNext(criteria) %}

<div class="mt-4 mt-lg-0 border-bottom border-bottom-lg-0 py-4 py-lg-0">
{% if prevEntry %}
  <a class="btn-prev {% if nextEntry %}me-3{% endif %}" href="{{ prevEntry.url }}">Previous</a>
{% endif %}
{% if nextEntry %}
  <a class="btn-next" href="{{ nextEntry.url }}">Next</a>
{% endif %}

Is there a way for me to essentially get all entries within all the rows in the current entry's category, and find the next and previous entries that have been selected? I have tried setting getPrev and getNext criteria as described in answers such as this one , but I'm struggling to know what to replace things like 'sourceElement' with.

I am using Craft CMS 4.2.2.


Trying the below code gets me no further, it's still looping through the entries according to their structure order, not according to their order within the matrix field.

{% set bodyOfWork = entry.bodyOfWork.one() %}
{% set artworks = craft.entries({
  relatedTo: { sourceElement: bodyOfWork, field: 'artworkImages.artwork' }
}) %}
{{ artworks | length }}

{% set prevEntry = entry.getPrev(artworks) %}
{% set nextEntry = entry.getNext(artworks) %}
  • @MoritzLost would you have any availability to advise on this? You've both been incredibly helpful in the past!
    – Grace
    Commented Jan 10, 2023 at 12:59

1 Answer 1


I really don’t want to be ‘that guy’ because I’m sure you have your reasons.

But layout is a CSS thing. So I would drop the matrix field, if necessary add a couple of fields to each artwork that define things like the preferred ratio. Then let CSS Grid sort it out.

Then you’re back to a really simple list of artworks in Craft, with the standard prev/next twig code.

  • Yes unfortunately I think you're probably right, I'll need to rework how it's done. Thing is though won't they still end up being Matrix fields? If I need to be able to select entries and then also include a field for each entry for its proportion?
    – Grace
    Commented Jan 16, 2023 at 10:10
  • Again, I don’t know your full brief from the client, so I could be making assumptions. But the way I would do it would be to add the ratio to each artwork entry. (The ratio doesn’t change on an artwork, but you could add multiple ratios if you needed to for some reason.) Then you’re just selecting the artwork, from a list of entries. In the template, write the ratio for the entry into a CSS variable, and use that to control its display.
    – mitrol
    Commented Jan 23, 2023 at 15:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.