WordPress doesn’t select page template for parent pages

I’m developing a WP Theme and I’m having this issue that’s driving me crazy…

For every single (parent) page I create, WP chooses index.php as template. I’m aware of WP template hierarchy but it doesn’t matter if I use page.php, page-$slug.php or page-$id.php, I’ve also tried using:

Read More
<?php
  /*
  Template Name: My Template 
  */
?>

and assigning the page this template in the Editor.

I’ve also hooked, for debugging purposes, template_include as follows:

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

function check_template_selection( $template ) {
    global $post;
    echo "Post ID: " . $post->ID . "<br/>";
    echo "Template File:" . $template . "<br/>";
    echo is_page() ? "YES" : "NO";
    return $template;
}

And for every page URL I check (for example: http://example.com/mypage) I’m getting:

Post ID:

Template File:/nfs/c11/h05/mnt/205383/domains/example.com/html/wp-content/themes/mytheme/index.php

NO

I’ve tried deleting the pages and creating them again, but it’s the same. The fact is it works fine for child pages. For example: http://example.com/mypage/mychildpage shows:

Post ID:589

Template File:/nfs/c11/h05/mnt/205383/domains/example.com/html/wp-content/themes/mytheme/page-mychildpage.php

YES

I’m using WP 4.4.2, ACF PRO 5.3.6.1, Custom Post Type UI 1.2.4 and Instagram Feed Pro Personal 2.0.4.1. No other plugins. Anyway, I tried unsuccessfully to desactivate them all. I’ve developed the theme from scratch and I’ve done dozens of themes before and I’ve never experienced this issue before…

This is my .htaccess and my permalinks are properly configured as /%postname%/, no category base.

# BEGIN WordPress

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress

So please, any advice or hint on what am I missing here would be very apreciated. Thank you guys!


Edit:

After hours of deep search (Thank you so much John Blackbourn for your Query Monitor plugin) I found that my parent pages match the wrong rewrite rule, so now I guess it’s something about rewrite rules priority.

Wrong match for a parent page URL: http://example.com/about

Wrong Match
Wrong Rule Selected

Right match for a child page URL: http://example.com/about/about-child

Right Match
Right Rule Selected

Related posts

1 comment

  1. It’s a few years later, but I ran into an almost identical problem. Using the Query Monitor plugin as you did also helped me figure it out.

    What I had did earlier today was make a custom taxonomy named year. The idea was to group my posts—which are now vehicles for my client’s site—by the year the vehicle was manufactured.

    However, like you, when I went to any top level page I was directed to the index.php template file instead of the page.php template file.

    By using the Query Monitor plugin, I saw that I was getting the exact same results as you. My matched query was year=about.

    I removed my custom year taxonomy and then refreshed my permalinks by saving them through the WordPress admin Settings > Permalinks and voila, my top level pages are now going to the correct page.php template file.

Comments are closed.