To create an archive page in WordPress that lists all authors and provides author profile pages with the URL structure domain/authors/author
, follow these steps:
1. Create a custom page template for author archive
Create a template file: In your active theme, create a new file called page-authors.php
. This will be a custom page that displays a list of all authors.
Add code to display authors: In the page-authors.php
file, add the following code:
<?php
/* Template Name: Authors Archive */
get_header();
?>
<main>
<h1>Our Authors</h1>
<ul>
<?php
$authors = get_users([
'who' => 'authors',
'has_published_posts' => true, // Only authors with posts
]);
foreach ($authors as $author) {
echo '<li>';
echo '<a href="' . esc_url(get_author_posts_url($author->ID)) . '">' . esc_html($author->display_name) . '</a>';
echo '</li>';
}
?>
</ul>
</main>
<?php get_footer(); ?>
Create a page in the WordPress admin: In the WordPress admin panel, create a new page called “Authors” (or “Authors” in Russian) and select the Authors Archive
template you just created for it.
2. Set up the author URL structure
Update permalink settings: In WordPress, go to Settings > Permalinks, and make sure one of the human-readable URL options is selected (for example, /%postname%/
).
Use a filter to modify the author URL: By default, author pages have URLs like /author/username
. To change it to /authors/username
, add the following code to your theme’s functions.php
file:
add_filter('author_link', function ($link, $author_id, $author_nicename) {
return home_url('/authors/' . $author_nicename . '/');
}, 10, 3);
Add a rewrite rule: To ensure WordPress properly handles the new author URLs, you need to add a rewrite rule:
add_action('init', function () {
add_rewrite_rule('^authors/([^/]+)/?', 'index.php?author_name=$matches[1]', 'top');
});
Update permalinks: After adding this code, go to Settings > Permalinks and click “Save Changes” to refresh the rewrite rules.
3. Customize the author profile page
If you want to customize the author archive page (the archive of their posts), edit the author.php
file in your theme, or create it if it doesn’t exist.
Here’s a basic example for author.php
:
<?php
get_header();
?>
<main>
<h1>Posts by: <?php echo get_the_author(); ?></h1>
<p><?php echo get_the_author_meta('description'); ?></p>
<?php if (have_posts()) : ?>
<ul>
<?php while (have_posts()) : the_post(); ?>
<li>
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
</li>
<?php endwhile; ?>
</ul>
<?php else : ?>
<p>This author has no published posts yet.</p>
<?php endif; ?>
</main>
<?php
get_footer();
?>
4. Testing and verification
- Ensure that the
/authors/
page displays all authors. - Verify that
/authors/username/
URLs work correctly and lead to the respective author’s post archive.