"Previous" and "Next" article links inside an article
  • Is it possible to add links to previous and next article of the same section inside an article template? I'm looking for a solution but I can't find a proper way to do it.

    All best.
  • 3 Comments sorted by
  • Hi,
     here is an old solution developed for the client with just a few articles per section (meaning it can be optimized, especially for situations when section has lot of articles), try to play with it. The idea is to make a list of articles, remember the position of currently displayed article in that list (by assigning list index value to variable) and then making links to prev and next article in that list (what you can do is to remember article id for next and previous article in another list (you know the positions of prev and next articles in that list) and then just make prev/next links by {{ set_article number=$prevArt }}

    <div id="prevnext"> 

        {{ list_articles constraints="type is news" }}
        {{ if $gimme->article->number == $gimme->default_article->number }}
        {{ assign var="artpos" value=$gimme->current_list->index }}
        {{ /if }}

        {{ if $gimme->current_list->at_end }}
        {{ assign var="length" value=$gimme->current_list->index }}
        {{ /if }}   
        {{ /list_articles }}   
        
        {{ if $artpos gt 1 }}
        {{ assign var="prevart" value=$artpos-1 }}
        {{ else }}
        {{ assign var="prevart" value=0 }}
        {{ /if }}
        {{ if !($artops == $length) }}
        {{ assign var="nextart" value=$artpos+1 }}
        {{ else }}
        {{ assign var="nextart" value=0 }}
        {{ /if }}
        
        {{ if $prevart }}
        {{ list_articles constraints="type is news" }}
        {{ if $gimme->current_list->index == $prevart }}
        <p class="previous"><a href="{{ uri options="article" }}" rel="prev">Previous article</a></p>
        {{ /if }}
        {{ /list_articles }}
        {{ /if }}
        
        {{ if $nextart }}
        {{ list_articles constraints="type is news" }} 
        {{ if $gimme->current_list->index == $nextart }}   
        <p class="next"><a href="{{ uri options="article" }}" rel="next">Next article</a></p>
        {{ /if }}
        {{ /list_articles }}  
        {{ /if }}     

    </div><!-- /#prevnext -->


    --
    Ljuba Rankovic
    Senior Front End Developer, Sourcefabric
    ljuba.rankovic@sourcefabric.org

    http://www.sourcefabric.org
    http://www.twitter.com/Sourcefabric
  • It works!

    Thanks, Ljuba, this is exactly what I need.

    All best.
  • Hi, you can do

    {{ list_articles ignore_issue="true" length="1" order="byPublishDate asc" constraints="type is news number greater `$gimme->default_article->number`" }}
    ...
    {{ /list_articles }}

    {{ list_articles ignore_issue="true" length="1" order="byPublishDate desc" constraints="type is news number smaller `$gimme->default_article->number`" }}
    ...
    {{ /list_articles }}

    If you need to evaluate for conditional content/classes run first each of the up mentioned and evaluate like

    {{ list_articles ignore_issue="true" length="1" order="byPublishDate
    desc" constraints="type is news number smaller
    `$gimme->default_article->number`" }}{{ /list_articles }}
    {{ if $gimme->prev_list_empty }}
         {{ $next = 0 }}
    {{ else  }}
         {{ $next = 1 }}
    {{ /if }}