Parasolx

Professional in Drupal web development, theme designing, consultation and training

[Snippet] Numbering index for comment

07 Feb 2014 - 03:09 pm

Default comment system in Drupal providing variables $id to be used as index and numbering for each comment. However, when continue to another page, this number will start to 1 again. This snippet will help to determine the current number of comment per page and recalibrate for each pages.

Add these code into template.php for current theme.

<?php
function THEME_preprocess_comment(&$vars) {

  // Calculate the true comment index
  $page = (isset($_GET['page'])) ? (int)$_GET['page'] : 0;
  $comments_per_page = variable_get('comment_default_per_page_' . $vars['node']->type);
  $index = ( $page * $comments_per_page ) + $vars['id'];
  $vars['comment_index'] = l(
    '#' . $index, 'node/' . $vars['comment']->nid,
    array(
      'fragment'=>'comment-' . $vars['comment']->cid,
      'attributes' => array(
        'title' => t("Link to this comment")
      ),
    )
  );
}
?>

Replace or used $comment_index string in comment.tpl.php

<?php print $comment_index; ?>
Kategori: 
Tags: 
snippets