Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I want to create a badge image for comment author of a certain role, say, Editor. I want to style comments of users with the Editor role differently than comments of non-registered or lower-level users. For instance, I'd like to have an image appear next to his/her name.

I can do the whole thing in CSS, but I want a bit more flexibility so I can add more juice. How can I do it with a conditional tag? Any ideas?

share|improve this question
    
@ Milo Did you ever got it to work ? I really need that option as well I hope you will share the final result with me. Franco –  user13911 Mar 7 '12 at 12:34

2 Answers 2

Conditional tags are used to change content on the basis of what conditions a page meets. While there are no conditional tags depending on a user's role, this is how you can manipulate content depending on roles:

Display content to the user, depending on his role

<?php
   global $current_user;
   get_currentuserinfo(); //not strictly required, $current_user should be populated anyway 
   if(in_array('editor', $current_user->roles)) {
      // echo markup here
   }
?>

Alternatively, you might also want to check out current_user_can()

Edit: Display content based on a commentator's role

<?php
   $commentator_id = get_comment(get_comment_ID())->user_id;
   $commentator_info = get_userdata($commentator_id);
   $capabilities = $user_info->wp_capabilities;
   if (array_key_exists('editor', $capabilities)) {
      // echo markup here
   }
?>

The above will have to go in the comments loop.

Resources: get_comment() | get_user_data() | Comment Loop Beauty

share|improve this answer
    
Thanks for the snippet! The above code only examine whether the login user is editor and then show something to him. But what I want to do is to display certain badge for the comment author. Say if the comment author is an Editor, an Editor badge will show next to his comment, and everybody else can see the badge. It's kind like assign badge to certain users without using CSS. Any idea about how I can do it? –  Milo Nov 10 '11 at 15:51
    
@Milo I have updated the answer. See Edits. –  Johannes Pille Nov 10 '11 at 16:44
    
Thanks Johannes! I tried out the code but it didn't work. Nothing showed up. I tried several roles but still nothing showed up. I also tried the "Comment Loop Beauty" one, by using $comment->user_id == '1', it works well but only a certain user ID is allowed. –  Milo Nov 10 '11 at 17:49
    
@Milo where and how did you use the 2nd snippet? What file(s) of your theme are you editing? Can you show some of your relevant code? –  Johannes Pille Nov 10 '11 at 18:18
    
I use a custom function for the comment template. Inside comments.php: <?php $commentator_id = get_comment(get_comment_ID())->user_id; $commentator_info = get_userdata($commentator_id); $capabilities = $user_info->wp_capabilities; if (array_key_exists('editor', $capabilities)) { echo 'Editor' ; } ?> The complete code is here: gist.github.com/1355876 –  Milo Nov 10 '11 at 19:28

Ok; so I found a 4 year old function that's never been committed to core:

http://core.trac.wordpress.org/attachment/ticket/5290/author-template-the_author_role.diff

... so my guess is that by using this; we can do a simple if statement in the loop to show something if the post author is in a role/capability; but not sure how that relates to comment authors.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.