0
$query = db_select('node', 'n');
$query = $query->condition('n.type', 'article', '=')
  ->fields('n', array('nid', 'title', 'uid'))
  ->extend('PagerDefault') 
  ->limit(1);

$result = $query->execute();
while ($record = $result->fetchObject()) {
  echo $record ->title."<br/>";
}

echo theme('pager');

If I put the code in page--node--3.tpl.php there is a pager under the title. When I put the same code in page--taxonomy.tpl.php the title is output, but there is no pager. Why?

If there is a term ID, the pager will disappear.

template.php

function mythemename_preprocess_page(&$vars)  {
 if (isset($vars['node']->type)) {
$nodetype = $vars['node']->type;
$vars['theme_hook_suggestions'][] ='page__' . $nodetype;
}

}
2
  • Try extending it as a pager query right at the beginning: db_select('n.type', 'n')->extend('PagerDefault')->condition('n.type', 'article', '=')-> Dec 8, 2012 at 7:57
  • i did as you told, it not work. could you do it with a test, i have added the overwrite template file function on my question.
    – 123
    Dec 8, 2012 at 8:39

1 Answer 1

0

I tried with the Bartik theme, and it works.

screenshot

The steps I followed as the following ones:

  • I copied the page.tpl.php file the Bartik theme has in its templates directory in the page--taxonomy.tpl.php file

  • Right before <?php print render($page['content']); ?>, I copied the following code.

    <?php
      $query = db_select('node', 'n');
      $query = $query->condition('n.type', 'article', '=')
        ->fields('n', array('nid', 'title', 'uid'))
        ->extend('PagerDefault') 
        ->limit(1);
    
      $result = $query->execute();
      foreach ($result as $node_obj) {
        echo $node_obj->title . "<br/>";
      }
    
      echo theme('pager');
    ?>
    
  • I selected another theme instead of the Bartik theme, disabled the Bartik theme, and then selected the Bartik theme as default one. (This step is probably not necessary, but I did it to be sure Drupal would notice I added a new template file.)

  • I visited http://tero.local/dr72/taxonomy/term/2.

If it doesn't work for you, I suggest trying with another theme, possibly one that comes with Drupal, for example the Bartik theme. Be sure you are adding the template file to the theme Drupal is using. If the theme selected for the current user is another one, the template file is not being used, and that would explain why you are not getting any output.

This is what I suspect, as the code doesn't depend from arg(2). You just need to visit a page whose path starts with taxonomy, for example http://example.com/taxonomy/term/2, or http://example.com/taxonomy/term. (Replace example.com with the domain you should use for your site.)

Update

It works, but using the following code.

<?php
  $query = db_select('node', 'n');
  $query = $query->condition('n.type', 'article', '=')
    ->fields('n', array('nid', 'title', 'uid'))
    ->extend('PagerDefault')
    ->element(10) 
    ->limit(1);

  $result = $query->execute();
  foreach ($result as $node_obj) {
    echo $node_obj->title . "<br/>";
  }

  echo theme('pager', array('element' => 10));
?>

Apparently, there is a conflict between the pager added from Drupal, and the pager added from my code. This is the reason why such code should not be added to a template file. Rather than using this method, it is better to override the taxonomy term page with other methods, such as the Views module, or implementing hook_menu_alter() in a custom module.

Using the Bartik module this is the output I get:

  • When there aren't nodes using that taxonomy term

    screenshot

  • When there are nodes using that taxonomy term

    screenshot

Any different result could be caused from the code used for the specific theme used on the site. Understanding what exactly causes the different behavior seems a little outside the purpose of this site, as it is too localized to be helpful for future readers, considering that similar code should not written in a template file.

2
  • i show my theme, if your put your code in the page--taxonomy.tpl.php it not show the pager. expect to help me to see what's the reason cause it. many many thanks
    – 123
    Dec 8, 2012 at 9:49
  • the pager questions still exist in our company, i don't know why our DIY theme can't work.
    – 123
    Dec 8, 2012 at 9:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.