I have this issue where I need to get all page templates. I know there are ways to get them based on their name. I know that I can include a page template but I am trying to include them in the loop on the index page. I just installed the 2012 theme, modified the header to include my scripts. I used a function that Milo offered from this site to load all posts and pages into the homepage which is working. Now I need to include all templates even those that I don't know their name.

looking in the codex, I found a function called get_page_templates() that doesn't appear to be valid anymore. it gave this snippet

    $templates = get_page_templates();
    foreach ( $templates as $template_name => $template_filename ) {
    echo "$template_name ($template_filename)<br />";

This code snippet should have done the trick but it gave me an invalid function error. I messed around with get_page_template() but all it would return for me is page.php

Looking in the database in the _wp_post_meta, I see the field _wp_page_template and I can see that it holds all the template names that I need to include. I am just not sure how to get those names out of that field.

I tried this

$template = get_post_meta($post->ID,'_wp_page_template',true);

but when I checked the value of $template, I found nothing.

can you help?

4 Answers 4


Checking for get_page_templates() in the core, I found a workaround that doesn't break the theme like:

Fatal error: Call to undefined function get_page_templates()

I'm using this just after <body> and works ok:

$templates = wp_get_theme()->get_page_templates();

foreach ( $templates as $template_name => $template_filename ) 
    echo "$template_name ($template_filename)<br />";
  • I think this will only return the "page" templates and not the entire list of files.
    – Wyck
    Jan 26, 2013 at 3:01
  • @Wyck, yes, that's what I understood of the Q...
    – brasofilo
    Jan 26, 2013 at 3:03
  • It used to return all the files at some point , I think it was confusing.
    – Wyck
    Jan 26, 2013 at 3:15
  • this is an interesting solution. What if you want to get the template information though? Like the_title? Your snippet just returns template names right? I forgot to take into account in my original post that I do also need other information like the_title outside of the template query. Some very helpful information in this thread. I like it
    – Jamie
    Jan 26, 2013 at 3:24
  • @Jamie, but the_title is a property of a post, not of a template file by itself, no? Check the content of $firephp->log(wp_get_theme());
    – brasofilo
    Jan 26, 2013 at 3:38

That example in the codex is not valid anymore, this code was changed somewhere between 3.3--3.5.

An alternative way to is to use the following.

$stylesheet = get_stylesheet();
$theme = wp_get_theme( $stylesheet );
// this will only return .php files extend as needed
$allowed_files = $theme->get_files( 'php', 1 );

var_dump($allowed_files); // see what is there

I figured it out. This was my solution. Do you have a better one? after the endwhile, I did this ( all my templates are stored in the inc folder)

 <?php $args = array( 'meta_key' => '_wp_page_template');
        $templates = get_pages($args);
        foreach ( $templates as $template){
            $templateName = $template->meta_value;
            if( $templateName != 'default' && $templateName !='') include('inc/'. $templateName);

It's a valid function, you just can't use it on the front end because theme.php file isn't loaded on the front end, you'd have to include it-

include_once ABSPATH . 'wp-admin/includes/theme.php';
$templates = get_page_templates();

It doesn't give you what you actually need though, brasofilo and Wyck's answers are likely the ones you want.

  • But why does wp_get_theme()->get_page_templates() work by itself? I get that the OP wants all page templates, not theme templates...
    – brasofilo
    Jan 26, 2013 at 3:18
  • @brasofilo it's a different file, wp-admin/includes/theme.php vs wp-includes/theme.php
    – Milo
    Jan 26, 2013 at 3:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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