Drupal pagination help I am not sure how this works, hence need a little help. I am fetching some nodes using db_select and rendering this in a foreach loop in a custom template file. I would give a hint of my code :

$nodes = db_select('node', 'n')
// Some fields...
// Some conditions...
// -&gt;execute();
$nodes = $nodes-&gt;fetchAll();
return theme('my_custom_callback', array('nodes' =&gt; $nodes));


Now I get this $nodes in my tpl file and print them to the design. But what I want is to have the Drupal default pagination effect (probably using the PagerDefault facility). I have come accross many examples of the use of PagerDefault but I am not sure how to render the pagination in my tpl file.

Please note using a Drupal view or a table theme is not an option in my case.

Help appreciated...
Well as no one responded, I searched a bit and found a normal PHP function to achieve the same. I know it may not be a Drupal way, but its for them who are looking for a quick solution for now (like me). But would like to add that other answers to achieve this the specially the Drupal way is very much welcome. Also please note, I almost copy pasted the actual function, so you probably may want to modify that as per your UI (for actual function I have given the reference below) :

// This is the menu callback where $data are the records fetched to be displayed in this page, and $total_count is the total number of count of all the records.

$pager = pagination($current_page, $total_count, $record_limit, 2, "url-menu-page?page=%d");

$return_val = theme('my_callback_function', array('data' =&gt; $data, 'pager' =&gt; $pager));

return $return_val;


And here goes the actual pagination building function :

/**
 * Displays pagination links based on given parameters
 *
 * @param int $currentPage - current page
 * @param int $itemCount - number of items to paginate, used to calculate total number of pages
 * @param int $itemsPerPage - number of items per page, used to calculate total number of pages
 * @param int $adjacentCount - half the number of page links displayed adjacent to the current page
 * @param (string|callable) $pageLinkTemplate - pagination URL string containing %d placeholder or a callable function that accepts page number and returns page URL
 * @param boolean $showPrevNext - whether to show previous and next page links
 * @return void
 */
function pagination($currentPage, $itemCount, $itemsPerPage, $adjacentCount, $pageLinkTemplate, $showPrevNext = true) {
  $pager = '';
  $firstPage = 1;
  $lastPage  = ceil($itemCount / $itemsPerPage);
  if ($lastPage == 1) {
      return;
  }
  if ($currentPage &lt;= $adjacentCount + $adjacentCount) {
      $firstAdjacentPage = $firstPage;
      $lastAdjacentPage  = min($firstPage + $adjacentCount + $adjacentCount, $lastPage);
  } elseif ($currentPage &gt; $lastPage - $adjacentCount - $adjacentCount) {
      $lastAdjacentPage  = $lastPage;
      $firstAdjacentPage = $lastPage - $adjacentCount - $adjacentCount;
  } else {
      $firstAdjacentPage = $currentPage - $adjacentCount;
      $lastAdjacentPage  = $currentPage + $adjacentCount;
  }
  $pager = '&lt;div class="pagination-hld"&gt;&lt;h2 class="element-invisible"&gt;Pages&lt;/h2&gt;&lt;div class="item-list"&gt;&lt;ul class="pager"&gt;';
  if ($currentPage != $firstPage) {
    $pager .=  '&lt;li class="pager-first first"&gt;&lt;a href="' . (is_callable($pageLinkTemplate) ? $pageLinkTemplate($firstPage) : sprintf($pageLinkTemplate, $firstPage)) . '"&gt;&amp;laquo; first&lt;/a&gt;&lt;/li&gt;';  
  }

  if ($showPrevNext) {
      if ($currentPage == $firstPage) {
      //$pager .= '&lt;li title="Go to first page" class="pager-first first"&gt;&lt; previous&lt;/li&gt;';
      } else {
      $pager .= '&lt;li title="Go to previous page" class="pager-previous"&gt;&lt;a title="Go to previous page" href="' . (is_callable($pageLinkTemplate) ? $pageLinkTemplate($currentPage - 1) : sprintf($pageLinkTemplate, $currentPage - 1)) . '"&gt;&amp;lsaquo; previous&lt;/a&gt;&lt;/li&gt;';
      }
  }
  if ($firstAdjacentPage &gt; $firstPage) {
      if ($firstAdjacentPage &gt; $firstPage + 1) {
      $pager .=  '&lt;span&gt;...&lt;/span&gt;';
      }
  }
  for ($i = $firstAdjacentPage; $i &lt;= $lastAdjacentPage; $i++) {
      if ($currentPage == $i) {
      $pager .=  '&lt;li class="pager-current"&gt;' . $i . '&lt;/li&gt;';
      } else {
      $pager .=  '&lt;li class="pager-item"&gt;&lt;a title="Go to page '.$i.'" href="' . (is_callable($pageLinkTemplate) ? $pageLinkTemplate($i) : sprintf($pageLinkTemplate, $i)) . '"&gt;' . $i . '&lt;/a&gt;&lt;/li&gt;';
      //$pager .=  "&lt;li class='pager-item'&gt;&lt;a title=\"Go to page $i\" href=\"' . (is_callable($pageLinkTemplate) ? $pageLinkTemplate($i) : sprintf($pageLinkTemplate, $i)) . '\"&gt;$i&lt;/a&gt;&lt;/li&gt;\r\n";
      }
  }
  if ($lastAdjacentPage &lt; $lastPage) {
      if ($lastAdjacentPage &lt; $lastPage - 1) {
      $pager .=  '&lt;span&gt;...&lt;/span&gt;';
      }
  }
  if ($showPrevNext) {
      if ($currentPage == $lastPage) {
      //$pager .=  '&lt;li class="pager-next"&gt;&lt;span&gt;next &gt;&lt;/span&gt;&lt;/li&gt;';
      } else {
      $pager .=  '&lt;li class="pager-next"&gt;&lt;a href="' . (is_callable($pageLinkTemplate) ? $pageLinkTemplate($currentPage + 1) : sprintf($pageLinkTemplate, $currentPage + 1)) . '"&gt;next &amp;rsaquo;&lt;/a&gt;&lt;/li&gt;';
      }
  }
  if ($currentPage != $lastPage) {
    $pager .=  '&lt;li class="pager-last last"&gt;&lt;a href="' . (is_callable($pageLinkTemplate) ? $pageLinkTemplate($lastPage) : sprintf($pageLinkTemplate, $lastPage)) . '"&gt;last &amp;raquo;&lt;/a&gt;&lt;/li&gt;';
  }
  $pager .=  '&lt;/ul&gt;&lt;/div&gt;&lt;/div&gt;';

  return $pager;
}


Hope this helps somebody...

Original code credit goes to the author of this blog : http://salman-w.blogspot.in/2014/04/stackoverflow-like-pagination.html