Solving The WordPress Template Hierarchy Puzzle: Author Archives

Josh Pollock - May 20, 2014

http://joshpress.net/blog/category/wordpress/tutorials/solving-the-wordpress-template-hierarchy-puzzle/http://joshpress.net/blog/category/wordpress/tutorials/solving-the-wordpress-template-hierarchy-puzzle/

So far in my series on understanding the WordPress template hierarchy I have covered a lot of ground. This has included what template in the active theme are used for displaying posts, pages, custom post types, and taxonomies, including categories, tags and custom taxonomies. Today I begin to get in to the more obscure parts of the template hierarchy by looking at author archives.

Author Archives

Author archives are pages that show all posts by an author. At least that is their intended use and how they are used in most themes. With a little work they can be used for user profiles. But that is a topic for another day.

The template hierarchy for author archives is as follows:

author-{nicename}.php -> author-{id}.php -> author.php -> archive.php -> index.php

Like all archives, if no specific author template exists, WordPress will fall back to archive.php, and if that doesn’t exist, it will fallback to index.php. You can also create a template just for author archives using “author.php” and create special templates for each author.

For author specific template files, you can use either the author nicename or author ID to name the template. Nicenames, which take precedence over IDs are the slug that you see in the URL for an author profile when using pretty permalinks.

This means that if you have an author whose nicename is “luke-skywalker” and the user ID of 4, the template “author-luke-skywalker.php” would be used to display all posts by this author. This is true, whether “author-4.php” exists or not. If “author-luke-skywalker.php” did not exist, and “author-4.php” did exist, “author-4.php” would be used.

Since nicenames are automatically generated based on other fields, I do not recommend using them to name templates. If this user changed his first name from “Luke” to “Master” his nicename would become “master-skywalker” and the “author-luke-skywalker.php” template would no longer be used for his archive.

Conditional Functions For Author Archives

There is only one important conditional function for author archives–is_author(). If used with out passing any parameters is_author() will return true on any author archive. You may also pass a user ID, or user nicename, or even an array of IDs and/ or nicenames to is_author(). For example:

if ( is_author() ) {
	//true for any author archive.
}
if ( is_author( 4 ) ) {
	//true for author archive for the author with the user ID 4
}
if ( is_author( array( 4, 'mara-jade-skywalker' ) ) ) {
	//true for author archive for the author with the nice name 'mara-jade-skywalker' or the user ID 4
}
if ( !is_author( array( 'luke-skywalker', 'mara-jade-skywalker', 'ben-skywalker' ) ) ) {
	//true for author archives when the author nicename IS NOT 'luke-skywalker', 'mara-jade-skywalker' or 'ben-skywalker'
}