4.6.x pager.inc theme_pager($tags = array(), $limit = 10, $element = 0, $attributes = array())
4.7.x pager.inc theme_pager($tags = array(), $limit = 10, $element = 0, $parameters = array())
5.x pager.inc theme_pager($tags = array(), $limit = 10, $element = 0, $parameters = array())
6.x pager.inc theme_pager($tags = array(), $limit = 10, $element = 0, $parameters = array(), $quantity = 9)
7.x pager.inc theme_pager($variables)

Returns HTML for a query pager.

Menu callbacks that display paged query results should call theme('pager') to retrieve a pager control so that users can view other results. Format a list of nearby pages with additional query results.

Parameters

$variables: An associative array containing:

  • tags: An array of labels for the controls in the pager.
  • element: An optional integer to distinguish between multiple pagers on one page.
  • parameters: An associative array of query string parameters to append to the pager links.
  • quantity: The number of pages in the list.

Related topics

28 theme calls to theme_pager()
blog_page_last in modules/blog/blog.pages.inc
Menu callback; displays a Drupal page containing recent blog entries of all users.
blog_page_user in modules/blog/blog.pages.inc
Menu callback; displays a Drupal page containing recent blog entries of a given user.
comment_admin_overview in modules/comment/comment.admin.inc
Form builder for the comment overview administration form.
dblog_overview in modules/dblog/dblog.admin.inc
Page callback: Displays a listing of database log messages.
dblog_top in modules/dblog/dblog.admin.inc
Page callback: Shows the most frequent log messages of a given event type.

... See full list

File

includes/pager.inc, line 320
Functions to aid in presenting database results as a set of pages.

Code

function theme_pager($variables) {
  $tags = $variables['tags'];
  $element = $variables['element'];
  $parameters = $variables['parameters'];
  $quantity = $variables['quantity'];
  global $pager_page_array, $pager_total;

  // Calculate various markers within this pager piece:
  // Middle is used to "center" pages around the current page.
  $pager_middle = ceil($quantity / 2);

  // current is the page we are currently paged to
  $pager_current = $pager_page_array[$element] + 1;

  // first is the first page listed by this pager piece (re quantity)
  $pager_first = $pager_current - $pager_middle + 1;

  // last is the last page listed by this pager piece (re quantity)
  $pager_last = $pager_current + $quantity - $pager_middle;

  // max is the maximum page number
  $pager_max = $pager_total[$element];

  // End of marker calculations.
  // Prepare for generation loop.
  $i = $pager_first;
  if ($pager_last > $pager_max) {

    // Adjust "center" if at end of query.
    $i = $i + ($pager_max - $pager_last);
    $pager_last = $pager_max;
  }
  if ($i <= 0) {

    // Adjust "center" if at start of query.
    $pager_last = $pager_last + (1 - $i);
    $i = 1;
  }

  // End of generation loop preparation.
  $li_first = theme('pager_first', array(
    'text' => isset($tags[0]) ? $tags[0] : t('« first'),
    'element' => $element,
    'parameters' => $parameters,
  ));
  $li_previous = theme('pager_previous', array(
    'text' => isset($tags[1]) ? $tags[1] : t('‹ previous'),
    'element' => $element,
    'interval' => 1,
    'parameters' => $parameters,
  ));
  $li_next = theme('pager_next', array(
    'text' => isset($tags[3]) ? $tags[3] : t('next ›'),
    'element' => $element,
    'interval' => 1,
    'parameters' => $parameters,
  ));
  $li_last = theme('pager_last', array(
    'text' => isset($tags[4]) ? $tags[4] : t('last »'),
    'element' => $element,
    'parameters' => $parameters,
  ));
  if ($pager_total[$element] > 1) {
    if ($li_first) {
      $items[] = array(
        'class' => array(
          'pager-first',
        ),
        'data' => $li_first,
      );
    }
    if ($li_previous) {
      $items[] = array(
        'class' => array(
          'pager-previous',
        ),
        'data' => $li_previous,
      );
    }

    // When there is more than one page, create the pager list.
    if ($i != $pager_max) {
      if ($i > 1) {
        $items[] = array(
          'class' => array(
            'pager-ellipsis',
          ),
          'data' => '…',
        );
      }

      // Now generate the actual pager piece.
      for (; $i <= $pager_last && $i <= $pager_max; $i++) {
        if ($i < $pager_current) {
          $items[] = array(
            'class' => array(
              'pager-item',
            ),
            'data' => theme('pager_previous', array(
              'text' => $i,
              'element' => $element,
              'interval' => $pager_current - $i,
              'parameters' => $parameters,
            )),
          );
        }
        if ($i == $pager_current) {
          $items[] = array(
            'class' => array(
              'pager-current',
            ),
            'data' => $i,
          );
        }
        if ($i > $pager_current) {
          $items[] = array(
            'class' => array(
              'pager-item',
            ),
            'data' => theme('pager_next', array(
              'text' => $i,
              'element' => $element,
              'interval' => $i - $pager_current,
              'parameters' => $parameters,
            )),
          );
        }
      }
      if ($i < $pager_max) {
        $items[] = array(
          'class' => array(
            'pager-ellipsis',
          ),
          'data' => '…',
        );
      }
    }

    // End generation.
    if ($li_next) {
      $items[] = array(
        'class' => array(
          'pager-next',
        ),
        'data' => $li_next,
      );
    }
    if ($li_last) {
      $items[] = array(
        'class' => array(
          'pager-last',
        ),
        'data' => $li_last,
      );
    }
    return '<h2 class="element-invisible">' . t('Pages') . '</h2>' . theme('item_list', array(
      'items' => $items,
      'attributes' => array(
        'class' => array(
          'pager',
        ),
      ),
    ));
  }
}

Comments

sreynen’s picture

When calling theme('pager') outside a pager query, it's necessary to initialize the pager first with a call to pager_default_initialize()

rakesh.gectcr’s picture

$per_page = 10;
// Initialize the pager
$current_page = pager_default_initialize(count($rows), $per_page);
// Split your list into page sized chunks
$chunks = array_chunk($rows, $per_page, TRUE);
// Show the appropriate items from the list
$output = theme('table', array('header' => $header, 'rows' => $chunks[$current_page]));
// Show the pager
$output .= theme('pager', array('quantity',count($rows)));

mustimpress’s picture

I believe there's a common syntax error floating around the web regarding this.

The following doesn't work in D7:

$num_of_pager_links = 5;
echo theme('pager', array('quantity', $num_of_pager_links));

Whereas an associated array of variables does:

$num_of_pager_links = 5;
echo theme('pager', array('quantity' => $num_of_pager_links));
gsmwe’s picture

Thanks for your solution and it works fine but I have an item_list that should be shown as links with pager.
How can I theme_list_item(...).. with ajax pager functionality.
Really appreciate.
Thanks

rkedde’s picture

Is there someone who can tell me how to set the pager limit?
In Drupal 6 there is a simple way like this:

theme_pager($tags = array(), $limit = 10, $element = 0, $parameters = array(), $quantity = 9)

But in Drupal 7 it look like this:

theme_pager($variables)

Thanks for your help.

hatuhay’s picture

The answer is on #1, but for more explicit example see http://api.drupal.org/api/drupal/includes!pager.inc/function/pager_defau...

aaron’s picture

More likely, you will probably need to do something like $query = db_select('some_table')->extend('PagerDefault'); (which you will find at pager_default_initialize).

sirajs’s picture

I'm also trying to change the pager limit in Drupal 7. Specifically I want to take the content area on my webpage and display all content nodes ... not just the default 10 nodes that seems to be the limit.

.... But I don't understand the previous comments and the references to pager_default_initialize. Disclaimer: I have only a shallow understanding of Drupal 7 - so I'm largely operating on educated guess work to get things working.

I have a custom theme called: mytheme and here's what I have done so far. Note that after each attempt, I have used the Drupal 7 administrative interface on my website to clear all caches. I've also reloaded the pages after clearing my browser cache.

  • I have tried to modify my template.php file by adding the pager_default_initialize function call to my mytheme_preprocess_page(&$vars) function as follows:
    function mytheme_preprocess_page(&$vars) 
    {
        .....  
        pager_default_initialize($total, $limit=30, $element = 0);
    }
    

    This doesn't work.

  • I have tried to modify the same function in template.php as follows:
    function mytheme_preprocess_page(&$vars) 
    {  
        // Select nid from published nodes with 20 pager limit 
        $query = db_select('node', 'n')
            ->condition('status', 1)
            ->extend('PagerDefault')
            ->limit(30)
            ->fields('n', array('nid'));
    
        $results = $query->execute();
        
        pager_default_initialize($total, $limit, $element = 0);
    }
    

    This doesn't work either. I've tried running the code without the call to pager_default_initialize too.

  • I've also tried adding a separate function to my template.php file as follows - clearly this is Drupal 6 format function, but thought I'd try anyway:
    function mytheme_pager($tags = array(), $limit = 30, $element = 0, $parameters = array(), $quantity = 9) 
    {
        if (drupal_is_front_page()) 
        {
            return "";
        }
        else 
        {
            return theme_pager($tags, $limit, $element, $parameters, $quantity);
        }
    }
    

    This doesn't work. It does remove the pager link buttons but nothing more.

  • Finally, I've tried the same function in Drupal 7 format: mytheme_pager($variables) with all combination of the above three code examples inside the function.

Absolutely any help pointing out the right direction to take would be helpful... in fact, if anyone can suggest a way to do this through the Drupal 7 Web Administration interface - that would be amazing! Thanks!

ckollars’s picture

Aaron, did you ever get a response to this. I'm trying to do the same but I'm not following on what is need to change the pager limit.

motomc’s picture

You can try this, it works for me in D7:

function [THEMENAME]_pager($variables) {
  if ($variables['quantity'] > 5) {
    $variables['quantity'] = 5;
  }
  return theme_pager($variables);
}
snoopy77’s picture

For what it's worth -- from within theme_pager ($variables), changing the value of $quantity does the trick.

a6hiji7’s picture

"theme_pager" does not have any call to "theme_pager_link", instead it has calls to "theme_pager_previous", "theme_pager_next" etc., which in turn call "theme_pager_link". Now, "theme_pager_link" accepts the "attributes" in the "variables" but the others do not. Does this mean, if I want to add an attribute (like changing the default titles), I have to override "theme_pager_previous" etc. ? Should not I be able to do that just by overriding "theme_pager"?

a6hiji7’s picture

"theme_pager" does not have any call to "theme_pager_link", instead it has calls to "theme_pager_previous", "theme_pager_next" etc., which in turn call "theme_pager_link". Now, "theme_pager_link" accepts the "attributes" in the "variables" but the others do not. Does this mean, if I want to add an attribute (like changing the default titles), I have to override "theme_pager_previous" etc. ? Should not I be able to do that just by overriding "theme_pager"?

a6hiji7’s picture

Sorry for double post, there is a bug in this page too - the comment preview shows the error that "Function, file, or topic" is required and shows a validation error with the search form.

zterry95’s picture

pager_default_initialize(888,10);
$args = array('quantity' => 12,'tags' => array('<<','<','','>','>>'));
theme('pager',$args);

Hope this can help.

wheelercreek’s picture

I'm using this in some custom code in my theme:

$query = db_select('node', 'n')->extend('PagerDefault')->limit(3); 

And I was also making use of an extra parameter (?c=27) in my URL string, I found I had to do this to make pagination work:

$args = array('element' => 1, 'parameters' => array('c' => 27));
theme('pager', $args); 

Hope that helps someone.

sheikh303’s picture

How a fragment can be added with each pager item link. i.e.
http://example.com/listing-page?page=3#content-section

akki123’s picture

I am using paging for data received from webservice.
Here is how to add a parameter type to url ....?type=1&page=2

<?php
theme('pager', array('parameters'=> array('type'=>1), array('quantity', 9) ) );
?>
myl6858’s picture

How do I add class "last" to the last pager item?

nehapandya55’s picture

Using theme_pager_link you can achieve this:
override this function in template.php and replace theme with your theme name.
function theme_pager_link($variables) {
$text = $variables['text'];
$page_new = $variables['page_new'];
$element = $variables['element'];
$parameters = $variables['parameters'];
$attributes = $variables['attributes'];

$page = isset($_GET['page']) ? $_GET['page'] : '';
if ($new_page = implode(',', pager_load_array($page_new[$element], $element, explode(',', $page)))) {
$parameters['page'] = $new_page;
}

$query = array();
if (count($parameters)) {
$query = drupal_get_query_parameters($parameters, array());
}
if ($query_pager = pager_get_query_parameters()) {
$query = array_merge($query, $query_pager);
}

// Set each pager link title
if (!isset($attributes['title'])) {
static $titles = NULL;
if (!isset($titles)) {
$titles = array(
t('« first') => t('Go to first page'),
t('‹ previous') => t('Go to previous page'),
t('next ›') => t('Go to next page'),
t('last »') => t('Go to last page'),
);
}
if (isset($titles[$text])) {
$attributes['title'] = $titles[$text];
}
elseif (is_numeric($text)) {
$attributes['title'] = t('Go to page @number', array('@number' => $text));
}
}

// @todo l() cannot be used here, since it adds an 'active' class based on the
// path only (which is always the current path for pager links). Apparently,
// none of the pager links is active at any time - but it should still be
// possible to use l() here.
// @see http://drupal.org/node/1410574
$attributes['href'] = url($_GET['q'], array('query' => $query));
return '' . check_plain($text) . '';
}

rohankumar.1524’s picture

Hi,
There is not options for custom attributes in theme_pager function.
I need to add custom attributes like class 'pagination' without using pager class but unfortunately, I am unable to do it.
So I think the return string should be,

return '' . t('Pages') . '' . theme('item_list', array(
'items' => $items,
'attributes' => array('class' => array_merge(array('pager'),$attributes['class'])),
));

One more thing to do, is that we need to check for the empty $attributes, if it is not set then define it with class key like $attributes=array('class'=>array());

I found a solution on http://drupal.stackexchange.com/questions/95762/how-to-change-pager-clas... but it not worked for me.

Valera Tumash’s picture

How can I always show previous and next pager elements even if I am on the first or last pages?

aniket.mohite88’s picture

bruceci’s picture

I click a ajax enabled button that obtain a pagered table from ajax callback , the pager link url is "system/ajax?page=2" that lead me to a blank page . what should i do?.