Template Hierarchy - Pagination - home.php overiding index.php (6 posts)

  1. Drew Baker
    Posted 5 years ago #

    Hey Guys,
    I've noticed an inconsistency with the way template hierarchy works (IMHO).

    My theme has a home.php, which displays correctly on the sites homepage. But, when clicking through to "More posts" (www.example.com/page/2) the template that is called is home.php, not index.php as expected.

    My reading settings are: Settings>Reading>Your latest posts

    My understanding is that home.php is only shown as the site's homepage, meaning, the first page displayed. I don't think /page/2/ should use home.php. Is this a bug, or is there a reason why it's made like this?


  2. David Sader
    Posted 5 years ago #

    home.php trumps index.php in the template hierarchy.

    What you are seeing is the way it has always been. /page/2/ will use home.php as well. front-page.php may be what you are after, but you'll need to set it as the front under Settings->Reading.

    If you want some code within either to display conditionally on the home page (ie the static front page or first page) use
    <?php if(is_home()) { ... } ?>

  3. Drew Baker
    Posted 5 years ago #

    Hey David,
    Thanks for that. It still seems unintuitive to me, but if that's they way it is then that's the way it is.


  4. Drew Baker
    Posted 5 years ago #

    OK, so how do I achieve this then?

    http://www.example.com = template1.php
    http://www.example.com/page/2 = template2.php

    If I'm looking at:

    Would the conditional tag is_home be true? If so, what about on:

    I need a way to have the homepage be different than the pagination pages. I don't want to have to set a static page as the front page, because the URL's must stay as http://www.example.com and http://www.example.com/page/2.

    Thanks for the help,

  5. vtxyzzy
    Posted 5 years ago #

    This is UNTESTED, but it may work. Have your home.php choose which template to include:

    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    if ( $paged == 1 ) {
      include(TEMPLATEPATH . '/template1.php');
    } else {
      include(TEMPLATEPATH . '/template2.php');
  6. Drew Baker
    Posted 5 years ago #

    Thanks vtxyzzy,
    I never tried you're way, I got it working doing this before I saw your reply:

    <?php if (!is_paged()) {
                $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
            } ?>
            <?php if (have_posts()) : ?>
            <?php while (have_posts()) : the_post(); ?>

    This is in my home.php file. Thanks anyway!

Topic Closed

This topic has been closed to new replies.

About this Topic