Dissecting WordPress Themes Part 7: Author Hierarchy

In the past two articles of the “Dissecting WordPress Themes” series we examined the post archive segment of the template hierarchy for categories and tags. In this article, we’ll continue working our way through the archives with the author archive. The author archives provide a mechanism for readers to search for and filter all posts written by a particular author. The UI for this action is usually implemented with a hyperlink on the author’s name.

As with the category and tag archives, the author archive is rooted at the archive.php template (which itself falls back to index.php). Beyond that it has a template level that applies to all authors as well as two levels that allow for special-purpose formatting of a particular author. In order to demonstrate the hierarchy, we’ll first create some users and then assign them to our existing posts as authors. Then we will implement the templates level-by-level to show how the overrides work.

Create and Assign Authors

After installing WordPress, you will have a single user that is created from an administrator ID that you supply during the installation. In the case of my installation, this user is called lab1. It is recommended that this user be dedicated to site administration and maintenance. Additional users should then be created as necessary for the purposes of creating and managing the content of the site. This security protocol reduces the risk of a blog author inadvertently changing configuration options, de-activating themes, uninstalling plugins, etc. Even when the administrator and the author are the same person, it is a good practice to have different user accounts for these different roles.

For the purposes of this article, we will create three users and assign them the author role. However, we’ll just assign the various existing posts to these users as the administrator instead of logging in as each user to create additional posts. Log in to the Administration Screen and select Users→Add New from the left menu. This will display the Add New User page as shown below.

Add New User page

The Add New User page lets you create new users of your site. It is good practice to create a user separate from your admin user for content management activities. This user can be assigned to a less-privileged role like Author, Contributor, or Editor.

Enter the Username, E-mail, First Name, Last Name, Password, and Role as shown in the table below. You can leave the Send Password? box unchecked. For each user remember to click the Add New User button.

Username E-mail First Name Last Name Password Role
userx userx@acme.com User Xray userx Author
usery usery@acme.com User Yankee usery Author
userz userz@acme.com User Zulu userz Author

After adding the new users, choose Users→All Users from the admin menu to view the Users screen. Your screen should look similar to this.

Users page

The Users page shows our new user information.

Next, select the Posts→All Posts menu item to view the Posts page. As we left the database from the prior article, we have 10 posts and three Books (a custom post type). You can update the author of each post using the Quick Edit function by hovering over the post title and clicking the Quick Edit link. Then select the user from the drop-down list next to Author as shown in the table below. Remember to click the Update button after each change.

Post Title Author
Post 1 User Xray
Post 2 User Yankee
Post 3 User Zulu
Post 4 User Xray
Post 5 User Yankee
Post 6 User Zulu
Post 7 User Xray
Post 8 User Yankee
Post 9 User Zulu
Post 10 User Xray
Book 1 User Yankee
Book 2 User Zulu
Book 3 User Xray

After assigning users to all of the posts, select Posts→All Posts from the left menu to view the Posts page as shown below.

Posts page

The Posts page shows author assignments to our existing posts.

Display Assigned Authors

Now that we have some author assignments to play with, let’s modify our index.php template to display them. Open index.php in your editor and add the code shown below. I added the Author column header to the table (after Format) and added a call to the_author_posts_link(). This call returns the complete anchor tag (<a>) with href, title, and rel attributes as well as the anchor text.

<?php
get_header(); 
echo "<p>In index.php</p>\n";

if (have_posts()) {   // if there are posts
	echo "<table>\n";
	echo "<tr><th>ID</th><th>Title</th><th>Format</th><th>Author</th>",
			"<th>Category</th><th>Tags</th></tr>\n";
	while (have_posts()) {   // start the loop
		the_post();
		echo "<tr>";
		echo "<td>", get_the_ID(), "</td>";
		echo "<td><a href='", get_permalink(), "'>",
				get_the_title(), "</a></td>";
		$postFormat = get_post_format();
		echo "<td>", $postFormat ? $postFormat : 'standard', "</td>";
		echo "<td>", the_author_posts_link(), "</td>";
		echo "<td>", get_the_category_list(', '), "</td>";
		echo "<td>", get_the_tag_list('', ', '), "</td>";
		echo "</tr>\n";
		}
	echo "</table>\n";
	next_posts_link("Older posts");
	echo "<br/>";
	previous_posts_link("Newer posts");
} else {
	echo "<p>no posts to display</p>\n";
}
    
get_sidebar();
get_footer();
?>

Save your changes to index.php and visit the site’s home page by hovering your mouse over the site name in the toolbar and selecting Visit Site. You should see the index.php template execute with the Author column as shown below. Click the Older posts link to see that the authors are correctly assigned for the first three posts also.

Home page with author assignments

Our update home page listing now shows the author assignments for each post.

If you hover your mouse over the URLs generated in the Author column, you will note that the format is http://localhost/lab1/author/[username]. The author component of the URL indicates to WordPress that an author archive is requested, while the [username] component provides the username of the requested author. Click on one of the author links, say User Xray at the top of the list. You will notice that the archive.php template executes and displays the posts authored by userx. Let’s also add the Author column to this table to make this easier to see. Make the same changes to your archive.php template file as you made to index.php earlier.

<?php
get_header(); 
echo "<p>In archive.php</p>\n";

if (have_posts()) {   // if there are posts
	echo "<table>\n";
	echo "<tr><th>ID</th><th>Title</th><th>Format</th><th>Author</th>",
			"<th>Category</th><th>Tags</th></tr>\n";
	while (have_posts()) {   // start the loop
		the_post();
		echo "<tr>";
		echo "<td>", get_the_ID(), "</td>";
		echo "<td><a href='", get_permalink(), "'>",
				get_the_title(), "</a></td>";
		$postFormat = get_post_format();
		echo "<td>", $postFormat ? $postFormat : 'standard', "</td>";
		echo "<td>", the_author_posts_link(), "</td>";
		echo "<td>", get_the_category_list(', '), "</td>";
		echo "<td>", get_the_tag_list('', ', '), "</td>";
		echo "</tr>\n";
		}
	echo "</table>\n";
	next_posts_link("Older posts");
	echo "<br/>";
	previous_posts_link("Newer posts");
} else {
	echo "<p>no posts to display</p>\n";
}
    
get_sidebar();
get_footer();
?>

Now refresh the archive.php template. It should now be apparent that the Loop has filtered the post list for posts authored by User Xray. Return to the home page and select User Yankee and User Zulu to verify that they each execute archive.php and filter posts based on the chosen author.

Archive.php template with author assignments

The archive.php template overrides index.php for author archives. Our updated version shows the author assignments.

Recall that archive.php is overriding index.php for all archive requests: categories, tags, authors, dates, custom taxonomies, and custom post types. To prove that to yourself you can temporarily rename archive.php and refresh the page. You should then see that index.php takes its place. Next, we’ll put in some new templates to demonstrate the author archive hierarchy.

Exercise The Hierarchy

Since archive.php is common to all of the  archive types, you may have a need to format the author archives differently from the others. You can accomplish this by introducing the author.php template file to override archive.php for author requests. Copy archive.php to author.php and make the changes indicated below. I changed the trace message and added a new message to display the author that was selected. I also removed the author column since that information is now redundant.

<?php
get_header(); 
echo "<p>In author.php</p>\n";
$author = get_queried_object();
$name = $author->display_name;
echo "<p>Browsing author ", $name, "</p>\n";

if (have_posts()) {   // if there are posts
	echo "<table>\n";
	echo "<tr><th>ID</th><th>Title</th><th>Format</th>",
			"<th>Category</th><th>Tags</th></tr>\n";
	while (have_posts()) {   // start the loop
		the_post();
		echo "<tr>";
		echo "<td>", get_the_ID(), "</td>";
		echo "<td><a href='", get_permalink(), "'>",
				get_the_title(), "</a></td>";
		$postFormat = get_post_format();
		echo "<td>", $postFormat ? $postFormat : 'standard', "</td>";
		echo "<td>", get_the_category_list(', '), "</td>";
		echo "<td>", get_the_tag_list('', ', '), "</td>";
		echo "</tr>\n";
		}
	echo "</table>\n";
	next_posts_link("Older posts");
	echo "<br/>";
	previous_posts_link("Newer posts");
} else {
	echo "<p>no posts to display</p>\n";
}
    
get_sidebar();
get_footer();
?>

In order to get the author’s display name outside the loop, I called get_queried_object() to retrieve the author object being queried and then used that to get the display_name attribute. There are other ways to do this—see the Author Templates reference at the end of the article for other options. After making these changes, an author query will now use the author.php template as shown below:

Author.php overrides archive.php

The author.php template overrides archive.php for author archive pages.

Next up in the author hierarchy is author-[id].php. This template allows you to specialize formatting for a particular author. However, you must first find the ID for the author you want to target. To do this, head back to the Administration Screen and select Users→All Users from the left menu. Hover your mouse over userx and select Edit. Examine the URL query string for the user_id property. The value for that property is the user’s ID. In my case, that value is 2 as shown below. It may be different on your system. Note the user ID for use in the next step.

Edit User page

The Edit User page can be used to find a user’s ID. Look for the value of the user_id property in the URL query string. The ID for this user is 2.

Now, copy the author.php file to author-[id].php where [id] is the user ID for userx as noted above. In my case, I copied author.php to author-2.php. Alter the trace message to In author-[id].php, replacing the [id] with your value. My author-2.php template code is shown below:

<?php
get_header(); 
echo "<p>In author-2.php</p>\n";
$author = get_queried_object();
$name = $author->display_name;
echo "<p>Browsing author ", $name, "</p>\n";

if (have_posts()) {   // if there are posts
	echo "<table>\n";
	echo "<tr><th>ID</th><th>Title</th><th>Format</th>",
			"<th>Category</th><th>Tags</th></tr>\n";
	while (have_posts()) {   // start the loop
		the_post();
		echo "<tr>";
		echo "<td>", get_the_ID(), "</td>";
		echo "<td><a href='", get_permalink(), "'>",
				get_the_title(), "</a></td>";
		$postFormat = get_post_format();
		echo "<td>", $postFormat ? $postFormat : 'standard', "</td>";
		echo "<td>", get_the_category_list(', '), "</td>";
		echo "<td>", get_the_tag_list('', ', '), "</td>";
		echo "</tr>\n";
		}
	echo "</table>\n";
	next_posts_link("Older posts");
	echo "<br/>";
	previous_posts_link("Newer posts");
} else {
	echo "<p>no posts to display</p>\n";
}
    
get_sidebar();
get_footer();
?>

Return to the home page and select User Xray from one of the posts. You should see that author-[id].php now executes instead of author.php. Check the other two users to verify that they still use the common author.php template as before.

Author-2.php

The author-[id].php template overrides author.php for a particular author archive query. Here author-2.php is executed when a request is made for the userx author archive.

The final level in the author archive hierarchy is author-[nice name].php. This template gives you an alternative to using the author’s ID when targeting a particular author archive. The nice name is just the user name that you assigned when the user was created, e.g. userx. Admittedly, nice names are usually nicer than this one. Copy the author.php template to author-userx.php, change the trace message, and save the file. The listing for author-userx.php follows.

<?php
get_header(); 
echo "<p>In author-userx.php</p>\n";
$author = get_queried_object();
$name = $author->display_name;
echo "<p>Browsing author ", $name, "</p>\n";

if (have_posts()) {   // if there are posts
	echo "<table>\n";
	echo "<tr><th>ID</th><th>Title</th><th>Format</th>",
			"<th>Category</th><th>Tags</th></tr>\n";
	while (have_posts()) {   // start the loop
		the_post();
		echo "<tr>";
		echo "<td>", get_the_ID(), "</td>";
		echo "<td><a href='", get_permalink(), "'>",
				get_the_title(), "</a></td>";
		$postFormat = get_post_format();
		echo "<td>", $postFormat ? $postFormat : 'standard', "</td>";
		echo "<td>", get_the_category_list(', '), "</td>";
		echo "<td>", get_the_tag_list('', ', '), "</td>";
		echo "</tr>\n";
		}
	echo "</table>\n";
	next_posts_link("Older posts");
	echo "<br/>";
	previous_posts_link("Newer posts");
} else {
	echo "<p>no posts to display</p>\n";
}
    
get_sidebar();
get_footer();
?>

Again, select User Xray from one of the posts on the home page and notice that author-userx.php overrides author-[id].php.

Author-[nicename].php template

The author-[nicename].php template can be used to provide special formatting for a particular author. It will override the author-[id].php template when both are present.

As with categories and tags, it’s not likely that you would use both the author-[id].php and author-[nice name].php templates for the same user as I did in this demonstration. However, you have both methods of user identification available to you if want to provide a format specific to a particular author.

We can now expand upon our template hierarchy diagram by adding the author hierarchy. As you can see, it is very similar to the category and tag hierarchies that we explored in the prior two articles.

Template Hierarchy with Author Archives

The author archive hierarchy is similar to the category and tag archives covered in the last two articles. All of the archives will use archive.php (if present) instead of index.php. In turn, author archives will use author.php instead of archive.php and specialized formatting for a particular author can be provided with author-[id].php and author-[nicename].php.

Display Author List in the Footer

Next, let’s add a list of author links to our footer.php file. As usual, WordPress makes this ridiculously easy. I just called wp_list_authors() from footer.php to create the list. The wp_list_authors() call has many parameters to control its output, but I just used the default values. Interestingly, the call includes <li> and </li> tags around the links but it doesn’t include the <ul> and </ul> tags, so I just added them in the echo statement. Here’s the code:

<?php
echo "<p>In footer.php</p>\n";
wp_tag_cloud();
echo "<p>Authors:</p><ul>", wp_list_authors(), "</ul>";
echo "</body>\n";
echo "</html>\n";
?>

Here’s what the home page looks like with the author list. Note that the links generated are the same as those on the individual posts, that is, links to the archive page for each author.

Author list in the footer

Adding a list of author links in the sidebar or footer is simple with the wp_list_authors() call.

In this article we took a look at the author archive hierarchy. As with the rest of the archive hierarchy, it is rooted with archive.php, which itself overrides index.php. Author archives can attain special formatting using author.php. For special formatting of particular authors, you can use author-[id].php and author-[nice name].php.

In the next article, we continue with the archive hierarchy by examining the date hierarchy which allows you to filter based on day, month, and year of publication. Fun stuff!

References

Speak Your Mind

*