webappguides
Create Wordpress Theme From Scratch! Build Your Own

Get author nickname in wordpress

webapps log

webapps Lab
May 31, 2021
ThemeCreation

WordPress allows displaying the author’s nickname instead of the real name.

The actual and non-deprecated WordPress function for display is get_the_author_meta.

To output the nickname, the field value of nickname passes to get_the_author_meta

This function is able to retrieve various data relating to the author.

Check other usages here: How to get Author email

Here are some ways to get an author’s nickname in WordPress.

Method 1: using nickname field.

$theAuthorNickName = get_the_author_meta('nickname');

The shortcode displays the current username.

This means you will implement it inside the loop that iterates the posts.

For example.

<?php if(have_posts():?>

<?php while(have_posts(): the_post(); ?>

$theAuthorNickName = get_the_author_meta('nickname');

<h2><?php echo $theAuthorNickName ;?></h2>

<?php endwhile;?>

<?php endif?>

The name is displayed using heading level 2

<h2><?php echo $theAuthorNickName ;?></h2>

The function returns a string value that needs echoing to print the name on the page.

If you have experience with WordPress you already know the difference function starts with  “get_” and these start with “the”

Method 2: Using the user_nicename field.

The user_nicename argument to get_the_author_meta sets out the author’s email as a nickname.

It doesn’t take the entire email but it omits some character specifically the “@” character.

For example, assume the author’s email is author@webapplabs.com the nickname will be authorwebapplabs-com.

Here is how to implement user_nicename to set the author nickname.

$theAuthorNickName2 = get_the_author_meta('user_nicename');

To show the name, the echo keyword is to be used.

<?php echo $theAuthorNickName2; ?>

user_nicename is similar to user_login but they are different.

Learn more here user_nicename vs user_login

Don’t forget to the nickname snippet inside a loop.

Method 3: Using the display_name field value

The display name field shows the user login name.

For example, a user login name is myNickname.

display_name outputs myNickname when the is passed to get_the_author_meta.

<?php

$theUserName = get_the_author_meta('display_name');

echo $theUserName;

?>

Display First Name and Last Name

It is possible to set the nicknames to both two sets.

In fact, a get_user_meta can write the author’s first name and the last name.

The function needs the author’s ID before taking the author’s names.

$theUserAuthorId = get_the_author_meta('ID');

To get the author’s first name get_user_meta uses the first_name key.

For the last name, it uses the last_name key.

$authorFirstName = get_user_meta ($theUserAuthorId,'first_name');

$authorLastName = get_user_meta ($theUserAuthorId, 'last_name');

Yous should echo these values

echo $authorFirstName;

echo $authorLastName;

How to link author nickname

Normally author real names display with a link but not the nickname.

There is a convenient way to create an author’s nickname URL in a single post or on any page.

You will link the nickname by implementing get_the_author_posts_url.

To know the right author URL get_the_author_meta has to provide the author ID.

This a full implementation.

<a href = <?php echo get_the_author_posts_url ( get_the_author_meta ('ID')); ?> >

<?php echo get_the_author_meta ('nickname'); ?>

</a>

Wrap-Up

Generally there straightway forward to retrieve the author’s nickname.

get_the_author_meta can be filled with constant variable ‘nickname’

For the author who has at least two names, get_user_meta can offer these names easily after receiving first_name and last_name keywords.

It is recommended to the user a non-deprecated function to retrieve the nickname.

get_author_name is a WordPress deprecated function that can display the author name.

Some interesting WordPress Features.

add bootstrap carousel in WordPress

Display popular posts widget.