I have a channel called "course_content" that contains a relationship field which holds entries from a "lesson_content" channel.

The user clicks on a course (course_content) and all of the lessons related to that course are displayed. They then select the lesson that they want to view.

I'm trying to add "Previous" and "Next" links to the lesson entry pages to allow the user to navigate the lessons specific to that course. Currently, I'm using the following code:

{exp:channel:prev_entry channel="lesson_content"}

    <a class="older" href="{path=courses/lesson}"><span><i class="icon-left"></i> Previous</span></a>

{/exp:channel:prev_entry}


{exp:channel:next_entry channel="lesson_content"}

    <a class="newer" href="{path=courses/lesson}"><span>Next <i class="icon-right"></i></span></a>

{/exp:channel:next_entry}

The problem is that is is looping through the entire "lesson_content" channel so when you get to the end of the lessons for that class, you continue to lessons from the next class.

I'm sure I need to use an embed, I just can't figure out how to specify just the entries that are found in the same relationship as the entry that is being viewed.

Any help would be appreciated.

I don't think you need an embed - have you tried the siblings tags?

https://docs.expressionengine.com/latest/fieldtypes/relationships.html#accessing-siblings

Someone has posted a potential solution here: https://expressionengine.com/forums/topic/247219/siblings-tag-cant-handle-dynamic-parameters

To summarise here (untested):

{exp:channel:entries channel="lesson_content"}
  {siblings limit="1" orderby="entry_date" sort="desc" stop_before="{entry_date}"}
    <a href="{siblings:url_title_path=courses}">{siblings:title}</a>
  {/siblings}
  {siblings limit="1" orderby="entry_date" sort="asc" start_on="{entry_date}"}
    <a href="{siblings:url_title_path=courses}">{siblings:title}</a>
  {/siblings}
{/exp:channel:entries}
  • Thanks James. That code, along with the link to the EE Forum post got it working for me. I always forget about the EE Forums. :-( I'm going to post my code as an answer. – Brad Hopkins Jan 5 at 18:07
up vote 0 down vote accepted

I ended up using an embed to accomplish this.

Embed Code - based on the EE Forum link: https://expressionengine.com/forums/topic/247219/siblings-tag-cant-handle-dynamic-parameters

{embed="courses/lesson-pagination" entry_id="{entry_id}" entry_date="{entry_date format='%Y-%m-%d %H:%i'}"}

Embed Template

{exp:channel:entries channel="lesson_content"}
              {siblings limit="1" orderby="entry_date" sort="desc" stop_before="{embed:entry_date}"}
                <span class="navigate previous"><a href="{siblings:url_title_path=courses/lesson}"><i class="icon-left"></i> Previous</a></span>
              {/siblings}

              {siblings limit="1" orderby="entry_date" sort="asc" start_on="{embed:entry_date}"}
                <span class="navigate next"><a href="{siblings:url_title_path=courses/lesson}">Next <i class="icon-right"></i></a></span>
              {/siblings}
            {/exp:channel:entries}

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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