• Hello,

    For example, I have the following code on the main page (index.php):

    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    <div id="entry">
    <h3><?php the_title(); ?></h3>
    <?php the_content(); ?>
    — <?php the_time('d F, Y') ?> | <a href="#"><?php comments_number() ?></a>
    <?php endwhile; ?>
    <?php endif; ?>

    And let’s assume I don’t have any comments right now. Then how do I hide the empty link and the “|” symbol? I don’t want to display “0 comments” or anything like it. Just want it to dissapear if no comments added, and then appear if at least one comment was added.

    I have plugin installed that manages parameters of comments_number, so it’s not an option to modify it.

    I tried to use <?php if ( have_comments() ) : ?> and other similar code infront of the comments_number, but without any result.

Viewing 3 replies - 1 through 3 (of 3 total)
  • ?


    You could use the parameters to define what to output according to the number of comments.
    The parameters go as follows: comments_number(‘zero comments’, ‘one comment’, ‘more than one comments’)
    So, in your case, the best I figure you could use is this:
    <?php the_time('d F, Y') ?> <a href="#"><?php comments_number('', '| 1 comment', '| % comments') ?></a>
    Though the pipe will be inside the link (and that’s not right). But I’d suggest you to avoid using the pipe and use CSS to arrange/style elements.

    I don’t know if you still need it, but here’s a solution I’ve found:

    <?php $comment_count = get_comment_count($post->ID); ?>
    <?php if ($comment_count['approved'] > 0) : ?>your html code<?php endif; ?>

    Thanks koyder — that helped me do what I was trying to do, anyway. I don’t really understand have_comments() but it clearly is not for detecting comments. get_comment_count() does just that. Here’s what I was trying to do:

    If comments are open or there are comments, show the comment count.
    example: 0 comments or 50 comments

    If it failed the comments-are-open condition, add a notice.
    example: 50 comments (comments are closed)

    If it failed both the comments-are-open and the there-are-comments conditions, ie there 0 comments and comments are closed, don’t show anything (This would be the state if you had never opened that post to comments. It could also be used to save you the embarrassment of advertising the fact that no one had anything to say before you closed your comments and put your post out of its misery.)

    <?php $comment_count = get_comment_count($post->ID);
    if ( comments_open() || $comment_count['approved'] > 0 ) : comments_number('0 comments','1 comment','% comments');
    if ( !comments_open() ) : ?> (comments are closed)<?php endif; ?>
    <?php endif; ?>
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘If comments number in Post Loop’ is closed to new replies.