If you ever need previous/next links for pages in WordPress, you may have noticed that it’s not a built in feature like it is for posts – at least as far as I can tell.
I suppose it’s understandable since pages don’t really have linear organization, but every now and then, the ability to simple click ‘next’ ‘next’ ‘next’ to cycle through some pages would be really handy. For instance, I work on a lot of sites for bed & breakfasts and they inevitably have a landing page for their rooms, then child pages for each individual room. These individual rooms pages often have “next room” and “previous room” links, plus one back to the main page.
Anyway… adding those links manually was really annoying and rather time consuming – especially if anything got out of order or if a room needed to be added or removed….
So here’s the code I came up with to make my life just a little bit easier:
{Updated 05/17/09}
<?php /* prev/next links */ if ($post->post_parent == 19) { /* 19 == id of 'Rooms' page */ $exclude = implode(',',array('984')); $toplevel = 'title_li=&depth=1&echo=0&exclude='.$exclude; $siblings = $toplevel.'&child_of='.$post->post_parent; if (function_exists(pause_exclude_pages)) { pause_exclude_pages(); $links = ($post->post_parent) ? wp_list_pages($siblings) : wp_list_pages($toplevel); resume_exclude_pages(); } else { $links = ($post->post_parent) ? wp_list_pages($siblings) : wp_list_pages($toplevel); } $links = array_filter(explode("</li>",$links)); foreach($links as $k=>$v) { if (strpos($v,'current_page_item') !== FALSE) { $current = $k; } $links[$k] = $v.'</li>';} $total = count($links)-1; $prev = (int)$current-1; $next = $current+1; if ($current == $total) $next = '0'; // if you're on the last page, next will be the first if ($current == '0') $prev = $total; // if you're on the first page, prev will be the last $prevLink = strip_tags($links[$prev],'<a>'); //remove the <li> tags $nextLink = strip_tags($links[$next],'<a>'); //remove the <li> tags function change_anchor_text($link, $newtext, $placement) { $pos1 = strpos($link, '>')+1; $pos2 = strpos($link, '<', 1); $anchortext = substr($link, $pos1, $pos2-$pos1); if($placement == 'before') { return str_replace($anchortext, $newtext.' '.$anchortext, $link); } else if ($placement == 'after') { return str_replace($anchortext, $anchortext.' '.$newtext, $link); } else if ($placement == 'replace') { return str_replace($anchortext, $newtext, $link); } } $prevLinkb = $prevLink; $prevLink = change_anchor_text($prevLink, ' < View Previous Room ', 'replace'); /* link customization goes here */ $nextLink = change_anchor_text($nextLink, ' View Next Room > ', 'replace'); $prepend = ''; $separator = '<a href="/lodging/" class="norm">Return to All Rooms</a>'; $separator2 = '<a href="#" class="norm">Check Availability Online</a>'; echo '<p class="pnpn">'.$prepend.$prevLink.$separator.$nextLink.'</p>'; } /* end prev/next links */ ?>
- Works with the Exclude Pages Plugin
- Can easily define page IDs to skip
- Customize link text (Insert your < or > before or after the default menu label, or replace the text altogether)
- Someday I’ll get around to widgetizing this
Suggestions for improvement? Please let me know in the comments.