Forum Moderators: coopster

Message Too Old, No Replies

Wordpress Comments - comment count is incorrect

         

cyberpunkstudio

11:30 pm on Jul 10, 2009 (gmt 0)

10+ Year Member



I am using Wordpress and have just built my own theme.

In my comments at the top of it it displays the total number of comments, if 5 people have commented it would display the string '5 comments', however I have already had 4 comments and it displays 'No Comments', which is really strange it doesnt seem to match the total number of comments left by users.

This is my comments.php:

<?php
if ('comments.php' == basename($_SERVER['SCRIPT_FILENAME'])) die ('Please do not load this page directly. Thanks!');
if (!empty($post->post_password)) {
if ($_COOKIE['wp-postpass_' . COOKIEHASH] != $post->post_password) {
?>

<h2>This Post is Password Protected!</h2>
<p>Please enter the password to view Comments.</p>

<?php return;
}
}
?>

<?php $i = 0; ?>
<?php if ($comments) : ?>

<div class="comment_top">
<h2 id="num_comments" class="floatleft"><?php comments_number('No Comments', 'One Comment', '% Comments' );?></h2>
<span><a href="#respond" class="leave_comment floatright">Leave a Comment</a></span>
</div>

<ul class="commentlist">
<?php foreach ($comments as $comment) : ?>
<?php $i++; ?>
<?php if(get_comment_type() == 'comment'): ?>

<li id="comment-<?php comment_ID(); ?>" <?php include (TEMPLATEPATH . '/includes/comments-author-class.php'); ?>>
<div class="comment_wrap">
<div class="comment_author">
<!-- Link to default avater! -->
<?php if (function_exists('get_avatar')) { echo get_avatar(get_comment_author_email(),'82', $default='http://www.example.com/wp-content/themes/example/images/default-avatar.jpg'); } ?>
<p>
<?php echo $i; ?>.
<?php comment_author_link(); ?>
<small><?php comment_date('M jS, Y'); ?></small>
</p>
</div>
<div class="comment_text">
<div class="comment_tail"></div>

<?php comment_text(); ?>
<?php if ($comment->comment_approved == '0') : ?>
<em class="comment_moderation"><?php _e('Your comment is awaiting moderation.<br /> You do not need to resubmit') ?></em>
<?php endif; ?>
</div>
</div>
</li>

<?php endif; ?>
<?php endforeach; ?>
</ul>

<ol>
<?php foreach ($comments as $comment) :
$comment_type = get_comment_type();
if($comment_type != 'comment') { ?>
<li><?php comment_author_link() ?></li>
<?php } endforeach; ?>
</ol>



<?php else : ?>

<?php if ($post->comment_status == 'open') : ?>
<p>There are no comments yet, add one below.</p>
<? else : ?>
<p>Comments are closed.</p>
<?php endif; ?>

<?php endif; ?>

<?php if ('open' == $post->comment_status) : ?>

<div id="respond">
<a name="respond"></a>

<h3><?php comment_form_title( 'Leave a Comment', 'Leave a Reply to %s' ); ?></h3>
<?php if ( get_option('comment_registration') && !$user_ID ) : ?>
<p>You must be <a href="<?php bloginfo('url'); ?>/wp-login.php?redirect_to=<?php the_permalink(); ?>">logged in</a> to post a comment.</p>

<?php else : ?>
<form action="<?php echo get_option('siteurl'); ?>/wp-comments-post.php" method="post" id="commentform">

<?php comment_id_fields(); ?>

<?php if ( $user_ID ) : ?>

<p>Logged in as <a href="<?php echo get_option('siteurl'); ?>/wp-admin/profile.php"><?php echo $user_identity; ?></a>.
<a href="<?php echo get_option('siteurl'); ?>/wp-login.php?action=logout" title="Log out of this account">Logout</a></p>
<?php else : ?>

<div>
<label for="author" class="detail">Name:</label>
<input type="text" name="author" id="author" value="<?php echo $comment_author; ?>" class="input" />
<label for="author"><small><?php if ($req) echo "(Required)"; ?></small></label>
</div>

<div>
<label for="email" class="detail">Email:</label>
<input type="text" name="email" id="email" value="<?php echo $comment_author_email; ?>" size="50" class="input" />
<label for="email"><small><?php if ($req) echo "(Required)"; ?> (Will not be displayed)</small></label>
</div>

<div>
<label for="url" class="detail">Website:</label>
<input type="text" name="url" id="url" value="<?php echo $comment_author_url; ?>" size="50" class="input"/>
</div>

<?php endif; ?>

<div>
<label for="comment" class="comment_box detail">Comment:</label>
<textarea name="comment" id="data" cols="60" rows="7" tabindex="4"></textarea>
</div>

<div>
<input name="submit" type="submit" id="submit" tabindex="5" value="Submit Comment" />
</div>

<?php do_action('comment_form', $post->ID); ?>

</form>

<p class="get_avatar"><a href="http://www.example.com/" rel="external">Get a Gravatar</a> Get your own Avatar and displayed here for free</p>

<div id="cancel-comment-reply">
<small><?php cancel_comment_reply_link() ?></small>
</div>

</div>

<?php endif; ?>
<?php endif; ?>

I followed this tutorial on how to create this comments section and I tried using theirs it worked perfectly, but I dont understand why my one keeps displaying the wrong total amount of comments.

I have cleared my database and reinstalled wordpress but i still have this problem I think it maybe my script. It may not be the comments.php page but could possibly be some other script that is affecting it, I'm not sure, does any one have any ideas? Or any way to fix this?

Hope you can help.

Thank you

[edited by: dreamcatcher at 6:03 am (utc) on July 11, 2009]
[edit reason] use example.com. Thanks. [/edit]

PokeTech

4:27 am on Jul 11, 2009 (gmt 0)

10+ Year Member



Where is your $comments variable coming from? Unless I'm mistaken in the script you provided $comments has never been set, this may be causing it to display "No Comments".

cyberpunkstudio

12:12 pm on Jul 11, 2009 (gmt 0)

10+ Year Member



I think $comments is set in anohter functions file in one of wordpress' directory, I will go and have a look

cyberpunkstudio

5:00 pm on Jul 11, 2009 (gmt 0)

10+ Year Member



The $comments is a default variable set by Wordpress, within its software its automatically set.

But part from that I really dont know whats causing this problem. I tried installing a new theme and that works fine so I presume it must be my own script.

It may not be the comments.php thats wrong it maybe to do with other files but I'm not sure.

Does anyone know how to fix this please? OR any one know of a wordpress developer I can ask help from?

Thanks