Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Wordpress Comments Loop

Featured Replies

Hey I've been trying to get this to work for a while now:

 

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

<div class="entry">
<h3><?php the_title(); ?><span class="comment_count"><?php comments_number('No Comments','One Comment','% Comments'); ?></span></h3>

  <?php the_content(); ?>

  <div class="post_details">
  <p>Author: <span class="author"><?php the_author(); ?>.</span></p>
<p>Posted On: <span class="date_posted"><?php echo get_the_date(); ?></span></p>
  </div>

</div> <!-- end entry div -->   

  <h3>Comments</h3>

  <?php if($comments) : ?>  
   <ul class="comments"> 
   <?php foreach($comments as $comment) : ?>
	<div class="comment_meta">
	<?php echo get_avatar(); ?>
		<span class="name"><a href="<?php comment_author_link(); ?>"><?php comment_author(); ?></a></span>
		<span class="date"><?php comment_date(); ?></span>
	</div>
           <?php if($comment->comment_approved == '0') : ?>  
               <li class="comment_content"><p>Your comment is awaiting approval.</p></li>  
           <?php endif; ?>  
			<li class="comment_content"><?php comment_text(); ?></li> 
   <?php endforeach; ?>  
   </ul>  
<?php else : ?>  
   <p>No comments currently for this post.</p>  
<?php endif; ?> 

<?php endwhile; else: ?>
<p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
<?php endif; ?>

  <?php comment_form(); ?>]

 

Can anyone see a fault with the code as all that's ever returned is 'No comments currently for this post.'

 

Or is this method now depreciated with the use of wp_list_comments(); ?

 

Thanks, Adam.

Edited by AdamConder

  • Author

I was thinking the same, but everywhere I've looked is the same. Surely need to store a SQL statement inside and check if anything has been returned?

 

Or is $comments a global variable that wp understands?

  • Author

Solved:

<?php $comments = get_comments(); ?>

Thanks for the prompt :p

I was thinking the same, but everywhere I've looked is the same. Surely need to store a SQL statement inside and check if anything has been returned?

 

Or is $comments a global variable that wp understands?

 

I think $comment is a global variable, not $comments.

 

You could try using have_comments() to perform the check for comments - http://codex.wordpress.org/Function_Reference/have_comments

  • Author

Problem now is how can I specify the post id? It's returning all comments

 

something like:

<?php $comments = get_comments($post->ID); ?>

Problem now is how can I specify the post id? It's returning all comments

 

something like:

<?php $comments = get_comments($post->ID); ?>

Almost, I think it should be:

 

<?php $comments = get_comments(get_the_ID()); ?>

 

Ref: http://codex.wordpress.org/Function_Reference/get_the_ID

Edited by andyl

  • Author

This fixed it:

 

   <?php $post_id = $post->ID; ?>
<?php $comments = get_comments(array('post_id' => $post_id)); ?>

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.