I have a page that displays posts (4 on each row), with a thumbnail image & title for each. Is it possible to click a text/icon link and dynamically switch the page template? I'm looking to provide an alternate view of the posts, perhaps with larger images, more text/custom fields added etc.

I thought it would be easy just to link to another page template but it changes the url (would like the url to stay the same) and for some reason it loads the closest matching post, rather than the new page template (I have the page template nested within the first).

Something like this


  • 1
    Hi Andrew, take a look at this site. If this what you want to achieve then follow the tutorial in my answer. Commented Jun 26, 2011 at 22:40
  • @azizur That site you provided above looks like what I want to achieve. He is using query strings to modify the layout. The tutorial you provided is different however and just hides the content with css. How can I build something like resetstudio? any tutorials on query strings?
    – Andrew
    Commented Jun 27, 2011 at 3:37

4 Answers 4


I think I have a workable solution on what I was trying to do. This works but is there a better way? Also are there any implications I should be aware of when using $_GET? Didn't realize it would be this easy :) perhaps I'm missing something.

<?php $view = $_GET["view"]; ?>

Then if the URL is domain.com/page/?view=list I use an IF statement inside the loop to modify the markup as required:

<?php if ( $view == "list" ) : ?>

    // code here for list view

<?php else : ?>

    // code here for normal default view

<?php endif; ?>
  • 2
    nope, it's the best solution for what you're trying to achieve. Just remember to sanitize $_GET/$_POST variables if you're going to perform queries with them (it's not the case in the code above) Commented Jun 27, 2011 at 5:42
  • @One Trick Pony Good to know, although I have no idea what sanitizing the variables means. I'll look into it, sounds like fun :) If you have any good recommendations on tutorials please share, otherwise I'll hunt around, thanks!
    – Andrew
    Commented Jun 27, 2011 at 9:20

It can be achieved with jquery slider or tabs, instead of page template where URL will change. Just need to work on the styling.

  • I'm not sure I follow you. I am aware that I can use CSS to change the page layout but in this instance I need to add/change php, hence needing a new page template.
    – Andrew
    Commented Jun 24, 2011 at 6:30
  • If you're still looking for a solution. Take a look at this. dynamicdrive.com/dynamicindex17/ajaxcontent.htm
    – user6428
    Commented Jun 26, 2011 at 19:03
  • jquery slider or tabs does not help here. Andrew is looking into loading complete different layout if I understood the question. Commented Jun 26, 2011 at 22:18
  • That's correct, looking to load a completely different page template if this is possible.
    – Andrew
    Commented Jun 27, 2011 at 2:02

You can change the stylesheet of your current page with some Javascript.

In your header.php

<link rel="stylesheet" href="main.css" id="stylesheet">

<script type="text/javascript">
function switchStyleSheet( url ) {
    document.getElementById( 'stylesheet' ).href = url;

And in your template file :

<a href="#" onclick="switchStyleSheet('main.css'); return false;" >switch to main style</a> 

<a href="#" onclick="switchStyleSheet('special.css'); return false;" >switch to special style</a>
  • Thanks for your answer but I need to change the page template, not the CSS as I need to modify PHP code also. Any ideas?
    – Andrew
    Commented Jun 24, 2011 at 11:03
  • @Andrew, ok well if the diffrences are not that big between your two templates, like hide/show certain fields for example, you could go with css and one template only. If you want to include other stuff you could also pass parameters to the URL in your link and on page reload you'd filter those GET variables to show your extra code.
    – mike23
    Commented Jun 24, 2011 at 11:27

Have you followed the Create a Multi-Layout Portfolio with WordPress tutorial and seen a working demo.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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