page_template_dropdown() WordPress Function

The page_template_dropdown() function allows you to select a custom page template for a specific post or page. This can be useful if you want to use a different template for a specific post or page, or if you want to change the template for an existing post or page.

page_template_dropdown( string $default_template = '', string $post_type = 'page' ) #

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


Parameters

$default_template

(string)(Optional) The template file name.

Default value: ''

$post_type

(string)(Optional) Post type to get templates for. Default 'post'.

Default value: 'page'


Top ↑

Source

File: wp-admin/includes/template.php

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>';
	}
}


Top ↑

Changelog

Changelog
VersionDescription
4.7.0Added the $post_type parameter.
1.5.0Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.