Custom blog archive.

By default, WordPress shows the blog as the site’s front page. The main blog archive is also known as the home page in WordPress. This is a bit confusing, and an outgrowth of WordPress’s origins as a blogging platform. Typically, home page and front page are synonymous. In WordPress, they are not.

On this site, we have assigned a static page to be the site’s front page. In order to display the blog, we’ve created a page called “Articles”, and we have assigned the blog to that page. In WordPress parlance, this is now the “home page”.

By default, the home page uses index,php, as do a number of other pages: single posts (unless they have been assigned specific templates), author archives, date archives, 404… Tag and category archives use the archive.php file.

On this site, we want the home page and all archive pages to use the same file: archive.php, which we will customize to display posts in a grid.

To do this we need to create a function that will use archive.php for the home page.

Step 1: Define the Function

We start by defining a function called gpc_use_archive_for_homepage. This function takes one parameter, $template, which represents the current template being used.

function gpc_use_archive_for_homepage( $template ) {
     // code goes here.
}

Step 2: Conditional Checks

if ( is_home() && ! is_front_page() ) {
      // do stuff.
}

Inside the function, we have a conditional statement that checks if the current page is the blog home page (using is_home()) and not the front page (using !is_front_page()).

These two conditionals are explained here. The simple explanation is that we are checking to see if this is the home page (ie the main blog page), but not the front page. Normally, this page would use index.php, but we are going to assign archive.php as the template instead.

Step 3: Template Location

$template = locate_template( array( 'archive.php', 'index.php' ) );

Inside the if statement, if the conditions in the previous step are met, we use the locate_template() function to search for two template files: archive.php and index.php. The function will look for these templates in the theme directory and its parent directories. The reason we are searching for index.php and not just assigning archive.php is to make sure there is a fallback in case archive.php cannot be found. It’s a best-practices safety measure.

Step 4: Template Assignment & Return Template

return $template;

If the archive.php template is found, we assign it to the $template variable. If for some reason the archive.php template isn’t found, we assign index.php to the template variable. This means that either the archive.php or the index.php template will be used as the template for the current page.

We return the $template value, which now holds the assigned template, to make sure it is used by WordPress for rendering the page.

Step 5: Hooking the Function

add_filter( 'template_include', 'gpc_use_archive_for_homepage', 99 );

Finally, we use the add_filter() function to hook our gpc_use_archive_for_homepage function into the template_include filter. This filter allows us to modify the template that will be included for the current page. By adding our function to this filter with a priority of 99, we ensure that it is applied when determining the template for the current page.

The entire function is here:

// Define gpc_use_archive_for_homepage function
function gpc_use_archive_for_homepage( $template ) {
  if ( is_home() && ! is_front_page() ) {
    $template = locate_template( array( 'archive.php', 'index.php' ) );
  }
  return $template;
}

// Hook gpc_use_archive_for_homepage into template_include
add_filter( 'template_include', 'gpc_use_archive_for_homepage', 99 );

That’s it! The gpc_use_archive_for_homepage function checks if the current page is the blog home page but not the front page, and assigns either the archive.php or the index.php template based on availability. By using the add_filter() function, we integrate this functionality into WordPress and override the default template selection for the blog home page.

Leave a Reply

Your email address will not be published. Required fields are marked *