WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a custom post type called movies and I need to quickly switch the template that displays the movies cpt on the front end via a link. How can this be done?

Files I have: single-movies.php template-movieslanding.php template-movieswholesale.php

Functionality: A link group is displayed on each of the three templates as demonstrated below. These links are only displayed to logged in staff. The staff are not technical in the least and need a simplistic solution.

Views: Overview | Landing Page | Wholesale

When each link is clicked I need to switch to that template. All templates use the same custom post type.

Thanks! ~Matt

share|improve this question
up vote 5 down vote accepted

you can do it like this:

//add movies_view to query vars
add_filter('query_vars', 'my_query_vars');

function my_query_vars($vars) {
    // add movies_view to the valid list of variables
    $new_vars = array('movies_view');
    $vars = $new_vars + $vars;
    return $vars;

then add a template redirect based on that query_var:

add_action("template_redirect", 'my_template_redirect');

// Template selection
function my_template_redirect()
    global $wp;
    global $wp_query;
    if ($wp->query_vars["post_type"] == "movies")
        // Let's look for the property.php template file in the current theme
        if (array_key_exists('movies_view', $wp->query_vars) && $wp->query_vars['movies_view'] == 'overview'){
            include(TEMPLATEPATH . '/single-movies.php');
        if (array_key_exists('movies_view', $wp->query_vars) && $wp->query_vars['movies_view'] == 'landing'){
            include(TEMPLATEPATH . '/template-movieslanding.php');
        if (array_key_exists('movies_view', $wp->query_vars) && $wp->query_vars['movies_view'] == 'wholesale'){
            include(TEMPLATEPATH . '/template-movieswholesale.php');

then add this var to your links

  • For Overview add ?movies_view=overview to the url
  • For Landing Page add ?movies_view=landing to the url
  • For Wholesale add ?movies_view=wholesale to the url

Hope this helps

share|improve this answer
That worked perfectly. Thank you! – mattrepublic Feb 6 '11 at 13:01
Glad i could help. – Bainternet Feb 6 '11 at 13:37
I don't think it's 100% ideal from a pretty permalinks perspective but it is a pretty nice solution. I ran a test with Addthis and unfortunately the Twitter share link directs users to the original template so this solution is not going to work for the site I'm working on. – Jason Paul Apr 30 '11 at 22:04
Nice, also this could easily be modified to have "pretty url's" as well – JasonDavis Apr 11 '12 at 9:25

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.