I have the following code that allows me to use custom templates on my WP site based of a given url.

//* Create Variables for URL Templates
function sjc_add_query_vars($vars) {
return array('template') + $vars;
}
add_action('query_vars', 'sjc_add_query_vars',10,1);

//* Load Cutstom Page Templates by URL
function sjc_template($template) {
global $wp;
if ($wp->query_vars['template']=='basic') {
global $post;
$posttype = get_post_type($post->ID);
return dirname( __FILE__ ) . '/single-'. $posttype .'-basic.php';
}
else {
return $template;
}
}
add_filter( 'template_include', 'sjc_template', 99 );

This works great. The problem is that if you click any links off that page the url reverts and the default template is used. So, is there a way to make any links on the custom template pages keep using that custom template?

Read more here: Make Subsequent urls retain query_vars.


Solution:

If you know the solution of this issue, please leave us a reply in Comment section, to update the question.


Wordpress related questions and answers: