Advertisement
Guest User

WP Custom Query Pagination

a guest
May 24th, 2010
5,760
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.02 KB | None | 0 0
  1. <?php
  2. /**
  3.  *
  4.  * This module demonstrates pagination for a Custom Query in
  5.  * WordPress. It should work as a template in the default theme.
  6.  * Most of the formatting has been left out to make the core
  7.  * coding stand out.
  8.  *
  9.  * Author: Mac McDonald
  10.  * Contact using About->Contact Us at BluegrassMiataClub.com
  11.  *
  12.  */
  13. /*
  14. Template Name: Demo Paginate
  15. */
  16. ?>
  17.  
  18. <?php get_header(); ?>
  19.  
  20.    <div id="content" class="narrowcolumn" role="main">
  21.  
  22. <?php $sql = "SELECT p.* from $wpdb->posts p
  23. WHERE p.post_type = 'post'
  24. AND p.post_status = 'publish'
  25. ORDER by p.post_title";
  26. $mypages = $wpdb->get_results($sql);
  27.  
  28. if ($mypages) :
  29.    $limit = 3;  // The number of posts per page
  30.    $range = 5;   // The number of page links to show in the middle
  31.    $mypage = (isset($_GET['mypage'])) ? $mypage = $_GET['mypage'] : 1;
  32.    $start = ($mypage - 1) * $limit;
  33.    for ($i=$start;$i<($start + $limit);++$i) {
  34.       if ($i < sizeof($mypages)) {
  35.         // Process each element of the result array here
  36.         $post = $mypages[$i];
  37.         setup_postdata($post);
  38.         echo '<h2>';the_title();echo '</h2>';
  39.       }
  40.    }
  41.    echo '<br />';
  42.    echo _mam_paginate(sizeof($mypages),$limit,$range);
  43. else:
  44.    echo '<h2>Sorry, There are no Pages to list</h2>';
  45. endif;?>
  46. </div>
  47.  
  48. <?php get_footer(); ?>
  49.  
  50. <?php function _mam_paginate($numrows,$limit=10,$range=7) {
  51.  
  52.    $pagelinks = "<div class=\"pagelinks\">";
  53.    $currpage = $_SERVER['PHP_SELF'] . "?" . $_SERVER['QUERY_STRING'];
  54.    if ($numrows > $limit) {
  55.       if(isset($_GET['mypage'])){
  56.          $mypage = $_GET['mypage'];
  57.       } else {
  58.          $mypage = 1;
  59.       }
  60.       $currpage = $_SERVER['PHP_SELF'] . "?" . $_SERVER['QUERY_STRING'];
  61.       $currpage = str_replace("&mypage=".$mypage,"",$currpage); // Use this for non-pretty permalink
  62.       $currpage = str_replace("?mypage=".$mypage,"",$currpage); // Use this for pretty permalink
  63.       if($mypage == 1){
  64.          $pagelinks .= "<span class=\"pageprevdead\">&laquo PREV </span>";
  65.       }else{
  66.          $pageprev = $mypage - 1;
  67.          $pagelinks .= "<a class=\"pageprevlink\" href=\"" . $currpage .
  68.                "&mypage=" . $pageprev . "\">&laquo PREV </a>";
  69.       }
  70.       $numofpages = ceil($numrows / $limit);
  71.       if ($range == "" or $range == 0) $range = 7;
  72.       $lrange = max(1,$mypage-(($range-1)/2));
  73.       $rrange = min($numofpages,$mypage+(($range-1)/2));
  74.       if (($rrange - $lrange) < ($range - 1)) {
  75.          if ($lrange == 1) {
  76.             $rrange = min($lrange + ($range-1), $numofpages);
  77.          } else {
  78.             $lrange = max($rrange - ($range-1), 0);
  79.          }
  80.       }
  81.       if ($lrange > 1) {
  82.          $pagelinks .= "<a class=\"pagenumlink\" " .
  83.             "href=\"" . $currpage . "&mypage=" . 1 .
  84.             "\"> [1] </a>";
  85.          if ($lrange > 2) $pagelinks .= "&nbsp;...&nbsp;";
  86.       } else {
  87.          $pagelinks .= "&nbsp;&nbsp;";
  88.       }
  89.       for($i = 1; $i <= $numofpages; $i++){
  90.          if ($i == $mypage) {
  91.             $pagelinks .= "<span class=\"pagenumon\"> [$i] </span>";
  92.          } else {
  93.             if ($lrange <= $i and $i <= $rrange) {
  94.                $pagelinks .= "<a class=\"pagenumlink\" " .
  95.                         "href=\"" . $currpage . "&mypage=" . $i .
  96.                         "\"> [" . $i . "] </a>";
  97.             }
  98.          }
  99.       }
  100.       if ($rrange < $numofpages) {
  101.          if ($rrange < $numofpages - 1) $pagelinks .= "&nbsp;...&nbsp;";
  102.             $pagelinks .= "<a class=\"pagenumlink\" " .
  103.                "href=\"" . $currpage . "&mypage=" . $numofpages .
  104.                "\"> [" . $numofpages . "] </a>";
  105.       } else {
  106.          $pagelinks .= "&nbsp;&nbsp;";
  107.       }
  108.       if(($numrows - ($limit * $mypage)) > 0){
  109.          $pagenext = $mypage + 1;
  110.          $pagelinks .= "<a class=\"pagenextlink\" href=\"" . $currpage .
  111.                     "&mypage=" . $pagenext . "\"> NEXT &raquo;</a>";
  112.       } else {
  113.          $pagelinks .= "<span class=\"pagenextdead\"> NEXT &raquo;</span>";
  114.       }
  115.  
  116.    }
  117. $pagelinks .= "</div>";
  118. return $pagelinks;
  119. }
  120. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement