• Resolved luisthegeek

    (@luisthegeek)


    I am trying to display the author role in the author box next to their display name. I have the following in my functions.php file:

    function get_author_role()
    {
        global $authordata;
    
        $author_roles = $authordata->roles;
        $author_role = array_shift($author_roles);
    
        return $author_role;
    }

    And the following in my single.php file in the author box area:

    <h4 class="author-name"><?php the_author_meta( 'display_name' ); ?><?php echo " , " .get_author_role(); ?>

    It works great except it displays the role name in lowercase, for example, Administrator displays as administrator.

    I have set up custom roles such as Publisher, Correspondent, Staff Writer, etc. I want to be able to display the role’s display name rather than the role name.

    I tried adding the following to my functions.php file:

    
    if ($author_role == 'publisher' {
    echo 'Publisher';
    } elseif ($author_role == 'editor') {
    echo 'Editor';
    } elseif ($author_role == 'staff-writer') {
    echo 'Staff Writer';
    } elseif ($author_role == 'contributor') {
    echo 'Contributor';
    } elseif ($author_role == 'correspondent') {
    echo 'Correspondent';
    }

    But it still shows the author role name, not the display name. Am I doing something wrong or should I be calling another function? Not sure if there is an author_display_role or anything.

    • This topic was modified 7 years, 1 month ago by luisthegeek.
    • This topic was modified 7 years, 1 month ago by luisthegeek.

    The page I need help with: [log in to see the link]

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Author Vladimir Garagulya

    (@shinephp)

    Use WP_Roles WordPress class (wp-includes/class-wp-roles.php) as reference. You can always request WP user roles using wp_roles() function:

    
    $wp_roles = wp_roles();
    

    Then show role name instead of its ID this way:

    
    echo $wp_roles->role_names[$author_role];
    
    Thread Starter luisthegeek

    (@luisthegeek)

    Im a bit confused. Sorry, long day. So am I keeping the functions I already have? Where do I the codes you gave?

    Plugin Author Vladimir Garagulya

    (@shinephp)

    Assuming that $authordata contains valid author user roles array

    
    function get_author_role_name()
    {
        global $authordata;
    
        $author_roles = $authordata->roles;
        $role = array_shift($author_roles);
        $wp_roles = wp_roles();
        // take role display name by role ID
        $role_name = $wp_roles->role_names[$role];
    
        return $role_name;
    }
    

    Use this function then where you wish to output author role display name.

    Thread Starter luisthegeek

    (@luisthegeek)

    This works beautifully! Thanks!

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Display Author Role in author box’ is closed to new replies.