Need to add previous / next page functionality, with some fine tuning
-
I need to create previous/next type links on sibling pages. Basically something similar to this https://www.ads-software.com/support/topic/next-page-literally?replies=23 (the version posted by kristjan)
The code I’ve been trying thus far is a bit different. In functions.php I’ve got:
function siblings($link) { global $post; $siblings = get_pages('child_of='.$post->post_parent.'&parent='.$post->post_parent); foreach ($siblings as $key=>$sibling){ if ($post->ID == $sibling->ID){ $ID = $key; } } $closest = array('before'=>get_permalink($siblings[$ID-1]->ID),'after'=>get_permalink($siblings[$ID+1]->ID)); if ($link == 'before' || $link == 'after') { echo $closest[$link]; } else { return $closest; } }
And then in my sidebar I have:
<a href="<?php siblings('before'); ?>">Previous</a> <a href="<?php siblings('after'); ?>">Next</a>
This method has a problem – I haven’t found a way to make it so that when one is on the last page in the list, that the Next link either goes away or if I could even add a class to it so I can modify it in CSS.
Question 1: How can I make it so that the Previous link will either be gone or I can add a class to it if it’s on the Previous page? (And obviously, the same with Next.
The second question is a bit more tricky. I need to add a page number between Previous & Next. So it would look like, for example, Previous 5/32 Next. Again, this is only with the sibling pages.
Question 2: How do I add page number including the total number of sibling pages?
Thanks much
- The topic ‘Need to add previous / next page functionality, with some fine tuning’ is closed to new replies.