Skip to content

Different Template for Pagination

August 13, 2023

A WordPress code snippet to display a different template for additional pagination pages.

For my KoiKin Dragon page, I have a template that displays the page content, the 12 most recent dragons, and a pagination link at the bottom to continue to the next page of dragons. Instead of using the same template that would display the page content on Page 2+, I wanted to only display the dragons and have a return to main page link between the two navigation links.

I came across this code snippet for the functions.php file that can be modified for whatever purpose you need it for:

add_action( 'template_include','function_name' );
function function_name( $template ){
    if( is_front_page() && is_paged() ){
        $template = locate_template(array('archive.php','index.php'));
    }
    return $template;
}

Original Code Source

The function is_front_page can be replaced with is_page($page), with $page being the Page ID, title, slug, or an array of those to check against.

While you could just have “archive.php” (or any template file you choose to use), it’s recommended to have another file to fallback on in case WordPress can’t locate the first file.

WordPress References:
Hook: template_include
Function: locate_template
Function: is_page