Jump to content

next/prev links based on field value


Marty Walker
 Share

Recommended Posts

Hi,

I'm setting up some next/prev page linking code. I only want those links to show if the page's publish_date field is less than today. Here's my convoluted and cobbled-together code:

<?php
$today = time();
if($page->prev->getUnformatted('publish_date')<$today) echo "<li id='prev'><a rel='nofollow' title='{$page->prev->title}' href='{$page->prev->url}'>{$page->prev->title}</a></li>";
if($page->next->getUnformatted('publish_date')<$today) echo "<li id='next'><a rel='nofollow' title='{$page->next->title}' href='{$page->next->url}'>{$page->next->title}</a></li>";

Firstly, is there a way to simplify this? Secondly, if there's no value returned for the previous page (for example) it still outputs the HMTL. ie:

<li id='prev'><a rel='nofollow' title='' href=''></a></li>

Thanks for any help.

Cheers

Marty

Link to comment
Share on other sites

if there's no value returned for the previous page (for example) it still outputs the HMTL

this happens because 0 is less than today. You have to test also for it's existence. "less than today AND more than 0" for example.

Link to comment
Share on other sites

Thanks diogo. This is what I ended up with which works fine:

<?php
$today = time();
if($page->prev->getUnformatted('publish_date')<$today && $page->prev->id > 0) echo "<li id='prev'><a rel='nofollow' title='{$page->prev->title}' href='{$page->prev->url}'>{$page->prev->title}</a></li>\n";
if($page->next->getUnformatted('publish_date')<$today && $page->next->id > 0) echo "<li id='next'><a rel='nofollow' title='{$page->next->title}' href='{$page->next->url}'>{$page->next->title}</a></li>";

Regards

Marty

Link to comment
Share on other sites

Here's another way you could go:

$siblings = $page->siblings('publish_date<' . time()); 
if(($prev = $page->prev($siblings)) && $prev->id) echo "...";
if(($next = $page->next($siblings)) && $next->id) echo "...";
  • Like 2
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...