10 Responses to “How To Change The WordPress Post Template Via URL Parameter”

This page contains comments from the How To Change The WordPress Post Template Via URL Parameter article.

  1. dains says:

    Hi, I just wanted to say that I commissioned nearly identical functionality from a developer for exactly the same reason! My implementation uses pages, so it was easier as pages already have a fully supported template system, but we had to ensure any page viewed in a popin had the correct template, and that meant a dynamic switching system for the page templates.

    I wanted to add that this also addresses a functionality issue when using popins to display WP content, as if the header file loads again, WP can pitch a fit over duplicate headers and possibly even whitescreen the page on you depending on what plugins are doing in wp_header. So this little trick is more important than it may seem :)

  2. plainflavour says:

    Hi,
    I’m trying to achieve the same thing, but for pages rather than posts.

    I’m having problems though. Firstly – I get an error in my functions.php with this line:

    if ($wp->query_vars['template']==’basic’)

    It says there is an ampersand in the wrong place.

    I tried > instead but that also didn’t work.

    Any ideas?

    thanks

  3. Toine says:

    Hi, thanks for this great post, I was looking for this!
    So if I understand correctly: you could add this parameter to the post permalink in your page-template to get a different single-post template something like this:

    href="/?template=basic"

    If I wanted to rewrite the url with parameter to a different domain, would my .htaccess file look something like this?

    RewriteCond %{HTTP_HOST} =www.originaldomain.com [NC]
    RewriteCond %{QUERY_STRING} (template=basic)
    RewriteRule ^$ http://www.newdomain.com/ [R=301,L]

    Thanks for your help!
    Cheers

  4. Toine says:

    It removed ‘the_permalink()’ in the href by the way…

    One other thing; how could this trick work if I allready have multiple single template files for multiple custom post types? Then the following line wouldn’t work:

    return dirname( __FILE__ ) . '/single-basic.php';

    Somehow I need to insert the ‘basic’ variable in the filename rather then renaming the whole filename. Any ideas?
    Thanks!

  5. Toine says:

    Sorry to bug you again, found a solution that I wanted to share with you!


    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('single_template', 'sjc_template');

    Works for me!

  6. Scott says:

    Hi there,

    I get an error when i paste the code in my functions file:

    function sjc_template($template) {
    global $wp;
    if ($wp->query_vars['template']==’basic’) {
    return dirname( __FILE__ ) . ‘/single-basic.php’;
    }
    else {
    return $template;
    }
    }
    add_filter(‘single_template’, ‘sjc_template’);

    Any ideas?

    Thanks,

  7. Scott says:

    I have the same issue as plainflour regarding my previous comment.

    if ($wp->query_vars['template']==’basic’)

  8. Hi Scott,

    I think there is something wrong with the code and that is should be:

    if ($wp->query_vars['template']=='basic')

    For some reason the > has been converted into special characters (and also you seem to have curly quotes around basic).

    Anyway, please try that and let me know if it doesn’t help.

    Cheers,
    Stephen

  9. Scott says:

    Hi Stephen,

    Thanks for clarifying that for me. As you can see even in the last comment, it converted the > (accent sign) into > – just need to make sure this is changed to the >

    Thanks very much for getting back to me mate. I’ll can now move on and see if it works.

  10. Scott says:

    Just to let you know, that’s working like a treat now! Great article.

Leave a Reply

Anti-Spam Quiz:


Stephen Cronin

is Manager of Online Service Delivery at a Queensland Government department & has been a freelance WordPress developer/consultant since 2007
*Content on this site is my own and is not related to my employer

Hire me - I'm expensive, but I'm very good!

WordPress Plugins

Want a Custom WordPress plugin? See my Services page.

Greasemonkey Scripts

Visit my home page at Userscripts.org.