I am using this code to show a different styling class for the comment box if the comment is made by the Admin
<li class="<?php if ($comment->user_id == 1) $oddcomment = "authorstyle"; echo $oddcomment; ?>"></li>
The admin has the user_id = 1. Now, what if i want to add user_id 5 and 6, how would I edit this code?
You could either use the || operator (which means or):
Or you could use in_array:
Which looks nicer and is more maintainable. 🙂
One (possibly) robust way to handle this kind of situation is to ask if the userid is one of a set of userids, thus something fairly trivial as a condition is:
Clearly you’re developing some kind of permission system, so you’d really be better off coming up with a way to abstract user rights from their identities, and verifying a user has some privilege or status rather than in each place checking for said status.
One way to do this might be assigning a user to an admin group, and then checking against the group, rather than against all ids. Better still, you could consider setting some number of properties on the group, say,
has_style_xyz
, then check againsthas_style_xyz
in your conditional.You could use something like this:
… and so on, for multiple styles.
… for multiple IDs with one style.
Simply use
OR
or||
in your if statement.<li class="<?php if ($comment->user_id == 1 || $comment->user_id == 5 || $comment->user_id == 6) $oddcomment = "authorstyle"; echo $oddcomment; ?>"></li>
You can try this code
In a simple way you could do something like this:
I would advise against using the user’s ID to determine their admin status because then you will have to manually add/remove each user’s ID.
This website is exactly what you want to do.
http://buildinternet.com/2009/09/automatically-highlight-admin-comments-in-wordpress/