A simple function for checking if a page is using any custom post template in WordPress

WordPress already has an in-built function for determining if the current page is using a particular page template using the function “is_page_template()” while this function definitely serves a purpose.

Sometimes in an instance where you are using one loop file as opposed to multiple loop files you want to check if you’re on a page that is using a page template but don’t care what type of page template it is, this is where the function I’ve created below comes in.

It’s simple. You loop through all available page templates with a foreach loop and check if the current page is using any of the templates.

// A simple function for checking if a page is using a custom template
function using_page_template()
{
    $templates = get_page_templates();
    $result    = FALSE;

    foreach ($templates AS $template_name => $template_filename)
    {
        if ( is_page_template($template_filename) )
        {
            $result = TRUE;
            break;
        } 
    }

    return $result;
}

// We don't need to do anything if we're in the admin section
if (!is_admin())
{
    // Sometimes there is an issue with the function not existing, so we define it
    // if we can't find the default Wordpress function (no harm in doing this)
    if ( !function_exists('get_page_templates') )
    {
        function get_page_templates() 
        {
            return array_flip( wp_get_theme()->get_page_templates() );
        }
    }
}