Collective Web Design

Support : How to change every other comment to a different class wordpress

You must be logged in to post Login Register

Search 
Search Forums:


 




How to change every other comment to a different class wordpress

UserPost

8:01 am
September 25, 2009


Mike Smith

New Member

posts 2

1

Post edited 8:50 pm – September 25, 2009 by admin


I tried to find some help on changeing every other comment in wordpress to a diffrent class, but wasn't able to resolve the problem.

What I want to do is pretty simple. In my wordpress comments.php file I want to have a different color for odd or even messages.

I tried nearly everything I could try on the net, toying with .odd and .even classes, but I think there is something really wrong with my code. If anyone could tell me what is wrong and how to make it work, I would greatly appreciate!

<?php // Do not delete these lines
   if ('comments.php' == basename($_SERVER['SCRIPT_FILENAME']))
      die ('Please do not load this page directly. Thanks!');

        if (!empty($post->post_password)) { // if there's a password
            if ($_COOKIE['wp-postpass_' . COOKIEHASH] != $post->post_password) {  // and it doesn't match the cookie
            ?>

            <p class=”nocomments”>This post is password protected. Enter the password to view comments.<p>

            <?php
            return;
            }
        }

      /* This variable is for alternating comment background */
      $oddcomment = 'odd';
?>

<!– You can start editing here. –>

<div class=”boxcomments”>

<?php if ($comments) : ?>

<?php

   /* Count the totals */
   $numPingBacks = 0;
   $numComments  = 0;

   /* Loop through comments to count these totals */
   foreach ($comments as $comment) {
      if (get_comment_type() != “comment”) { $numPingBacks++; }
      else { $numComments++; }
   }

?>

<?php

   /* This is a loop for printing comments */
   if ($numComments != 0) : ?>

   <ol class=”commentlist”>

   <li class=”commenthead”><h2 id=”comments”><?php comments_number('No Responses', 'One Response', '% Responses' );?> to “<?php the_title(); ?>”</h2></li>

   <?php foreach ($comments as $comment) : ?>
   <?php if (get_comment_type()==”comment”) : ?>

<li class=”<?php if ( $comment->comment_author_email == get_the_author_email() ) echo 'mycomment'; else echo $oddcomment; ?>” id=”comment-<?php comment_ID() ?>”>

      <p style=”margin-bottom:5px;”>By <strong><?php comment_author_link() ?></strong> on <a href=”#comment-<?php comment_ID() ?>” title=”"><?php comment_date('M j, Y') ?></a> | <a href=”#respond”>Reply</a><?php edit_comment_link('Edit',' | ',''); ?></p>
      <?php if ($comment->comment_approved == '0') : ?>
      <em>Your comment is awaiting moderation.</em>
      <?php endif; ?>
      <?php comment_text() ?>
   </li>

   <?php /* Changes every other comment to a different class */
   if ('alt' == $oddcomment) $oddcomment = '';
   else $oddcomment = 'odd';
   ?>

   <?php endif; endforeach; ?>

   </ol>

   <?php endif; ?>

<?php

   /* This is a loop for printing trackbacks if there are any */
   if ($numPingBacks != 0) : ?>

   <ol class=”tblist”>

<li><h2><?php _e($numPingBacks); ?> Trackback(s)</h2></li>

<?php foreach ($comments as $comment) : ?>
<?php if (get_comment_type()!=”comment”) : ?>

   <li id=”comment-<?php comment_ID() ?>”>
      <?php comment_date('M j, Y') ?>: <?php comment_author_link() ?>
      <?php if ($comment->comment_approved == '0') : ?>
      <em>Your comment is awaiting moderation.</em>
      <?php endif; ?>
   </li>

   <?php if('odd'==$thiscomment) { $thiscomment = 'even'; } else { $thiscomment = 'odd'; } ?>

<?php endif; endforeach; ?>

   </ol>

<?php endif; ?>

<?php else :

   /* No comments at all means a simple message instead */
?>

<?php endif; ?>

<?php if (comments_open()) : ?>

   <?php if (get_option('comment_registration') && !$user_ID ) : ?>
      <p id=”comments-blocked”>You must be <a href=”<?php echo get_option('siteurl'); ?>/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”>

   <h3 id=”respond”>Post a Comment</h3>

   <?php if ($user_ID) : ?>

   <p>You are logged in as <a href=”<?php echo get_option('siteurl'); ?>/wp-admin/profile.php”>
      <?php echo $user_identity; ?></a>. To logout, <a href=”<?php echo get_option('siteurl'); ?>/wp-login.php?action=logout” title=”Log out of this account”>click here</a>.
   </p>

<?php else : ?>   

      <p><label for=”author”>Name<?php if ($req) _e(' (required)'); ?></label>
      <input type=”text” name=”author” id=”author” value=”<?php echo $comment_author; ?>” size=”22″ tabindex=”1″ /></p>

      <p><label for=”email”>E-mail (will not be published)<?php if ($req) _e(' (required)'); ?></label>
      <input type=”text” name=”email” id=”email” value=”<?php echo $comment_author_email; ?>” tabindex=”2″ size=”22″ /></p>      

      <p><label for=”url”>Website</label>
      <input type=”text” name=”url” id=”url” value=”<?php echo $comment_author_url; ?>” size=”22″ tabindex=”3″ /></p>
   <?php
/****** Math Comment Spam Protection Plugin ******/
if ( function_exists('math_comment_spam_protection') ) {
   $mcsp_info = math_comment_spam_protection();
?>    <p><input type=”text” name=”mcspvalue” id=”mcspvalue” value=”" size=”22″ tabindex=”4″ />
   <label for=”mcspvalue”><small>Spam protection: Sum of <?php echo $mcsp_info['operand1'] . ' + ' . $mcsp_info['operand2'] . ' ?' ?></small></label>
   <input type=”hidden” name=”mcspinfo” value=”<?php echo $mcsp_info['result']; ?>” />
</p>
<?php } // if function_exists… ?>
   <?php endif; ?>

      <p><textarea name=”comment” id=”comment” cols=”5″ rows=”10″ tabindex=”4″></textarea></p>

      <p><input name=”submit” type=”submit” id=”submit” tabindex=”5″ value=”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 id=”comments-closed”>Sorry, comments for this entry are closed at this time.</p>
<?php endif; ?></div>

Thanks,

8:54 pm
September 25, 2009


JohnTheGeek

Moderator

Texas

posts 2

2

Using the above code if you change this section

<?php /* Changes every other comment to a different class */
   if ('alt' == $oddcomment) $oddcomment = '';
   else $oddcomment = 'odd';
   ?>

to this then it should work

<?php /* Changes every other comment to a different class */
   if ('odd' == $oddcomment) $oddcomment = '';
   else $oddcomment = 'odd';
   ?>

Then you can use the class .odd for alternating color.


7:47 am
September 26, 2009


Mike Smith

New Member

posts 2

3

JohnTheGeek said:


Using the above code if you change this section

<?php /* Changes every other comment to a different class */
   if ('alt' == $oddcomment) $oddcomment = '';
   else $oddcomment = 'odd';
   ?>

to this then it should work

<?php /* Changes every other comment to a different class */
   if ('odd' == $oddcomment) $oddcomment = '';
   else $oddcomment = 'odd';
   ?>

Then you can use the class .odd for alternating color.



Thank you JohnTheGeek it worked.

The support I get with my template is A++

Search 

About the Professional Web Templates & Customization Service forum

Most Users Ever Online:

8


Currently Online:

5 Guests

Forum Stats:

Groups: 3

Forums: 10

Topics: 3

Posts: 7

Membership:

There are 79 Members

There has been 1 Guest

There is 1 Admin

There is 1 Moderator

Top Posters:

Mike Smith – 2

Susie – 1

Mike J – 1

cfgjoj – -1

fakeoakleysd8 – -1

Boalttaladala – -1

Administrators: admin (2 Posts)

Moderators: JohnTheGeek (2 Posts)