scarstens
4/24/2013 - 12:13 AM

This is a WordPress function that will extend the "template heirarchy" so that you can also load child pages in your theme using the followi

This is a WordPress function that will extend the "template heirarchy" so that you can also load child pages in your theme using the following syntax: page-{PARENTSLUG}-child-{CHILDSLUG}.php

<?php 
//apparently no need for this... page-SLUG.php matches subpage slugs,
//only need this function if subpage slug conflicts occur

//Add this code to you themes functions.php file, or some other file. 
//Next create your child page lets says "domain.com/resources/faq"
//Finally, create the php template page following the naming schema:
//page-PARENTSLUG-child-CHILDSLUG.php -> page-resources-child-faq.php
add_filter( 'page_template', 'enable_child_page_templates' );
function enable_child_page_templates( $page_template ){
    global $wp;
    $slugs = explode('/', $wp->request);
    if(isset($slugs[0]) && isset($slugs[1])) {
        $f = __dir__.'/page-'.$slugs[0].'-child-'.$slugs[1].'.php';
    }
    if (isset($f) && file_exists($f) )
        return $f;
    else
        return $page_template;
};