Previous / Next page links for WordPress


/ Published in: PHP
Save to your folder(s)



Copy this code and paste it in your HTML
  1. <?php
  2. /* prev/next links */
  3. if ($post->post_parent == 19) { /* 19 == id of 'Rooms' page */
  4. $exclude = implode(',',array('984'));
  5.  
  6. $toplevel = 'title_li=&depth=1&echo=0&exclude='.$exclude;
  7. $siblings = $toplevel.'&child_of='.$post->post_parent;
  8. if (function_exists(pause_exclude_pages)) {
  9. pause_exclude_pages();
  10. $links = ($post->post_parent) ? wp_list_pages($siblings) : wp_list_pages($toplevel);
  11. resume_exclude_pages();
  12. }
  13. else {
  14. $links = ($post->post_parent) ? wp_list_pages($siblings) : wp_list_pages($toplevel);
  15. }
  16.  
  17. $links = array_filter(explode("</li>",$links));
  18.  
  19. foreach($links as $k=>$v) { if (strpos($v,'current_page_item') !== FALSE) { $current = $k; } $links[$k] = $v.'</li>';}
  20.  
  21. $total = count($links)-1; $prev = (int)$current-1; $next = $current+1;
  22.  
  23. if ($current == $total) $next = '0'; // if you're on the last page, next will be the first
  24. if ($current == '0') $prev = $total; // if you're on the first page, prev will be the last
  25.  
  26. $prevLink = strip_tags($links[$prev],'<a>'); //remove the <li> tags
  27. $nextLink = strip_tags($links[$next],'<a>'); //remove the <li> tags
  28.  
  29. function change_anchor_text($link, $newtext, $placement) {
  30. $pos1 = strpos($link, '>')+1;
  31. $pos2 = strpos($link, '<', 1);
  32. $anchortext = substr($link, $pos1, $pos2-$pos1);
  33.  
  34. if($placement == 'before') {
  35. return str_replace($anchortext, $newtext.' '.$anchortext, $link);
  36. }
  37. else if ($placement == 'after') {
  38. return str_replace($anchortext, $anchortext.' '.$newtext, $link);
  39. }
  40. else if ($placement == 'replace') {
  41. return str_replace($anchortext, $newtext, $link);
  42. }
  43.  
  44. }
  45. $prevLinkb = $prevLink;
  46. $prevLink = change_anchor_text($prevLink, ' &lt; View Previous Room ', 'replace'); /* link customization goes here */
  47. $nextLink = change_anchor_text($nextLink, ' View Next Room &gt; ', 'replace');
  48. $prepend = '';
  49. $separator = '<a href="/lodging/" class="norm">Return to All Rooms</a>';
  50. $separator2 = '<a href="#" class="norm">Check Availability Online</a>';
  51.  
  52. echo '<p class="pnpn">'.$prepend.$prevLink.$separator.$nextLink.'</p>';
  53.  
  54. }
  55. /* end prev/next links */
  56. ?>

URL: http://trepmal.com/scripts/previousnext-links-for-pages-in-wordpress/

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.