page_template_dropdown()
Print out option HTML elements for the page templates drop-down.
Uses: get_page_templates()
No Hooks.
Return
null
. Nothing (null).
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: 'page'
Examples
#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() page template dropdown code WP 6.7.2
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>'; } }