fenton Posted June 18, 2013 Posted June 18, 2013 Hi there, I feel stupid for asking this I'm trying to loop through folders with pages that have the template "articles" and want to display "prev I next" buttons, but don't want to display the pipe symbol on the first ot last post. I had it working with the explode version (uncommented code), but it doesn't work if I do it the right way with the PageArray. I'm referring to this post: http://processwire.com/talk/topic/3865-image-tags/?p=37806 <?php $siblings = $pages->find("template=article"); $next = $siblings->getNext($page); $prev = $siblings->getPrev($page); /* $pageID = $page->id; $siblings_array = explode("|", $siblings); $first_page = reset($siblings_array); $last_page = end($siblings_array); if ($pageID == $last_page) { echo "<a href='$prev->url'>prev</a>"; } elseif ($pageID == $first_page) { echo "<a href='$next->url'>next</a>"; } else { echo "<a href='$next->url'>next</a> | <a href='$prev->url'>prev</a>"; } */ $pageID = $page->id; $first_page = $siblings->first(); $last_page = $siblings->last(); if ($pageID == $last_page) { echo "<a href='$prev->url'>prev</a>"; } elseif ($pageID == $first_page) { echo "<a href='$next->url'>next</a>"; } else { echo "<a href='$next->url'>next</a> | <a href='$prev->url'>prev</a>"; } ?> any ideas what I'm doing wrong? thanks a lot, j
adrian Posted June 18, 2013 Posted June 18, 2013 Quick glance - do you need to have: $first_page = $siblings->first()->id 1
kongondo Posted June 18, 2013 Posted June 18, 2013 (edited) Even these: $next = $siblings->getNext($page); $prev = $siblings->getPrev($page); are verbose I think? PW already has $page->next(); and $page->prev(); Can't those be used in this case?See these (answer for you in first link): http://processwire.com/talk/topic/3133-detect-first-and-last-sibling-page/http://processwire.com/talk/topic/3714-how-to-do-this/ http://processwire.com/api/types/nullpage/ Edited June 18, 2013 by kongondo
fenton Posted June 18, 2013 Author Posted June 18, 2013 thank you guys! will go through your suggestions and report back
Soma Posted June 18, 2013 Posted June 18, 2013 There's an easy way to strip off chars with php method trim(). But in this case it can get tricky. Also regarding PageArray and explode doesn't really play a role here... But it sinks in better if you make faults. I think an idea would be to do: $nav = ''; if($page->prev->id) $nav .= "<a href='{$page->prev->url}'>prev</a> | "; if($page->next->id) $nav .= "<a href='{$page->next->url}'>next</a> | "; echo trim($nav," | "); Fairly easy concept. Depending on how and where you use it, this way it can get annoying as you get jumping link elements. You get sometimes "prev" sometimes "prev|next" links So maybe add some logic to show only a span when there's no next found. So you can fade it out with CSS. Now this way there's no need to trim. $nav = ''; if($page->prev->id) $nav .= "<a href='{$page->prev->url}'>prev</a> | "; else $nav .= "<span class='inactive'>prev</span> | "; if($page->next->id) $nav .= "<a href='{$page->next->url}'>next</a>"; else $nav .= "<span class='inactive'>next</span>"; echo $nav; 1
Soma Posted June 18, 2013 Posted June 18, 2013 This can get really creative, as Ryan would now maybe come with something like: $nav = ''; foreach(array("prev","next") as $dir) { if($page->$dir->id) $nav .= "<a href='{$page->$dir->url}'>$dir</a> | "; else $nav .= "<span class='inactive'>$dir</span> | "; } echo trim($nav, " | "); 3
fenton Posted June 19, 2013 Author Posted June 19, 2013 thanks a lot guys! But it sinks in better if you make faults. true PW already has $page->next(); and $page->prev(); Can't those be used in this case? the problem is, that I have to traverse folders, since my structure is like this: News - 2013 -- 05 --- article 06 --- article 05 --- article 04 --- article 03 --- article 02 -- 06 --- article 01 still wondering though, why from my example above: if ($pageID == $last_page) { echo "<a href='$prev->url'>prev</a>"; } doesn't work, even though if I echo both variables the value is the same: 1060 1060 cheers, j
fenton Posted June 19, 2013 Author Posted June 19, 2013 adrian - you are right! thanks Quick glance - do you need to have: $first_page = $siblings->first()->id that was the problem 1
Soma Posted June 19, 2013 Posted June 19, 2013 You dont need all of these checks, the code I posted aleeady takes care of it. 1
fenton Posted June 19, 2013 Author Posted June 19, 2013 Hi Soma, but it wont traverse the month folders (I can go back and forth within each of them)
Soma Posted June 19, 2013 Posted June 19, 2013 I don't really understand as your code doesn't aswell. Ah now I get it sorry! What you're doing may a little inefficient to load all articles.
fenton Posted June 19, 2013 Author Posted June 19, 2013 I know, I know ...it's people like me that give PHP a bad name e.g. if I'm on the first article "news/2013/06/newest-post" I can use "prev" till it reaches the last article in "news/2013/06/", it doesn't traverse the folders ( to "news/2013/05/older-post" )
Soma Posted June 19, 2013 Posted June 19, 2013 Yeah prev and next does only traverse inside a parent. But there's plenty traversal methods to get around it. $nav = ''; if($page->prev->id) { $nav .= "<a href='{$page->prev->url}'>prev</a> | "; } else if($page->parent->prev->id && $page->parent->prev->is("template=month") && $page->parent->prev->numChildren){ $nav .= "<a href='{$page->parent->prev->children->last->url}'>prev</a> | "; } if($page->next->id) { $nav .= "<a href='{$page->next->url}'>next</a> | "; } else if($page->parent->next->id && $page->parent->next->is("template=month") && $page->parent->next->numChildren){ $nav .= "<a href='{$page->parent->next->children->first->url}'>next</a> | "; } echo trim($nav," | "); Maybe need to adapt to your situation, but it's straight forward and everything is possible. 2
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now