ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

Pagination entry titles

September 22, 2012 6:27am

Subscribe [1]
  • #1 / Sep 22, 2012 6:27am

    Ataul Munim

    17 posts

    Is there a way to grab the titles of the previous and next post to display instead of “next” and “previous”?

    I could only find the “Next/Previous” or numbered pages on the pagination page.

    I would have tried to construct it using the channel entries tag, but I wasn’t sure how to offset the current entry_id.

    _
    v2.5.2 - Build Date: 20120606

  • #2 / Sep 24, 2012 10:48am

    Lisa Wess

    20502 posts

    Hi, Ataul -

    The first example on the entry linking documentation explains how to achieve this:

    {exp:channel:next_entry}
        Next entry: <a href="http://{path=site/comments}">{title}</a>
    {/exp:channel:next_entry}
    
    {exp:channel:prev_entry}
        Previous entry: <a href="http://{path=site/comments}">{title}</a>
    {/exp:channel:prev_entry}

    Does that help?

  • #3 / Sep 25, 2012 5:28am

    Ataul Munim

    17 posts

    Thanks Lisa, that’s exactly what I was after - was just finding it difficult finding where different things are documented :s

    Having a bit of trouble getting it working though - I stuck it outside the channel entries tag as the docs state, and also used “SITE_INDEX” as the path (I tried with the comments path as in your example too but with the same result) but nothing is showing up (I have two previous entries for sure).

    ...
    </article>
    
    {/exp:channel:entries}
    
    <nav id="pagination">
    {exp:channel:next_entry}
        <span><a href="http://{path=SITE_INDEX}">{title}</a></span>
    {/exp:channel:next_entry}
    
    {exp:channel:prev_entry}
        <span><a href="http://{path=SITE_INDEX}">{title}</a></span>
    {/exp:channel:prev_entry}
    </nav>
    
    ...

    I would have expected at least the previous entry’s title to show up, even if the link was incorrect.

  • #4 / Sep 25, 2012 8:46am

    Ataul Munim

    17 posts

    Sorry, it only doesn’t appear for the site index page - if I go to the page showing that single entry it’ll show the navigation. I know that the tag pair only works on pages with single entry - doesn’t the “limit” parameter accomplish this? (I’ve changed the path and the links themselves work now - it’s just a case of getting it to show on my home page http://www.ataulmunim.co.uk)

    {embed="includes/doc_header"}
    
    <body id="home">
    
    {embed="includes/banner_nav"}
    
    
    <section role="main">
    {exp:channel:entries channel="journal_entries" limit="1"}
    <article class="entry">
    <header>
    <h2 id="post-title">{title}</h2>
    <span><time pubdate datetime="{entry_date format="%Y-%m-%d"}">
    {entry_date format="%Y · %m · %d"} | {entry_date format="%g:%i%a"}</time></span>
    </header>
    
    
    {journal_entry_body}
    
    Tags: 
    {exp:tagger:tags entry_id="{entry_id}"}
        {if tagger:no_tags}no tags were found for this post.{/if}
        <a href="/tag/{tagger:urlsafe_tagname}">{tagger:tag_name}</a>
    {/exp:tagger:tags}
    
    
    {if share_page}
    {embed="includes/share_code" toShare="{share_page}" zeTitle="{title}" zeURL="{url_title_path=site/index}"}
    {/if}
    
    
    </article>
    
    {/exp:channel:entries}
    
    <nav id="pagination">
    {exp:channel:next_entry channel="journal_entries"}
        <span><a href="http://{path=site/}">{title}</a></span>
    {/exp:channel:next_entry}
    
    {exp:channel:prev_entry channel="journal_entries"}
        <span><a href="http://{path=site/}">{title}</a></span>
    {/exp:channel:prev_entry}
    </nav>
    
    </section><!-- main -->
    
    </body>
    </html>
  • #5 / Sep 25, 2012 9:55am

    Lisa Wess

    20502 posts

    Hi, Ataul,

    A single-entry page is defined as a template displayed when segment 2 or segment 3 have a URL or Entry ID in them.  You can read more about this in the first article on The Importance of Semantics

    For the home-page, if you need to get to the previous entry, you may need a separate channel entries tag, and to make use of:

    limit="1" offset="1"

    Or offsetting to whatever number beyond your limit.

    So for instance:

    {exp:channel:entries channel="journal_entries" limit="1" offset="1"}
    <span><a href="http://{comment_url_title_auto_path}">{title}</a></span>
    {/exp:channel:entries}

    Does that make sense?

  • #6 / Sep 25, 2012 10:12am

    Ataul Munim

    17 posts

    what an awesome collection of articles - that’s really helpful.

    And yes - usually when I receive advice on here, I partially understand it, and if it works I don’t question it further; I understood your last post fully (hopefully this will be a growing trend).

    Thanks!

    As an aside, what you suggested for the home page is what I was thinking about doing prior to learning about the next/previous entry links (but I didn’t know about offset) - why is this not used in general - is there an overhead to using it when compared to using next/prev tags?

  • #7 / Sep 25, 2012 10:25am

    Lisa Wess

    20502 posts

    what an awesome collection of articles - that’s really helpful.

    You just made my morning brighter, thank you Ataul! 

    why is this not used in general - is there an overhead to using it when compared to using next/prev tags?

    The ‘next’ would be hard to do - there isn’t an easy way with the channel entries tag to offset forward, if that makes sense.  Also this is going to be a bit more dynamic and ‘just work’.  Also, the tags like this call in explicitly the information they need and only the data they need; the channel entries tag is actually a pretty heavy hitting tag because it has to gather in a lot of data and do a lot of queries into the database.  So yes, the channel entries tag has overhead that tags made for specific functions do not need to have.

    Make sense?

  • #8 / Sep 25, 2012 10:58am

    Ataul Munim

    17 posts

    This is what I ended up with (doubt it’s particularly elegant or robust though) but at least it works:

    <nav id="pagination">
    {if last_segment == ''} <!-- if it's the index page (NOT single-entry) -->
        {exp:channel:entries channel="journal_entries" limit="1" offset="1"}
            <span><a href="http://{url_title_path=site/index}">{title}</a></span>
        {/exp:channel:entries}
    {if:else}
        {exp:channel:next_entry channel="journal_entries"}
            <span><a href="http://{path=site/}">{title}</a></span>
        {/exp:channel:next_entry}
    
        {exp:channel:prev_entry channel="journal_entries"}
            <span><a href="http://{path=site/}">{title}</a></span>
        {/exp:channel:prev_entry}
    {/if}
    </nav>
  • #9 / Sep 25, 2012 11:00am

    Ataul Munim

    17 posts

    The ‘next’ would be hard to do - there isn’t an easy way with the channel entries tag to offset forward, if that makes sense.

    Yes, I noticed this when I tried to use “-1” as a value :D

    Make sense?

    It does, thanks 😊

  • #10 / Sep 25, 2012 11:06am

    Lisa Wess

    20502 posts

    Hi, Ataul -

    One thing I’d warn you about is that with advanced conditionals everything inside is parsed first so you still have the overhead. If you use simple conditionals, then those things do NOT get parsed and your site is far better optimized.

    Example of simple conditional:

    {if channel_short_name == "news"}
    your stuff
    {/if}

    Example of advanced conditional - pretty much anything that uses if:else, or if:elseif, such as in your code. 

    Also, you’re using:

    {if last_segment == ''}

    The last segment won’t even exist if it’s blank.  Try this in a template to see what I mean:

    last segment: {last_segment}
    {if last_segment == ''} - last segment is blank {/if}
    {if last_segment == 'hi'} - last segment is hi{/if}

    Then try visiting:

    http://example.com/site/template/ <- last segment would in theory be segment 3 here, but it doesn’t exist to test.
    http://example.com/site/template/hi - third segment is hi and you’ll get ‘last segment is hi’ when you view your template.

    You’ll never get ‘last segment is blank’

    So, I’d recommend testing against a segment you know should be there - either 2 or 3, most likely.

    Make sense?

  • #11 / Sep 25, 2012 11:14am

    Ataul Munim

    17 posts

    Ah thanks. Probably safer to do:

    {if segment_1 == 'site'} <!-- if it's not the home page -->
        {exp:channel:next_entry channel="journal_entries"}
            <span><a href="http://{path=site/}">{title}</a></span>
        {/exp:channel:next_entry}
    
        {exp:channel:prev_entry channel="journal_entries"}
            <span><a href="http://{path=site/}">{title}</a></span>
        {/exp:channel:prev_entry}
    
    {if:else}
        {exp:channel:entries channel="journal_entries" limit="1" offset="1"}
            <span><a href="http://{url_title_path=site/index}">{title}</a></span>
        {/exp:channel:entries}
    {/if}

    Will it bother parsing the else block if the first block returns true?

  • #12 / Sep 25, 2012 11:16am

    Lisa Wess

    20502 posts

    If you have an {if:else} the entire thing in the conditional will be parsed.  You’d need to close out the conditional to make it a simple conditional - ie: {if blah} code {/if}.

    So you’re checking to see if segment 1 is site; I’m not sure which is your homepage, so I can’t confirm if that is right or not.  Are you getting the results that you want?

  • #13 / Sep 25, 2012 11:26am

    Ataul Munim

    17 posts

    http://ataulmunim.co.uk/ee/ is my home page, and doubles up as my single-entry template too (is this unconventional?).

    If a link is clicked to visit another entry, either via next/previous, or direct link from the archive page, then the url shows http://ataulmunim.co.uk/ee/site/maya-quicksave-script where “site” is my first segment, and the url-title follows.

    I’m getting the results I’m expecting (i.e. all the links work), but the way you describe the conditional’s behaviour is counter to how I expect them to work (from experience in native programming, like Java/C-based languages) but it doesn’t look like it’s having a noticeable effect on my site, so I’m happy to leave it at that for now 😊

  • #14 / Sep 25, 2012 11:33am

    Lisa Wess

    20502 posts

    I tend to prefer a separate single entry template and a separate multi entry template.  Using embeds and snippets for repeated information, and fewer conditionals for display, since EE already handles those conditionals innately. 

    Make sense?

    So ‘site’ is your template group, and ‘index’ is your template - assumed since it would be segment 2 if anything other than index.

    In any case, you have it working and you are happy with it, so let’s leave it there so you can do other work!  Is there anything else on this that I can help you with before closing it?

  • #15 / Sep 25, 2012 12:05pm

    Ataul Munim

    17 posts

    I tend to prefer a separate single entry template and a separate multi entry template.  Using embeds and snippets for repeated information, and fewer conditionals for display, since EE already handles those conditionals innately.

     
    Yes, I’ll try that for my next site, and it should make things a bit easier.

    So ‘site’ is your template group, and ‘index’ is your template…

    Yep, I’m creative with names :D

    That’s it for me, I think after a certain point, it’s better to start focusing on the content of the site, and not just how pretty it looks. It’s functional for the most part now anyway, thanks!

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases