WordPress.org

Support

Support » How-To and Troubleshooting » Template Hierarchy – Pagination – home.php overiding index.php

Template Hierarchy – Pagination – home.php overiding index.php

  • Drew Baker
    Member

    @dbaker

    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?

    Thanks,
    Drew

Viewing 5 replies - 1 through 5 (of 5 total)
  • David Sader
    Participant

    @dsader

    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()) { ... } ?>

    Drew Baker
    Member

    @dbaker

    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.

    Thanks!
    Drew

    Drew Baker
    Member

    @dbaker

    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:
    http://www.example.com

    Would the conditional tag is_home be true? If so, what about on:
    http://www.example.com/page/2

    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,
    Drew

    vtxyzzy
    Member

    @vtxyzzy

    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');
    }
    Drew Baker
    Member

    @dbaker

    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;
                $args=array(
                   'category_name'=>'topics',
                   'posts_per_page'=>2,
                   'paged'=>$paged,
                   );
                query_posts($args);
            } ?>
    
            <?php if (have_posts()) : ?>
            <?php while (have_posts()) : the_post(); ?>

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

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Template Hierarchy – Pagination – home.php overiding index.php’ is closed to new replies.