The correct way to create author profile in WordPress and display author profile section after the post, with options like bio, avatar, and social links.
How to Create The Author Profile in WordPress
Using functions:
- get_the_author_meta() Function to get author options and author ID.
- get_avatar_url() Function to get author avatar.
And using user_contactmethods filter we will add a new options for author and display the options in the author profile.
Add a New Options for Author
This is and example to add a new options for author, for example add social links:
// Add a new options to the user profile settings http://example.com/wp-admin/profile.php function add_user_social_links($fields){ $fields['twitter_of_author'] = 'Author Twitter'; $fields['facebook_of_author'] = 'Author Facebook'; return $fields; } add_filter('user_contactmethods', 'add_user_social_links', 10, 1);
The Author Profile
This is an example of the author profile:
function the_author_profile(){ /* Written by Qassim Hassan | wp-time.com */ $author_id = get_the_author_meta('ID'); // author ID. $avatar_url = get_avatar_url( get_the_author_meta( 'user_email', $author_id), array('size' => 192) ); // get author avatar link, this number "192" is size of avatar (width and height), you can change the number. $display_name = get_the_author_meta('display_name', $author_id); // get author display name $bio = get_the_author_meta( 'description' , $author_id); // get author bio (Biographical Info field). $facebook_link = get_the_author_meta( 'facebook_of_author' , $author_id); // get author facebook link $twitter_link = get_the_author_meta( 'twitter_of_author' , $author_id); // get author twitter link //$website_link = get_the_author_meta( 'url' , $author_id); // get author website link ?> <div class="author-profile"> <img src="<?php echo $avatar_url; ?>"> <h3>Post by <?php echo $display_name; ?></h3> <?php if ( $bio ) : // check if the author has bio ?> <p><?php echo $bio; // get author bio ?></p> <?php endif; ?> <?php if ( $facebook_link or $twitter_link ) : // check if the author has facebook link or twitter link ?> <ul> <?php if ( $facebook_link ) : // check if the author has facebook link ?> <li><a href="<?php echo $facebook_link; ?>">My facebook page</a></li> <?php endif; ?> <?php if ( $twitter_link ) : // check if the author has twitter link ?> <li><a href="<?php echo $twitter_link; ?>">My twitter profile</a></li> <?php endif; ?> </ul> <?php endif; ?> </div> <?php }
Display The Author Profile
To display the author profile add “the_author_profile()” function inside the loop, for example “twentyfifteen” loop in “content.php” file, open it and enter this code anywhere:
<?php if( is_single() ){ the_author_profile(); } ?>
Like this:
The result is:
Read about is_single().
To display the author profile in author archives page create “author.php” file and enter this code outside the loop:
<?php the_author_profile(); ?>
If you don’t have “author.php” file you can display the author profile anywhere but use is_author() function, for example in twentyfifteen “archive.php” file, open it and enter this code anywhere outside the loop:
<?php if( is_author() ){ the_author_profile(); } ?>
Enjoy.