<p>I really don’t want to be ‘that guy’ because I’m sure you have your reasons.</p>
<p>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.</p>
<p>Then you’re back to a really simple list of artworks in Craft, with the standard prev/next twig code.</p>

CC BY-SA 4.0
2023-01-16T10:10:48.610Z
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
CC BY-SA 4.0
2023-01-23T15:12:29.057Z
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
CC BY-SA 4.0
2023-01-12T20:46:47.537Z
2023-01-12T20:46:47.537Z
mitrol
<p>First of all I have looked through various similar questions/answers, but don't think any of them address my specific problem.</p>
<p>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 &quot;artworkImages&quot;. This matrix allows the adding of &quot;rows&quot;, 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:</p>
<p><a href="https://i.sstatic.net/zRU3l.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/zRU3l.png" alt="Screenshot of category layout in admin" /></a></p>
<p>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:</p>
<pre><code>{% set criteria = {section: 'artwork'} %}
{% set prevEntry = entry.getPrev(criteria) %}
{% set nextEntry = entry.getNext(criteria) %}

&lt;div class=&quot;mt-4 mt-lg-0 border-bottom border-bottom-lg-0 py-4 py-lg-0&quot;&gt;
{% if prevEntry %}
  &lt;a class=&quot;btn-prev {% if nextEntry %}me-3{% endif %}&quot; href=&quot;{{ prevEntry.url }}&quot;&gt;Previous&lt;/a&gt;
{% endif %}
{% if nextEntry %}
  &lt;a class=&quot;btn-next&quot; href=&quot;{{ nextEntry.url }}&quot;&gt;Next&lt;/a&gt;
{% endif %}
&lt;/div&gt;
</code></pre>
<p>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 <a href="https://craftcms.stackexchange.com/questions/15074/sorting-across-blocks-to-get-prev-next-links">this one</a> , but I'm struggling to know what to replace things like 'sourceElement' with.</p>
<p>I am using Craft CMS 4.2.2.</p>
<p><strong>Edit:</strong></p>
<p>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.</p>
<pre><code>{% 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) %}
</code></pre>

CC BY-SA 4.0
2023-01-10T12:59:59.923Z
@MoritzLost would you have any availability to advise on this? You've both been incredibly helpful in the past!
Grace
<matrix><previous-next>
Finding next and previous entry within multiple matrix fields
# Finding next and previous entry within multiple matrix fields

(Asked by: username_0 on 2023-01-09)

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:

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) %}


{% if prevEntry %}
  Previous
{% endif %}
{% if nextEntry %}
  Next
{% 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.
Edit:
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) %}

**Tags:** matrix, previous-next

> **Comments:**
> **username_0**: @MoritzLost would you have any availability to advise on this? You've both been incredibly helpful in the past!  

---

## 1 answers

### Answer by username_1 on 2023-01-12. Score: 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.

> **Comments:**
> **username_0**: 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?  
> **username_1**: 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.  

---

