Community Documentation

Pager at top and bottom of comments

Last updated September 8, 2012. Created by Paul Natsuo Kis... on May 4, 2007.
Edited by radiobuzzer, victor@gamedev.by, ronald_istos. Log in to edit this page.

Originally posted as http://drupal.org/node/79352#comment-223909

This snippet places a pager at the top of the comments on a node, in addition to the default pager at the bottom.

It is especially useful in forums. For an example theme called 'mytemplate,' add the following to template.php:

<?php
/**
* Add a pager at the top of a list of comments.
*/
function mytemplate_comment_wrapper($content) {
 
$comments_per_page = _comment_get_display_setting('comments_per_page');
 
$content = theme('pager', NULL, $comments_per_page, 0) . $content;
  return
theme_comment_wrapper($content);
}
?>

Details

The function comment_render() in comment.module calls theme('comment_wrapper', $output). This theming call will match any hook *_comment_wrapper(), or give up and fall back to theme_comment_wrapper() (which is also in comment.module). This is a great place to jump in and add an extra pager, because the default function only wraps the comments in a <div>.

Line-by-line, the above snippet does the following:

  1. Retrieve the number of comments per page, which is needed for the pager. This line is copied directly from near the top of comment_render().
  2. Prepend a pager to the content (which has already been given a pager at the end). This line is also found in comment.module and allows other theming applied to the pager to function properly.
  3. Use the default theming function from comment.module to wrap the whole set of pager + comments + pager in a <div>.

Comments

for drupal 6

the same code for drupal 6

function phptemplate_comment_wrapper($content, $node) {

  $comments_per_page = _comment_get_display_setting('comments_per_page');
   $content = theme('pager', NULL, $comments_per_page, 0) . $content; 

if (!$content || $node->type == 'forum') {
    return '<div id="comments">'. $content .'</div>';
  }
  else {
    return '<div id="comments"><h2 class="comments">'. t('Comments') .'</h2>'. $content .'</div>';
  }
}

----------------------------------------
Boldizsár Bednárik ing.
http://www.bboldi.com

----------------------------------------
Boldizsár Bednárik ing.

http://notifier.b2labs.com | http://oglasisrbije.com | http://onlineszotar.com | http://b2hq.com

One change

I had to make one change to this code for it not to give me an error (The second argument for _comment_get_display_setting):

<?php
function phptemplate_comment_wrapper($content, $node) {

 
$comments_per_page = _comment_get_display_setting('comments_per_page', $node);
  
$content = theme('pager', NULL, $comments_per_page, 0) . $content;

if (!
$content || $node->type == 'forum') {
    return
'<div id="comments">'. $content .'</div>';
  }
  else {
    return
'<div id="comments"><h2 class="comments">'. t('Comments') .'</h2>'. $content .'</div>';
  }
}
?>

Drupal 7?

Please, could anybody post here modification for Drupal 7?

in template.php function

in template.php

function mytemplate_preprocess_comment_wrapper(&$vars) {
$vars['top_pager'] = theme('pager');
}

and add in comment-wrapper.tpl.php

  <?php if (isset($top_pager)): ?>
  <nav>
    <?php print $top_pager; ?>
  </nav>
  <?php endif; ?>

Great, thanks!

Great, thanks!

Hello, does this only work

Hello,

does this only work for the Bartik theme? In my adromeda theme (Drupal 7) this code have no effect. :-( Any Idea?

Best regards
Frank

About this page

Drupal version
Drupal 5.x, Drupal 6.x
Audience
Designers/themers

Theming Guide

Drupal’s online documentation is © 2000-2013 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution-ShareAlike 2.0. PHP code is distributed under the GNU General Public License. Comments on documentation pages are used to improve content and then deleted.