Alternating colors troubleshooting
-
Hi,
I tried to find some help on the same issue over the net, 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 and have still another color for my personal comments.
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!
Thanks
[Code Moderated JC]
-
Hi,
I tried to find some help on the same issue over the net, 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 and have still another color for my personal comments.
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!
Thanks
<?$i;?> <?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; } } ?> <!-- You can start editing here. --> <!-- --> <!-- --> <!-- --> <?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" name="comment12"><?php comments_number('Aucun commentaire', 'Un commentaire', '% commentaires' );?> à la suite de ? <?php the_title (); ?> ?</h2> <?php foreach ($comments as $comment) : ?> <?php if (get_comment_type()=="comment") : ?> <?php $oddcomment = 'class="odd"'?> <li class="<?php if ( $comment->comment_author_email == get_the_author_email() ) echo 'mycomment'; else echo $oddcomment; ?>" id="comment-<?php comment_ID() ?>"> <li class="<?php if ($comment->user_id == 1) $oddcomment = "authorstyle"; echo $oddcomment; ?>"> <div class="gravatarimg_ctnr"><img src="<?php gravatar("R", 50, "https://www.lelectronlibre.net/carnet3/uploads/default.jpg","FF0000"); ?>" alt="Gravatar" heigth="50" width="50" /></div> <p style="margin-bottom:10px;"><?php comment_author_link() ?> le " title=""><?php comment_date('j M Y') ?> | <?php if( function_exists( 'atrwcp_reply' ) ) atrwcp_reply(); ?><?php edit_comment_link('Edit',' | ',''); ?></p> <?php if ($comment->comment_approved == '0') : ?> Your comment is awaiting moderation. <?php endif; ?> <?php comment_text() ?> <?php endif; endforeach; ?> <?php endif; ?> <?php /* This is a loop for printing trackbacks if there are any */ if ($numPingBacks != 0) : ?> # <h2><div align="center"><?php _e($numPingBacks); ?> Rétrolien(s)</div></h2> <!--<?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 /wp-login.php?redirect_to= <?php the_permalink(); ?>">logged in to post a comment.</p> <?php else : ?> <form action="<?php echo get_option('siteurl'); ?>/wp-comments-post.php" method="post" id="commentform"> <?php live_preview() ?> <div class="box1"> <h3 id="respond">Commenter ce texte</h3> <div class="bordure-top"></div> <?php if ($user_ID) : ?> <p>Vous êtes connecté en tant que /wp-admin/profile.php"> <?php echo $user_identity; ?>. /wp-login.php?action=logout" title="Se déconnecter">Se déconnecter. </p> <?php else : ?> <p><label for="author">Nom<?php if ($req) _e(' (*)'); ?></label> <input type="text" name="author" id="author" value="<?php echo $comment_author; ?>" size="22" tabindex="1" /></p> <p><label for="email">Courriel<?php if ($req) _e(' (*)'); ?></label> <input type="text" name="email" id="email" value="<?php echo $comment_author_email; ?>" tabindex="2" size="22" /></p> <p><label for="url">Site internet</label> <input type="text" name="url" id="url" value="<?php echo $comment_author_url; ?>" size="22" tabindex="3" /></p> <?php endif; ?> Commentaire: <p><textarea name="comment" id="comment" cols="4" rows="7" tabindex="4"></textarea></p> <p><input name="submit" type="submit" id="submit" class="input1" value="Envoyer" /> <input type="hidden" name="comment_post_ID" value="<?php echo $id; ?>" /> </p> </form> <div class="box2"><?php do_action('comment_form', $post->ID); ?></div> </div> <?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; ?>
For some reason not only does the alterning style does not work but the author style changes the background just over the author comment instead of modifying the author comment.
Hmmm… Now I tried this:
<?php $i = 0; ?> <?php foreach ($comments as $comment) : ?> <?php $i++; ?> <?php if (get_comment_type()=="comment") : ?> <?php if($i&1) { echo 'class="odd"'; } else { echo 'class="even"'; } ?> <li class="<?php if ( $comment->comment_author_email == get_the_author_email() ) echo 'mycomment'; else echo $oddcomment; ?>" id="comment-<?php comment_ID() ?>"> <li class="<?php if ($comment->user_id == 1) $oddcomment = "authorstyle"; echo $oddcomment; ?>">
And I do see an alternating pattern, but I see class=”even” and class=odd written on the web page instead of the .even or .odd class applied.
So weird.
I managed to get the original code and I did set the .mycomment class and was able to get comment style for my personal comments. However, I still can’t alternate colors even if the code says it should be possible:
<?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>
At the beginning of this code (sorry; I know it is long) it is written that odd is for alternating comments color. So I did set a class for .odd but the only thing it did was creating a color for each and every comments that is not mine.
I see in the code further down that there is something about ‘even’, so I tried to set a .even but it didn’t work.
Do you see something in the code that would show how to set the alternating comments?
Anyone can help me? I can’t believe I’m the only person with this problem.
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.
Hmmm… anyway I will try to use the new 2.7 improvements. So far I can have the alternating comments. I just need to be able to make .bypostauthor work and I’ll be all right.
@jeremyclark13: Is it me… or those two codes are the same?
I think I’ll stick to 2.6 for now. 2.7 is not ready. Too difficult to customize.
What is the difference between the two codes above?
I’m still having trouble with that.
Thank you Jeremy. I was looking to do the same thing and I was able to use your suggestion here and made mine work.
A very simple even-odd maker :
$e_o = "even"; // Set it to even on the first time foreach($tests as $test){ <div class="<?php echo $e_o; ?>"> // do your loop stuff </div> // if $e_o is now "even" it becomes "odd", else it becomes "even" $e_o = ($e_o == "even") ? $e_o = "odd" : $e_o = "even"; }
—
gd
- The topic ‘Alternating colors troubleshooting’ is closed to new replies.