1

I want to display custom text on a node if there is no comments yet. I think i should check for comments count in preprocess_node, and somehow add the text below Comments title.

function MYTHEME_preprocess_node(&$variables, $hook) {
  if($variables['comment_count'] === '0') {
     //somehow i need to display text - There is not comments yet
  }
}

How can achieve this? here is an example of how it should looks like:

description

EDIT: Also i have found better solution, may be useful for someone. (using Zen theme - aka STARTERKIT modified) I have added comments-wrapper.tpl.php

And in a template.php added preprocess function of this wrapper

function MYTHEME_preprocess_comments_wrapper(&$variables) {
    $variables['MYTHEME_comment_title'] = t('Comments');
      $commented_node = $variables['node'];

      if(!empty($commented_node) && is_object($commented_node)){
        $comment_count = 0;
        if(isset($commented_node->comment_count)){
          $comment_count = $commented_node->comment_count;
        }
        $variables['MYTHEME_comment_title'] = format_plural($comment_count, '1 comment', '@count comments');
         if($comment_count == 0) {
            $variables['MYTHEME_comment_title'] = t('Comments');
            $variables['empty_comments'] = t('There is no comments yet');
     }
}

And in a comments-wrapper.tpl.php i have printed out $empty_comments variable

  <?php print $empty_comments; ?>

1 Answer 1

4

In your node.tpl.php you could check the $comment_count variable to see if there are comments. If so, render the comments, if not display your alternet text.

Example:

<?php if ($comment_count > 0 ): ?>
  <?php print render($content['comments']); ?>
<?php else: ?>
        <strong> 0 comments yet</strong>
        <?php echo l('Be the first','commentpath'); ?>
<?php endif; ?>
0

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.