Search code examples
seopaginationexpressionengine

ExpressionEngine pagination: access last segment


I am working on some search engine optimisation for my expression engine 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 some 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


Solution

  • This is actually a really tricky question. There's no native way to accomplish this in EE, so you'd have to write a custom add-on to do this properly.

    You might want to delete this and instead post it over on the new ExpressionEngine StackExchange, where you'll have more capable eyes on it.