Archive template

WordPress is now using the index.php template file to display every type of web page that's a part of your site. Let's make a new fileone that will be used when viewing the monthly archive, category archive, and tag archive.

To learn more about how WordPress utilizes different files to display content, feel free to read the guide at https://www.codeinwp.com/blog/wordpress-theme-heirarchy/.

To create your archive template, make a copy of index.php and rename it archive.php. Now, navigate to see your monthly archive on the site by clicking on either of the months in the sidebar. At this point, the page you'll see should look exactly like the main listingthe one handled by index.php.

Let's make one change to the archive template. I'd like it to display a message that lets users know what type of archive page they are on. Currently, the archive looks the same as the main index listing, and this isn't the most optimized situation. To fix this, just add the following code the <?php if (have_posts()) : ?> line:

<header class="page-header">
  <?php
  the_archive_title('<h1 class="page-title">', '</h1>');
  the_archive_description('<div class="taxonomy-description">', '</div>');
  ?>
</header>

If I go to see my monthly archive nowor the category/tag archiveI'll get a new heading at the top of the page that lets me know where I am, as shown in the following screenshot:

This version of the archive.php file is available in the code bundle for this chapter, inside a sub-directory called phase 4.