Jump to content

[SOLVED] Need help for a wordpress PHP (hope this is the right forum)


Recommended Posts

Hi,

 

I am new here.  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>
    </li>


<?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, "http://www.lelectronlibre.net/carnet3/uploads/default.jpg","FF0000"); ?>" alt="Gravatar" heigth="50" width="50" /></div>


<p style="margin-bottom:10px;"><strong><?php comment_author_link() ?></strong> le <a href="#comment-<?php comment_ID() ?>" title=""><?php comment_date('j M Y') ?></a> | <?php if( function_exists( 'atrwcp_reply' ) ) atrwcp_reply(); ?><?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></li>











<?php endif; endforeach; ?>

</ol>

<?php endif; ?>

<?php

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

<li><h2><div align="center"><?php _e($numPingBacks); ?> Rétrolien(s)</div></h2></li>

<!--<?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">


<?php live_preview() ?>


<br/>
<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 <a href="<?php echo get_option('siteurl'); ?>/wp-admin/profile.php">
	<?php echo $user_identity; ?></a>. <br/><a href="<?php echo get_option('siteurl'); ?>/wp-login.php?action=logout" title="Se déconnecter">Se déconnecter</a>.
</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>

<br/>





<?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; ?>

I used this code 

  
<?php $i = 0; ?>
<?php foreach ($comments as $comment) : ?>
     <?php $i++; ?>
 <?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() ?>">

<li class="<?php if ($comment->user_id == 1) $oddcomment = "authorstyle"; echo $oddcomment; ?>"> 

    






<?php if($i&1) {
        echo 'class="odd"';
    } else {
        echo 'class="even"';
    } ?> 

 

but the only thing I managed to achieve was writting "class=odd"  or "class=even" on every comment on the page.  Please help me.

This is just some pseudo-code, but it should work:

 

foreach ($comments as $n => $comment) {
if ($post['user_id'] = $comment['user_id']) {
	$class = 'personal';
}
else {
	$class = $n % 2 == 0 ? 'even' : 'odd';
}

echo '<div class="' . $class . '">';
// etc.
}

 

Here $post is an array of the information about the post the comments belong to. $comments is an array containing all the comments of course.

Hi Daniel0,

 

Thank you very much for your help.  I will try this code later today.  Just curious: what do you mean by "pseudo-code"  and what should I put in place of "etc." in your code?

 

(I am a PHP newbie... willing to learn)

Hmmm... I got an error: Fatal error: Cannot use object of type stdClass as array in .... on line 56...

 

Line 56 is

if ($post['user_id'] = $comment['user_id']) {

 

I'm kind of a newbie with all this.  I will try to see what I can do; do I need to overlap the

<div class="' . $class . '">

with the rest of the code below so that each and every aspect of the comment will have the appropriate class?  And where must I put something like

<?php endif; endforeach; ?>

?

 

I'll be out for the day, but I'll work on this tomorrow.  Again thank you very much for your help. 

Thank you very much for your help.  I will try this code later today.  Just curious: what do you mean by "pseudo-code"  and what should I put in place of "etc." in your code?

 

Pseudo-code is code that's not indented to be actually used, but merely to show the concept of how things are done. This means you'll have to adapt it to your specific problem. I haven't touched the Wordpress code before so I don't know how it works.

 

Perhaps it would've been easier if I wrote my pseudo-code in English instead of PHP:

Iterate over each comment. For each comment you check if the user ID assigned to it is the same as the user ID assigned to the blog post*. If that's the case then you set the class to "personal". If that's not the case then you set it to "even" if the iteration number (starting from 0) is divisible by 2, otherwise you set it to "odd".

 

* Essentially anything information that both things have would do the trick.

Hi,

 

Thank you very much for your help.  Unfortunately, I wasn't able to use this code, because I am not good enough with PHP (really a newbie).

 

However, 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 &#8220;<?php the_title(); ?>&#8221;</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?

 

Thanks a lot!

I think I'll migrate to WP2.7 soon and will try to toy with all the new code that is there.  That might be simplier.  So far, I think I found my mistake: I simply didn't pu the .odd .even and the other one in the correct order in CSS.  Doh!  Yes, that's how I feel...

 

Thanks for helping!

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.