page_template_dropdown()WP 1.5.0

Print out option HTML elements for the page templates drop-down.

No Hooks.

Return

null. Nothing.

Usage

page_template_dropdown( $default_template, $post_type );
$default_template(string)
The template file name.
Default: ''
$post_type(string)
Post type to get templates for.
Default: 'post'

Examples

0

#1 Demo

<?php
//for the front
require_once ABSPATH . '/wp-admin/includes/theme.php';
require_once ABSPATH . '/wp-admin/includes/template.php';
?>

<select name="" id="">
	<?php page_template_dropdown(); ?>
</select>
<?php

The result is this HTML:

<select name="" id="">    
	<option value='page_tpl_custom.php'>My page template</option>
	<option value='page_tpl_custom2.php'>Another page template</option>
</select>

Changelog

Since 1.5.0 Introduced.
Since 4.7.0 Added the $post_type parameter.

page_template_dropdown() code WP 6.1.1

function page_template_dropdown( $default_template = '', $post_type = 'page' ) {
	$templates = get_page_templates( null, $post_type );

	ksort( $templates );

	foreach ( array_keys( $templates ) as $template ) {
		$selected = selected( $default_template, $templates[ $template ], false );
		echo "\n\t<option value='" . esc_attr( $templates[ $template ] ) . "' $selected>" . esc_html( $template ) . '</option>';
	}
}