Select page template via url parameter
-
I’ve found some old ways of doing this for posts ( for example https://scratch99.com/wordpress/development/how-to-change-post-template-via-url-parameter/), but it doesn’t seem to work for pages. Not sure why. For example, I’d like to be able to do:
mysite.com/mypage/?template=page_noheaderfooter
I know my template works because if I select it in the page editor it produces the result I want. Here’s the code I’ve been playing with which I modified slightly from the example provided above:
function template_query_vars($vars) {
return array(‘template’) + $vars;
}
add_filter(‘query_vars’, ‘template_query_vars’);function url_template($template) {
global $wp;
if ($wp->query_vars[‘template’]==’full’) {
return dirname( __FILE__ ) . ‘/page_noheaderfooter.php’;
}
else {
return $template;
}
}add_filter(‘template_include’, ‘url_template’);`
- The topic ‘Select page template via url parameter’ is closed to new replies.