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?
Checking for
get_page_templates()
in the core, I found a workaround that doesn’t break the theme like:I’m using this just after
<body>
and works ok: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.
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)
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-It doesn’t give you what you actually need though, brasofilo and Wyck’s answers are likely the ones you want.