This is How to Create a Custom Author Page for Your WordPress Custom Post Type
As a WordPress developer, you may have created a custom post type to add unique content to your website. However, when it comes to displaying the author information for these custom post types, the default WordPress author page may not be sufficient.
In this article, we'll explore how you can create a custom author page specifically for your WordPress custom post type. This will allow you to showcase all the posts written by a particular author, even if they are from your custom post type.
Understanding the Default WordPress Author Page
By default, WordPress provides an author page that displays all the posts written by a specific user. This page can be accessed by navigating to the URL example.com/author/username
, where username
is the user's login name.
The default author page works well for the built-in WordPress post type, but it may not include the posts from your custom post type. This is because the author page is designed to only display the posts from the standard "post" post type.
To create a custom author page that includes your custom post type, you'll need to modify the default WordPress functionality. Let's dive into the step-by-step process.
Step 1: Create a Custom Author Template
The first step is to create a custom author template that will be used to display the author's information and the list of posts from your custom post type.
-
In your WordPress theme's directory, create a new file called author-{post-type}.php
, where {post-type}
is the slug of your custom post type. For example, if your custom post type is called "books," the file should be named author-books.php
.
-
Inside the author-books.php
file, add the following code:
<?php
/**
* The template for displaying Author Archive pages
*
* @package WordPress
* @subpackage Flowpoint
* @since 1.0
* @version 1.0
*/
get_header(); ?>
<div class="wrap">
<div id="primary" class="content-area">
<main id="main" class="site-main" role="main">
<?php
if ( have_posts() ) :
?>
<header class="page-header">
<?php
the_archive_title( '<h1 class="page-title">', '</h1>' );
the_archive_description( '<div class="archive-description">', '</div>' );
?>
</header><!-- .page-header -->
<?php
// Start the Loop.
while ( have_posts() ) :
the_post();
/*
* Include the Post-Format-specific template for the content.
* If you want to override this in a child theme, then include a file
* called content-___.php (where ___ is the Post Format name) and that will be used instead.
*/
get_template_part( 'template-parts/post/content', get_post_format() );
endwhile;
the_posts_pagination( array(
'prev_text' => __( 'Previous page', 'flowpoint' ),
'next_text' => __( 'Next page', 'flowpoint' ),
'before_page_number' => '<span class="meta-nav screen-reader-text">' . __( 'Page', 'flowpoint' ) . ' </span>',
) );
else :
get_template_part( 'template-parts/post/content', 'none' );
endif;
?>
</main><!-- #main -->
</div><!-- #primary -->
<?php get_sidebar(); ?>
</div><!-- .wrap -->
<?php
get_footer();
This code is based on the default WordPress author template, but it has been modified to work with your custom post type. The key changes are:
- The file name is
author-{post-type}.php
, where {post-type}
is the slug of your custom post type.
- The loop inside the template is designed to display the posts from your custom post type, rather than the default "post" post type.
- Save the file and upload it to your WordPress theme's directory.
Step 2: Register the Custom Author Template
Next, you need to register the custom author template with WordPress. This will ensure that the template is used when someone visits the author page for your custom post type.
- Open your theme's
functions.php
file and add the following code:
/**
* Register custom author template for custom post type.
*
* @param string $template The path of the template to include.
* @param string $type The type of template.
* @return string The path of the template to include.
*/
function flowpoint_author_template( $template, $type ) {
if ( 'author' === $type && get_query_var( 'post_type' ) === 'books' ) {
$template = locate_template( 'author-books.php' );
}
return $template;
}
add_filter( 'archive_template', 'flowpoint_author_template', 10, 2 );
This code adds a custom filter to the archive_template
hook, which allows you to specify a custom template for the author page. In the example above, the custom template is used when the post type is "books." You'll need to modify the 'books'
part to match the slug of your custom post type.
- Save the
functions.php
file and upload it to your WordPress theme's directory.
Step 3: Customize the Author Page Content
Now that you have created the custom author template and registered it with WordPress, you can start customizing the content that will be displayed on the author page.
The default template includes the following elements:
- The author's name and description
- The list of posts written by the author
You can modify these elements to fit your specific needs. For example, you might want to display the author's profile picture, social media links, or a list of their published books (if your custom post type is for books).
To customize the content, you can use the various WordPress functions and template tags available. Here's an example of how you can modify the template to display the author's profile picture and a list of their published books:
<?php
/**
* The template for displaying Author Archive pages
*
* @package WordPress
* @subpackage Flowpoint
* @since 1.0
* @version 1.0
*/
get_header(); ?>
<div class="wrap">
<div id="primary" class="content-area">
<main id="main" class="site-main" role="main">
<?php
if ( have_posts() ) :
?>
<header class="page-header">
<?php
echo get_avatar( get_the_author_meta( 'ID' ), 120 );
the_archive_title( '<h1 class="page-title">', '</h1>' );
the_archive_description( '<div class="archive-description">', '</div>' );
?>
</header><!-- .page-header -->
<div class="author-books">
<h2>Published Books</h2>
<ul>
<?php
$author_id = get_the_author_meta( 'ID' );
$books = new WP_Query( array(
'post_type' => 'books',
'author' => $author_id,
) );
while ( $books->have_posts() ) {
$books->the_post();
?>
<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
<?php
}
wp_reset_postdata();
?>
</ul>
</div>
<?php
// Start the Loop.
while ( have_posts() ) :
the_post();
/*
* Include the Post-Format-specific template for the content.
* If you want to override this in a child theme, then include a file
* called content-___.php (where ___ is the Post Format name) and that will be used instead.
*/
get_template_part( 'template-parts/post/content', get_post_format() );
endwhile;
the_posts_pagination( array(
'prev_text' => __( 'Previous page', 'flowpoint' ),
'next_text' => __( 'Next page', 'flowpoint' ),
'before_page_number' => '<span class="meta-nav screen-reader-text">' . __( 'Page', 'flowpoint' ) . ' </span>',
) );
else :
get_template_part( 'template-parts/post/content', 'none' );
endif;
?>
</main><!-- #main -->
</div><!-- #primary -->
<?php get_sidebar(); ?>
</div><!-- .wrap -->
<?php
get_footer();
In this modified template, we've added the following:
- The author's profile picture using the
get_avatar()
function.
- A new section called "Published Books" that displays a list of all the books (custom post type) written by the author.
You can further customize this template to include additional information or features specific to your needs.
Why You Should Create a Custom Author Page
Creating a custom author page for your WordPress custom post type offers several benefits:
-
Improved User Experience: By providing a dedicated author page for your custom post type, you create a better user experience for your visitors. They can easily access all the content written by a specific author, making it easier for them to explore and engage with your website.
-
Enhanced SEO: A custom author page can help improve your website's search engine optimization (SEO). Search engines can better understand the structure and content of your website, which can lead to higher rankings and better visibility in search results.
-
Increased Engagement: A well-designed author page can encourage your visitors to explore more of your content, leading to increased engagement and potentially higher conversion rates.
-
Flexible Customization: With a custom author page, you have the freedom to tailor the content and presentation to your specific needs. This allows you to showcase your authors in a way that aligns with your brand and website's overall design.
By following the steps outlined in this article, you can create a custom author page that seamlessly integrates with your WordPress custom post type, providing a better user experience and potentially boosting your website's performance.
Remember, the key to a successful custom author page is to focus on the needs and expectations of your target audience. By providing them with relevant and engaging content, you can build a stronger connection with your users and drive more value for your website.
Flowpoint.ai can help you identify all the technical errors that are impacting conversion rates on your website and directly generate recommendations to fix them, including issues with custom post types and author pages
Get a Free AI Website Audit
Automatically identify UX and content issues affecting your conversion rates with Flowpoint's comprehensive AI-driven website audit.