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

 <?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?

4 s
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 />";
}

Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *