Paulund
2014-04-28 #wordpress

Get A List Of All Theme Page Templates

WordPress allows you to create custom page templates to use through out your site, using these page templates you can completely change the look and functionality of a certain page.

Create Page Template

To create a custom page template all you have to do is add a new blank php file to your theme directory and save this as custom_page_template.php. Within this new file all you have to do is add comments to the top of the file.


<?php
/*
Template Name: Custom Page Template
*/
?>

Adding this to the top will automatically add this file to a drop down when adding a new page, from the drop down you will see the default template (page.php) and Custom Page Template. If you select the new page to use the Custom Page Template then this will override the page.php and your page will use your custom page template. ## Get A List Of All Page Templates

If you are developing something for WordPress and need to get a list of all page templates that currently exist on the WordPress theme then you can use the following code snippet. Using the WordPress function wp_get_theme() will return an object for the current theme populated with important information about the theme.


$themeObj = wp_get_theme();

On this theme object there is a method avaiable for get_page_templates() which will return an array of all the page templates available on the theme.


<?php
    $theme = wp_get_theme();
    $templates = $theme->get_page_templates();
    
    foreach ( $templates as $template_name => $template_filename ) 
    {
        
    }
?>