Title: Comment Problem
Last modified: August 19, 2016

---

# Comment Problem

 *  Resolved [mrspabs](https://wordpress.org/support/users/mrspabs/)
 * (@mrspabs)
 * [16 years ago](https://wordpress.org/support/topic/comment-problem-5/)
 * Hi, I am working on single.php. I wanted to insert the comments and the add comment
   form under the post. I have it working, except if gives me the error
 * > Warning: Invalid argument supplied for foreach() in /homepages/7/d299647896/
   > htdocs/wwwsaldef/_WordPress/wp-content/themes/saldef/single.php on line 40
 * My code:
 *     ```
       <?php
       /**
        * @package WordPress
        * @subpackage Default_Theme
        */
   
       get_header();
       ?>
   
       	<div id="content">
           <div id="left_content">
   
       	<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
   
       		<div style="font-size:11px;"><?php the_time('l, F jS, Y')  ?></div>
       		<!--<div class="navigation">
       			<div class="alignleft"><?php previous_post_link('&laquo; %link') ?></div>
       			<div class="alignright"><?php next_post_link('%link &raquo;') ?></div>
       		</div>-->
   
       			<div class="news_content">
   
       				<h1><?php the_title(); ?></h1>
       				<br class="clear" />
   
       				<?php the_content(''); ?>
   
       				<?php edit_post_link('Edit this entry','','.'); ?>
   
       			<div style="clear:both;"></div>
   
       <?php comments_template(); ?>
   
       <?php if ( have_comments() ) : ?>
       <ol id="commentlist">
   
       		<?php foreach ($comments as $comment) : ?>
   
       			<li class="<?php /* Style differently if comment author is blog author */ if ($comment->comment_author_email == get_the_author_email()) { echo 'author_comment_'; } ?><?php echo $oddcomment; ?>" id="comment-<?php comment_ID() ?>">
       				<?php if ($comment->comment_approved == '0') : ?>
       					<em>Your comment is awaiting moderation.</em>
       				<?php else : ?>
       					<div class="comment_intro">
       						<?php if (function_exists('gravatar')) { ?><a href="http://www.gravatar.com/" title="What is this?"><img src="<?php gravatar("X", 32, ""); ?>" class="gravatar" alt="Gravatar Icon" /></a><?php } ?>
       						<span class="comment_author"><?php comment_author_link() ?></span><br />
       						<span class="comment_meta"><a href="#comment-<?php comment_ID() ?>" title=""><?php comment_date('F jS, Y') ?> at <?php comment_time() ?></a></span>
       					</div>
       					<div class="entry">
       						<?php comment_text() ?>
       					</div>
       				<?php endif; ?>
       			</li>
   
       		<?php /* Changes every other comment to a different class */
       			if ('alt' == $oddcomment) $oddcomment = '';
       			else $oddcomment = 'alt';
       		?>
   
       </ol>
       <?php endforeach; /* end for each comment */ ?>
       <?php else : // If there are no comments yet ?>
       	<p></p>
       <?php endif; ?>
   
       <?php if ( comments_open() ) : ?>
       <h2 id="postcomment"><?php _e('Leave a comment'); ?></h2>
   
       <?php if ( get_option('comment_registration') && !is_user_logged_in() ) : ?>
       <p><?php printf(__('You must be <a href="%s">logged in</a> to post a comment.'), wp_login_url( get_permalink() ) );?></p>
       <?php else : ?>
       <P>Please fill out the below form to leave a comment or question. Comments are moderated and do not appear immediately.  </P>
       <form action="<?php echo get_option('siteurl'); ?>/wp-comments-post.php" method="post" id="commentform">
   
       <?php if ( is_user_logged_in() ) : ?>
   
       <p><?php printf(__('Logged in as %s.'), '<a href="'.get_option('siteurl').'/wp-admin/profile.php">'.$user_identity.'</a>'); ?> <a href="<?php echo wp_logout_url(get_permalink()); ?>" title="<?php _e('Log out of this account') ?>"><?php _e('Log out &raquo;'); ?></a></p>
   
       <?php else : ?>
   
       <p><input type="text" name="author" id="author" value="<?php echo esc_attr($comment_author); ?>" size="22" tabindex="1" />
       <label for="author"><small><?php _e('Name'); ?> <?php if ($req) _e('(required)'); ?></small></label></p>
   
       <p><input type="text" name="email" id="email" value="<?php echo esc_attr($comment_author_email); ?>" size="22" tabindex="2" />
       <label for="email"><small><?php _e('Mail (will not be published)');?> <?php if ($req) _e('(required)'); ?></small></label></p>
   
       <p><input type="text" name="url" id="url" value="<?php echo esc_attr($comment_author_url); ?>" size="22" tabindex="3" />
       <label for="url"><small><?php _e('Website'); ?></small></label></p>
   
       <?php endif; ?>
   
       <p><textarea name="comment" id="comment" cols="50" rows="10" tabindex="4"></textarea></p>
   
       <p><input name="submit" type="submit" id="submit" tabindex="5" value="<?php esc_attr_e('Submit Comment'); ?>" />
       <input type="hidden" name="comment_post_ID" value="<?php echo $id; ?>" />
       </p>
       <?php do_action('comment_form', $post->ID); ?>
   
       </form>
   
       <?php endif; // If registration required and not logged in ?>
   
       <?php else : // Comments are closed ?>
       <p><?php _e(''); ?></p>
       <?php endif; ?>
   
       	</div>
   
       	<?php endwhile; else: ?>
   
       		<p>Sorry, no posts matched your criteria.</p>
   
       <?php endif; ?>
       </div>
   
       <?php get_sidebar(); ?>
       <?php get_footer(); ?>
       ```
   
 * I am pretty sure its a simple thing like i didnt close an if statement… Help?

Viewing 1 replies (of 1 total)

 *  Thread Starter [mrspabs](https://wordpress.org/support/users/mrspabs/)
 * (@mrspabs)
 * [16 years ago](https://wordpress.org/support/topic/comment-problem-5/#post-1505808)
 * Answering my own question. I guess you are not supposed to directly insert the
   comments code into single. Instead, I did the below.
    `<div id="comments"><?php
   comments_template(); ?></div>` And its works now.

Viewing 1 replies (of 1 total)

The topic ‘Comment Problem’ is closed to new replies.

## Tags

 * [Comments](https://wordpress.org/support/topic-tag/comments/)
 * [foreach](https://wordpress.org/support/topic-tag/foreach/)

 * 1 reply
 * 1 participant
 * Last reply from: [mrspabs](https://wordpress.org/support/users/mrspabs/)
 * Last activity: [16 years ago](https://wordpress.org/support/topic/comment-problem-5/#post-1505808)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
