سؤال

I am attempting to create a simple pager within Drupal 7 on page.tpl.php within my theme. The aim of the pager is to show 5 'courses' per page, with a pager at the bottom.

After scrounging around online, this is the best I could get, which shows the first 5 items, and no pager:

$query = db_select('node', 'n')
    ->extend('PagerDefault')
    ->limit(5);
$query->fields('n', array('nid', 'title'))
    ->orderBy('created', 'DESC')
    ->condition('type', 'course');
$result = $query->execute();
$output = NULL;
foreach($result as $row) {
    $output .= $row->nid.': '.$row->title.'<br />';
}
$output .= theme('pager');
echo $output;

Could anyone assist in how to correctly pull through a pager? Some answers mention to include pager_default_initialize but apparently this is not required?

هل كانت مفيدة؟

المحلول

Managed to fix the issue I had by specifying the element, element 0 was already in use (hence it not working) changing to element 1 fixed it, I just changed

$output .= theme('pager');

to

$output .= theme('pager', array('element' => 1));

It seems that pager ID 0 was used by Views, by enabling the Archive block.

نصائح أخرى

For rendering pager with some query, you need call $query = $query->extend('PagerDefault')->limit(5); before execute it.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top