The author.php
template in WordPress is used to display author archive pages, where their posts and additional information are shown. Here’s a basic structure for the author.php
file:
<?php
/**
* The template for displaying Author archive pages
*
* @package YourThemeName
*/
get_header(); ?>
<div class="author-page">
<header class="author-header">
<?php
// Get author data
$author = get_queried_object();
?>
<h1 class="author-title"><?php echo esc_html($author->display_name); ?></h1>
<?php if (get_the_author_meta('description', $author->ID)) : ?>
<p class="author-bio"><?php echo esc_html(get_the_author_meta('description', $author->ID)); ?></p>
<?php endif; ?>
</header>
<div class="author-posts">
<?php if (have_posts()) : ?>
<h2><?php esc_html_e('Posts by ', 'your-text-domain'); ?><?php echo esc_html($author->display_name); ?>:</h2>
<ul class="author-post-list">
<?php while (have_posts()) : the_post(); ?>
<li>
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
<span class="post-date"><?php echo get_the_date(); ?></span>
</li>
<?php endwhile; ?>
</ul>
<?php else : ?>
<p><?php esc_html_e('This author has no posts yet.', 'your-text-domain'); ?></p>
<?php endif; ?>
</div>
</div>
<?php get_footer(); ?>
Key Points:
- Author Header:
- The author’s name is displayed using
get_queried_object()
. - The author’s description is fetched using
get_the_author_meta('description')
.
- The author’s name is displayed using
- Author Posts:
- The standard WordPress Loop is used to display the author’s posts.
- If the author has no posts, a fallback message is shown.
- Translation:
- Use WordPress translation functions like
esc_html_e()
andesc_html__()
.
- Use WordPress translation functions like
- Security:
- Data is output with proper escaping functions such as
esc_html()
.
- Data is output with proper escaping functions such as
Additional Notes:
- Add relevant CSS classes for styling the template.
- If you need extended functionality, such as adding custom fields, you can use
get_user_meta()