5

I am working on some search engine optimisation for my ExpressionEngine site. I need to add some link tags into my head to allow Google crawlers to acknowledge the relationship between paginated pages in my blog.

I need to add:

<link rel="next" href="http://www.mysite.com/news/latest-news/P10" />

to the first news page, then:

<link rel="prev" href="http://www.mysite.com/news/latest-news/" />
<link rel="next" href="http://www.mysite.com/news/latest-news/P20" />

to the next page, and so on until the final page where I'll add

<link rel="prev" href="http://www.mysite.com/news/latest-news/PXX" />

I have been trying to achieve this by using the following code:

First news page:

{if last_segment == "latest-news"}
        <link rel="next" href="http://www.6dg.co.uk/news/latest-news/P10" />
    {/if}  

This works fine, but for subsequent pages I have tried this, which does not work as PX is not accessible as a url segment.

{if last_segment == "P10"}
        <link rel="prev" href="http://www.6dg.co.uk/news/latest-news" />
        <link rel="next" href="http://www.6dg.co.uk/news/latest-news/P20" />
    {/if}  

{last_segment} returns latest-news.

Does anyone know how I can get round this issue?

I also have another issue with this method. Currently I only have 3 paginated pages, but this will go up as more entries are added, so I am not going to know what segment value to target in order to add my final link tag.

Any help will be greatly appreciated.

I am using ExpressionEngine version 2.2.2

1
  • This is a tough one, though I'm sure a Stash devotee will have an answer ready here soon! Commented Jan 8, 2013 at 18:02

4 Answers 4

6

Done it!

Using Stash.

In my pagination tags I've added the following code:

{exp:stash:set name="total_pages"}{total_pages}{/exp:stash:set}
{exp:stash:set name="current_page"}{current_page}{/exp:stash:set}
{if previous_page}
    {exp:stash:set name="prev_auto_path"}{auto_path}{/exp:stash:set}
{/if}
{if next_page}
    {exp:stash:set name="next_auto_path"}{auto_path}{/exp:stash:set}
{/if}

Then in my tag I've added the following

{if segment_1 == "news"}
        {if {exp:stash:get name="current_page"} == 1}
            <link rel="next" href="{exp:stash:get name="next_auto_path"}" />        
        {/if}    

        {if {exp:stash:get name="current_page"} == 2 && {exp:stash:get name="current_page"} != {exp:stash:get name="total_pages"}}
            <link rel="prev" href="/news/latest-news" />        
            <link rel="next" href="{exp:stash:get name="next_auto_path"}" />    
        {/if}           

        {if {exp:stash:get name="current_page"} > 2 && {exp:stash:get name="current_page"} < {exp:stash:get name="total_pages"}}
            <link rel="prev" href="{exp:stash:get name="prev_auto_path"}" />        
            <link rel="next" href="{exp:stash:get name="next_auto_path"}" />        
        {/if}

        {if {exp:stash:get name="current_page"} == {exp:stash:get name="total_pages"} && {exp:stash:get name="current_page"} > 2}
            <link rel="prev" href="{exp:stash:get name="prev_auto_path"}" />        
        {/if}

        {if {exp:stash:get name="current_page"} == {exp:stash:get name="total_pages"} && {exp:stash:get name="current_page"} == 2}
            <link rel="prev" href="/news/latest-news" />    
        {/if}
    {/if}

Update:

I've updated the code to work properly if there is only 2 news pages. Thanks for your help

1
  • Glad you sorted it - be sure to accept your answer (you'll have to wait until 48 hours has passed since your question was posted). Commented Jan 9, 2013 at 13:48
2

Perhaps instead of looking for the last variable, you want to check if the page is paginated -- because that's basically what you're looking for, right?

Check: http://devot-ee.com/add-ons/mo-variables

{if paginated}, {if not_paginated}, {page_offset} are some variables that he has

{if paginated} would probably work for you and simply check if there's pagination occurring so you don't keep adding last_segment conditionals.

The tricky part is figuring out how to render what the next page is e.g. P20. You might just want to wrap the {if paginated} conditional with the entries tag and pagination actually will render the next page link.

The problem with that is could be resource intensive (ie. another entries tag just to get the pagination # or url?) Perhaps Stash would be a good way to set the next page URL from the current page body and just use stash get to have it render the next page URL.

Anyone else have a simpler or native way to do this or maybe we can still use Rob's page_offset? Not sure how it works but I can assume that MIGHT be simpler, even.

Hope that helps? :)

1
  • hello, I had a look a mo-variables but unfortunately you need to be running ee version 2.4, I'm only on 2.2.2 and can't upgrade (well, unless the client pays for it!). Stash sounds promising, I'll give that a whirl. Many thanks dave
    – magicspon
    Commented Jan 8, 2013 at 20:08
0

Checkout this thread on devot-ee. Using that conditional worked well for me in paginating a blog archive.

Note, you'll need to use Stash and Switchee ;-)

0

Try using {last_segment_absolute} instead of {last_segment} with the Last Segment extension.

This extension will add the following global variables:

  • {last_segment} – last segment (will never be the pagination segment ie /P1)
  • {last_segment_absolute} – absolute last segment (even pagination)
  • {last_segment_id} – the id of the last segment (for example “3”)
  • {last_segment_absolute_id} – the id of the absolute last segment

If you don’t want to add these global vars you can use the plugin instead:

{exp:last_segment} will return the last segment. {exp:last_segment ignore_pagination=‘no’} will be the same as {last_segment_absolute}

5
  • Hello, Tried. Doesn't work for me though. {last_segment_absolute} still ignores the pagination segment (as does {exp:last_segment ignore_pagination='no'} ). Thanks
    – magicspon
    Commented Jan 9, 2013 at 9:48
  • And you have the Last Segment addon installed? Just clarifying.
    – Anna_MediaGirl
    Commented Jan 9, 2013 at 14:24
  • I tried to use it but it didn't work for me. So now I don't have it installed
    – magicspon
    Commented Jan 10, 2013 at 9:34
  • That's odd. It's such a simple addon. Should have worked out of the box for you.
    – Anna_MediaGirl
    Commented Jan 10, 2013 at 17:09
  • aye! I would still have needed to use stash in order to get the {auto_path} tag stored for the next/prev links, as I will never know how many paginated pages there will be.
    – magicspon
    Commented Jan 11, 2013 at 17:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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