Pager at top and bottom of comments

Last modified: August 26, 2009 - 22:41

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>.

4.7

jferjan - May 30, 2008 - 16:15

apply this patch to get it working for 4.7

http://drupal.org/node/73748#comment-426393

for drupal 6

bboldi - December 9, 2008 - 10:38

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

Missing something

kawaku - February 7, 2009 - 18:51

bboldi, I am trying your snippet (added it on top of themes/garland/template.php) and it seems though that this function is never called.

In order to override it, should it not be (phptemplate_comment_wrapper) defined in includes/theme.inc? Because it is not in my Drupal (6.9). Or am I missing something else?

OK

kawaku - April 10, 2009 - 12:16

(Forgot to update... )

Newbie mistake! Wrong location, the snippet works fine. I was just changing the theme on the wrong location (should've been sites/all/themes/whatever_theme). Simply inserting theme('pager') in new page-forum.tpl.php and page-node.tpl.php works.

Thanks everyone for your

obamausa8 - June 30, 2009 - 08:31

Thanks everyone for your support, you're the best!!

[url=http://pret-auto.org][color=#FFFFFF][u]pret auto[/u][/color][/url]

 
 

Drupal is a registered trademark of Dries Buytaert.