0

I have theme where I want to have the home page look different to the rest of the post lists. I used home.php to create the homepage and assumed that when Older Posts was clicked /page/2 would use index.php but this appears to be wrong.

Is there a combination of theme files that can do this? or do I need to use a static page for my homepage with the contents of home.php.

My interpretation of the codex was:

  • example.com/ -> home.php
  • example.com/page/2 -> index.php
  • example.com/category/foo/ -> archive.php
  • A template file isn't the only way to modify a page. Look at using pre_get_posts to filter output based on certain conditions, like /page/2 – jdm2112 Jul 14 '16 at 14:54
2

I think you have a misunderstanding what the homepage (index page) is in WordPress context. The homepage is the main page of the site, by default, this will be like example.com, AND any paged page derived from this homepage. So example.com/page/2 is also regarded as the homepage. In short, any page where is_home() returns true will be a home page.

By default, index.php will be used for the homepage. home.php will be used, if available, for the blogpage, if set, when a static front page is set.

If you need page one of the homepage to be quite different styling wise than the other paged pages of the homepage, my best bet would be to create a custom template outside the normal hierarchy, and then use the home_template filter to include this template whenever page one is viewed of the homepage.

EXAMPLE:

Lets create our custom template and call it index-home.php. This naming convention is unique and does not exist in normal hierarchy, so we are save here. Now, we just need to include it

add_filter( 'home_template', function ( $template )
{
    // Only target page one    
    if ( is_paged() )
        return $template;

    // We are on page one of the homepage, lets locate and include our template
    $locate_template = locate_template( 'index-home.php' );

    if ( !$locate_template )
        return $template;

    return $locate_template;
});

You can also just modify index.php and add your custom styling and code for page one in a is_paged() condition. The only drawback to this might be that index.php is a fallback to all pages regardless, so this would make index.php an unmaintainable mess

EXAMPLE:

if (    !is_paged()
     && is_home()
) {
    // Add code etc for page one homepage
} else {
    // Add code etc for all oher pages
}

Just a final note, the main query would still be executed normally. If you need to alter this, you'll have to use pre_get_posts to alter the behavior of the main query

| improve this answer | |
  • add_filter('home_template', function($template){ if(is_paged()){ return locate_template('index'); }else{ return $template; } }); Does exactly what I need thanks! – Arcath Jul 14 '16 at 16:15
  • Glad it worked. I still do not really know why I added the is_home() condition. It is like installing a cooling system in the middle of the south pole. ;-) – Pieter Goosen Jul 14 '16 at 16:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.