Jump to content


Photo

next/prev links based on field value

solved previous next

  • Please log in to reply
3 replies to this topic

#1 Marty Walker

Marty Walker

    Sr. Member

  • Members
  • PipPipPipPip
  • 330 posts
  • 153

  • LocationKatoomba, AU

Posted 30 April 2012 - 02:41 AM

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

#2 diogo

diogo

    Hero Member

  • Moderators
  • 1,973 posts
  • 1059

  • LocationPorto, Portugal

Posted 30 April 2012 - 03:14 AM

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.

#3 Marty Walker

Marty Walker

    Sr. Member

  • Members
  • PipPipPipPip
  • 330 posts
  • 153

  • LocationKatoomba, AU

Posted 30 April 2012 - 05:55 AM

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

#4 ryan

ryan

    Hero Member

  • Administrators
  • 5,753 posts
  • 3102

  • LocationAtlanta, GA

Posted 30 April 2012 - 10:13 AM

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 "...";






Also tagged with one or more of these keywords: solved, previous, next

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users