How do you create pagination links related to entries within a Matrix Field in a single entry? I have combed through similar questions, although my pagination links are not performing properly.

I would like to add pagination links to entries within a channel. And I would like for the pagination order to be pulled from an Entries field within a Matrix located within a Single entry.

So far I have:

{% set source = craft.entries.sectionId(10).first().gallery.type('gallery').first().entries.relatedTo(entry) %}

{% set prevEntry = entry.getPrev(source) %}
{% set nextEntry = entry.getNext(source) %}

&lt;a href="{{ nextEntry.url }}"&gt;Next: {{ nextEntry.title }}&lt;/a&gt;
&lt;a href="{{ prevEntry.url }}"&gt;Previous: {{ prevEntry.title }}&lt;/a&gt;


Where SectionId(10.first() is pulling the Single entry and gallery.type('gallery').first().entries is pulling the Entries field within the Matrix.

Unfortunately this is producing blank Next and Previous links.

&lt;a href=""&gt;Next: &lt;/a&gt;
&lt;a href=""&gt;Previous: &lt;/a&gt;


I have referred to other questions regarding params and relations, although through experimenting with different solutions I am not queuing up the correct Next and Previous entries.

Neither is Craft providing any error reports regarding the blank state.

Any help is greatly appreciated.
In this particular case, .relatedTo(entry) is not necessary. So long the entry the following code is located within is within the entry within the source the pagination will populate. Otherwise these will remain empty.

{% set source = craft.entries.sectionId(10).first().gallery.type('gallery').first().entries %}

{% set prevEntry = entry.getPrev(source) %}
{% set nextEntry = entry.getNext(source) %}

&lt;a href="{{ nextEntry.url }}"&gt;Next: {{ nextEntry.title }}&lt;/a&gt;
&lt;a href="{{ prevEntry.url }}"&gt;Previous: {{ prevEntry.title }}&lt;/a&gt;