pmpro_getPaginationString

The Paid Memberships Pro pmpro getPaginationString function.

Description

pmpro_getPaginationString( $page, (number) $totalitems, (int) $limit = 15, (int) $adjacents = 1, (string) $targetpage = '/', (string) $pagestring = '&pn=' ); 

Parameters (6)

0. $page
The page.
1. $totalitems (number)
The totalitems.
2. $limit — Optional. (int) => 15
The limit.
3. $adjacents — Optional. (int) => 1
The adjacents.
4. $targetpage — Optional. (string) => '/'
The targetpage.
5. $pagestring — Optional. (string) => '&pn='
The pagestring.

Usage

  1. if ( !function_exists( 'pmpro_getPaginationString' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'paid-memberships-pro/includes/functions.php'; 
  3.  
  4. // The page. 
  5. $page = null; 
  6.  
  7. // The totalitems. 
  8. $totalitems = null; 
  9.  
  10. // The limit. 
  11. $limit = 15; 
  12.  
  13. // The adjacents. 
  14. $adjacents = 1; 
  15.  
  16. // The targetpage. 
  17. $targetpage = '/'; 
  18.  
  19. // The pagestring. 
  20. $pagestring = '&pn='; 
  21.  
  22. // NOTICE! Understand what this does before running. 
  23. $result = pmpro_getPaginationString($page, $totalitems, $limit, $adjacents, $targetpage, $pagestring); 
  24.  

Defined (1)

The function is defined in the following location(s).

/includes/functions.php  
  1. function pmpro_getPaginationString($page = 1, $totalitems, $limit = 15, $adjacents = 1, $targetpage = "/", $pagestring = "&pn=") 
  2. //defaults 
  3. if(!$adjacents) $adjacents = 1; 
  4. if(!$limit) $limit = 15; 
  5. if(!$page) $page = 1; 
  6. if(!$targetpage) $targetpage = "/"; 
  7.  
  8. //other vars 
  9. $prev = $page - 1; //previous page is page - 1 
  10. $next = $page + 1; //next page is page + 1 
  11. $lastpage = ceil($totalitems / $limit); //lastpage is = total items / items per page, rounded up. 
  12. $lpm1 = $lastpage - 1; //last page minus 1 
  13.  
  14. /** 
  15. Now we apply our rules and draw the pagination object. 
  16. We're actually saving the code to a variable in case we want to draw it more than once. 
  17. */ 
  18. $pagination = ""; 
  19. if($lastpage > 1) 
  20. $pagination .= "<div class=\"pmpro_pagination\""; 
  21. if(!empty($margin) || !empty($padding)) 
  22. $pagination .= " style=\""; 
  23. if($margin) 
  24. $pagination .= "margin: $margin;"; 
  25. if($padding) 
  26. $pagination .= "padding: $padding;"; 
  27. $pagination .= "\""; 
  28. $pagination .= ">"; 
  29.  
  30. //previous button 
  31. if ($page > 1) 
  32. $pagination .= "<a href=\"$targetpage$pagestring$prev\">« prev</a>"; 
  33. else 
  34. $pagination .= "<span class=\"disabled\">« prev</span>"; 
  35.  
  36. //pages 
  37. if ($lastpage < 7 + ($adjacents * 2)) //not enough pages to bother breaking it up 
  38. for ($counter = 1; $counter <= $lastpage; $counter++) 
  39. if ($counter == $page) 
  40. $pagination .= "<span class=\"current\">$counter</span>"; 
  41. else 
  42. $pagination .= "<a href=\"" . $targetpage . $pagestring . $counter . "\">$counter</a>"; 
  43. elseif($lastpage >= 7 + ($adjacents * 2)) //enough pages to hide some 
  44. //close to beginning; only hide later pages 
  45. if($page < 1 + ($adjacents * 3)) 
  46. for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++) 
  47. if ($counter == $page) 
  48. $pagination .= "<span class=\"current\">$counter</span>"; 
  49. else 
  50. $pagination .= "<a href=\"" . $targetpage . $pagestring . $counter . "\">$counter</a>"; 
  51. $pagination .= "..."; 
  52. $pagination .= "<a href=\"" . $targetpage . $pagestring . $lpm1 . "\">$lpm1</a>"; 
  53. $pagination .= "<a href=\"" . $targetpage . $pagestring . $lastpage . "\">$lastpage</a>"; 
  54. //in middle; hide some front and some back 
  55. elseif($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2)) 
  56. $pagination .= "<a href=\"" . $targetpage . $pagestring . "1\">1</a>"; 
  57. $pagination .= "<a href=\"" . $targetpage . $pagestring . "2\">2</a>"; 
  58. $pagination .= "..."; 
  59. for ($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++) 
  60. if ($counter == $page) 
  61. $pagination .= "<span class=\"current\">$counter</span>"; 
  62. else 
  63. $pagination .= "<a href=\"" . $targetpage . $pagestring . $counter . "\">$counter</a>"; 
  64. $pagination .= "..."; 
  65. $pagination .= "<a href=\"" . $targetpage . $pagestring . $lpm1 . "\">$lpm1</a>"; 
  66. $pagination .= "<a href=\"" . $targetpage . $pagestring . $lastpage . "\">$lastpage</a>"; 
  67. //close to end; only hide early pages 
  68. else 
  69. $pagination .= "<a href=\"" . $targetpage . $pagestring . "1\">1</a>"; 
  70. $pagination .= "<a href=\"" . $targetpage . $pagestring . "2\">2</a>"; 
  71. $pagination .= "..."; 
  72. for ($counter = $lastpage - (1 + ($adjacents * 3)); $counter <= $lastpage; $counter++) 
  73. if ($counter == $page) 
  74. $pagination .= "<span class=\"current\">$counter</span>"; 
  75. else 
  76. $pagination .= "<a href=\"" . $targetpage . $pagestring . $counter . "\">$counter</a>"; 
  77.  
  78. //next button 
  79. if ($page < $counter - 1) 
  80. $pagination .= "<a href=\"" . $targetpage . $pagestring . $next . "\">next »</a>"; 
  81. else 
  82. $pagination .= "<span class=\"disabled\">next »</span>"; 
  83. $pagination .= "</div>\n"; 
  84.  
  85. return $pagination; 
  86.