简体   繁体   中英

How can I display translated author role name in WordPress

Using this function in functions.php works but it displays the slug name for example: administrator

function get_author_role() {
    $userauthorid = get_the_author_meta( 'id' );
    $user_info = get_userdata($userauthorid );
    echo $user_info->roles['name']; 
}

I want it to show the translated role name.

UPDATE translate_user_role() : Unfortunately, as it seems this function wasn't meant to be executed in Wordpress Frontend. I thought that issue was over!

There are two solutions to this problem :

  • Solution 1: Load the Administration' Textdomain in frontend :
add_action( 'init', 'load_admin_textdomain_in_front' )    function load_admin_textdomain_in_front() {
           if ( ! is_admin() ) {
               load_textdomain( 'default', WP_LANG_DIR . '/admin-' . get_locale() . '.mo' );
           }    }
  • Solution 2:

User roles are just few words, so I just made this function to pick the translation of Role' name from an array of translations.

function get_author_info() {
      $post_data = get_post_data(); 
      $author_data = get_userdata($post_data->post_author);  
      $author_roles = $author_data->roles;
        $role = $author_data->roles[0];
        $rolesfr = array('editor'=>'Éditeur','administrator'=>'Administrateur','author'=>'Auteur','contributor'=>'Contributeur','subscriber'=>'Abonné');     
        echo $rolesfr[$role] ;
}

On a multilingual website, you could add an argument to this function to switch for the active language. Or use the first solution.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM