• In the comments.php page for my site, there’s a bit of php in there to create a different class for every other comment, which is awesome, but I’ve run into a problem. For some reason, the first comment on each post is not being assigned a class at all. This is the comments.php code (from the whiteboard framework)

    <?php
    
    // Do not delete these lines or the Matrix will implode. or explode. I can't remember. But whichever it is, it isn't good.
     if (!empty($_SERVER['SCRIPT_FILENAME']) && 'comments.php' == basename($_SERVER['SCRIPT_FILENAME']))
      die ('Please do not load this page directly or we will hunt you down. Thanks and have a great day!');
    
     if ( post_password_required() ) { ?>
      <p class="nocomments">This post is top secret and password protected. Enter the password to view comments. (do it within 10 seconds or your computer may explode.)</p>
     <?php
      return;
     }
    ?>
    
    <div id="comments">
    <?php if ( have_comments() ) : ?>
     <h4>Discussion</h4>
    
     <ol id="comments_list">
      <?php foreach ($comments as $comment) : ?>
      <?php $comment_type = get_comment_type(); ?><?php if($comment_type == 'comment') { ?>
       <li class="<?php if (the_author('', false) == get_comment_author()) echo 'author'; else echo $oddcomment; ?>" id="comment-<?php comment_ID() ?>">
        <div class="comments_meta">
    
         <?php echo get_avatar( $comment, 50 ); ?>
         <p class="date"><?php comment_date('M j, y') ?><br /><?php comment_time()?></p>
    
        </div>
    
       <?php if ($comment->comment_approved == '0') : ?>
        <em>Marginalia moderates comments to protect against spam. Your comment should appear shortly, so long as you're not a spammer.</em>
       <?php endif; ?>
        <div class="comment_text">
        <div class="name"><?php comment_author_link()?> says:</div>
         <?php comment_text(); ?>
        </div>
    
        <div class="clearfix"></div>
       </li>
       <?php /* Changes every other comment to a different class */
        if ('alt' == $oddcomment){
         $oddcomment = 'standard';
        }
         else {
         $oddcomment = 'alt';
        }
       ?>
      <?php } else { $trackback = true; } /* End of is_comment statement */ ?>
      <?php endforeach; /* end for each comment */ ?>
     </ol>
    
    <?php if ($trackback == true) { ?>
    <h3>Trackbacks</h3>
    <ol>
    <?php foreach ($comments as $comment) : ?>
    <?php $comment_type = get_comment_type(); ?>
    <?php if($comment_type != 'comment') { ?>
    <li><?php comment_author_link() ?></li>
    <?php } ?>
    <?php endforeach; ?>
    </ol>
    <?php } ?>
    
     <?php else : ?>
    
    	<?php if ( !comments_open() ) : //if comments are closed ?>
    		<p class="nocomments">Sorry, we've shut down discussion of this post, because we're internet facists.</p>
    
    	<?php endif; ?>
    <?php endif; ?>
    </div><!-- end #comments -->
    
    <?php if ( comments_open() ) : ?>
    
    <div class="comments_reply">
    
     <h4><?php comment_form_title( 'Leave a Reply', 'Leave a Reply to %s' ); ?></h4>
    
     <?php if ( get_option('comment_registration') && !is_user_logged_in() ) : //if visitors must be logged in to comment ?>
      <p>Sorry, but you must be <a href="<?php echo wp_login_url( get_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 if ( is_user_logged_in() ) : //if user is logged in, displays username and option to log out ?>
       <p>Logged in as <a href="<?php echo get_option('siteurl'); ?>/wp-admin/profile.php"><?php echo $user_identity; ?></a>. <a href="<?php echo wp_logout_url(get_permalink()); ?>" title="Log out of this account">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" <?php if ($req) echo "aria-required='true'"; ?> />
      <label for="author"><small>Name <?php if ($req) echo "(required)"; ?></small></label></p>
    
      <p><input type="text" name="email" id="email" value="<?php echo esc_attr($comment_author_email); ?>" size="22" tabindex="2" <?php if ($req) echo "aria-required='true'"; ?> />
      <label for="email"><small>Mail <small>(will not be published or shared)</small> <?php if ($req) echo "(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>Website</small></label></p>
      <?php endif; ?>
    
      <p><small>You can use these tags: <code><?php echo allowed_tags(); ?></code></small></p>
      <p><textarea name="comment" id="comment" cols="100%" rows="10" tabindex="4"></textarea></p>
    
      <p><input name="submit" type="submit" id="submit" tabindex="5" value="Submit Comment" />
      <?php comment_id_fields(); ?>
      </p>
      <?php do_action('comment_form', $post->ID); ?>
     </form>
    
     <p>By submitting a comment here you grant <?php bloginfo('name'); ?> a perpetual license to reproduce your words and name/web site in attribution. Inappropriate or irrelevant comments will be removed at an admin's discretion.</p>
    
     <?php endif; // If registration required and not logged in ?>
    </div><!-- end .comments_reply -->
    
    <?php endif; // if you delete this the Matrix will break and will hunt you down for revenge ?>

    and this is part of the html it’s returning:

    <div id="comments">
     <h4>Discussion</h4>
    
     <ol id="comments_list">
           <li class="" id="comment-8">
        <div class="comments_meta">
    
         <img alt='' src='http://1.gravatar.com/avatar/707ede041bd19792836a11e1daa3523c?s=50&d=http%3A%2F%2F1.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D50&r=G' class='avatar avatar-50 photo' height='50' width='50' />     <p class="date">Jul 1, 10<br />1:19 am</p>
    
        </div>
    
           <div class="comment_text">
        <div class="name"><a href='http://www.heaven.org' rel='external nofollow' class='url'>God</a> says:</div>
         <p>“I’m just saying you only get one life. There’s no God, no rules, no judgements, except for those you accept or create for yourself. And once it’s over, it’s over. Dreamless sleep forever and ever. So why not be happy while you’re here. Really. Why not?”</p>
        </div>
    
        <div class="clearfix"></div>
    
       </li>
                <li class="alt" id="comment-10">
        <div class="comments_meta">

    The relevant bit is here:

    <li class="" id="comment-8">

    It should be:

    <li class="standard" id="comment-8">

    and I can’t figure out for the life of my why it’s not working.

Viewing 2 replies - 1 through 2 (of 2 total)
  • the string $oddcomment, which is used to toggle the styles, is not defined before the first comment;

    to do this, simply add a line like this:
    $oddcomment = 'standard';
    or
    $oddcomment = 'alt';
    (whatever style you prefer to start with)
    quite at the beginning of the code of comments.php:

    for instance here:

    <?php if ( have_comments() ) : ?>
    <?php $oddcomment = 'standard'; ?>
     <h4>Discussion</h4>

    Thread Starter oloranya

    (@oloranya)

    Thanks so much, that fixed it! 😀

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Comment Template – first comment on post has no class assigned?’ is closed to new replies.