• Resolved machiniku

    (@machiniku)


    Hello,

    I’m trying to display the user role of the writer on a single blog post. Following this: http://wordpress.org/support/topic/display-user-role-on-profile-page?replies=6, I got it to work on the author’s page, but it doesn’t seem to grab the user role for a single post.

    Can somebody please suggest what I would need to modify/add to this code to make it work? I want to keep the first letter capital so ideally, would like to keep it in this format. Thanks!

    <?php
    $user_roles = $current_user->roles;
    $user_role = array_shift($user_roles);
    
    if ($user_role == 'administrator') {
    echo 'Administrator';
    } elseif ($user_role == 'editor') {
    echo 'Editor';
    } elseif ($user_role == 'author') {
    echo 'Author';
    } elseif ($user_role == 'contributor') {
    echo 'Contributor';
    } elseif ($user_role == 'subscriber') {
    echo 'Subscriber';
    } else {
    echo '<strong>' . $user_role . '</strong>';
    }
    ?>
Viewing 7 replies - 1 through 7 (of 7 total)
  • The $current_user is a global variable that may not be best suited for what you are trying to accomplish … it is used to represent the person currently viewing the site.

    Try using the post author’s information to establish the $user_role.

    Have a look through these pages:
    * http://codex.wordpress.org/Function_Reference/get_the_author_meta
    * http://codex.wordpress.org/Function_Reference/user_can
    * http://codex.wordpress.org/Roles_and_Capabilities

    Thread Starter machiniku

    (@machiniku)

    Thanks for the reply Edward, I understand what you mean. At the same time this part is beyond my knowledge. How would you write the code to establish the $user_role? I think I may be able to change the parameters around once I have the foundation.

    OK, it may be a little less involved than I first thought. Try this snippet in your single.php template:

    global $post;
    if ( user_can( $post->post_author, 'administrator' ) ) {
      echo 'Administrator';
    } elseif ( user_can( $post->post_author, 'editor' ) ) {
      echo 'Editor';
    } elseif ( user_can( $post->post_author, 'author' ) ) {
      echo 'Author';
    } elseif ( user_can( $post->post_author, 'contributor' ) ) {
      echo 'Contributor';
    } elseif ( user_can( $post->post_author, 'subscriber' ) ) {
      echo 'Subscriber';
    } else {
      echo '<strong>Guest</strong>';
    }

    NB: The call global $post; may not be needed, but I also did not test this very thoroughly either so make sure you have a proper backup.

    Edit: Here is a gist of the code: https://gist.github.com/Cais/6196562

    Thread Starter machiniku

    (@machiniku)

    I tried it with (and without) global $post; but nothing appears.

    The snippet above works fine for me on my test sites. I simply pasted it inside “the_Loop” and it did display the role as appropriate for the post author.

    Perhaps it is how you are implementing the code? Can you provide a link to a gist or pastebin of the code showing how you are using it?

    Thread Starter machiniku

    (@machiniku)

    Update: I realized I uploaded the wrong single.php file! Please ignore my last comment. Works like a charm! Thanks again for the help.

    No worries; glad to help. Besides, I was looking for a little snippet to write up a post for; this will work just fine (*grin*)

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘How to display user role in post’ is closed to new replies.