get page templates

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

Read More
 <?php 
    $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?

Related posts

Leave a Reply

4 comments

  1. 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 />";
    }
    
  2. 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
    
  3. 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);
            } 
    
  4. 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.