Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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

domain.com/page/?view=list

share|improve this question
1  
Hi Andrew, take a look at this site. If this what you want to achieve then follow the tutorial in my answer. –  Azizur Jun 26 '11 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 Jun 27 '11 at 3:37
add comment

4 Answers 4

up vote 2 down vote accepted

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; ?>
share|improve this answer
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) –  onetrickpony Jun 27 '11 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 Jun 27 '11 at 9:20
add comment

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

share|improve this answer
    
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 Jun 24 '11 at 6:30
    
If you're still looking for a solution. Take a look at this. dynamicdrive.com/dynamicindex17/ajaxcontent.htm –  user6428 Jun 26 '11 at 19:03
    
jquery slider or tabs does not help here. Andrew is looking into loading complete different layout if I understood the question. –  Azizur Jun 26 '11 at 22:18
    
That's correct, looking to load a completely different page template if this is possible. –  Andrew Jun 27 '11 at 2:02
add comment

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;
}
</script>

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>
share|improve this answer
    
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 Jun 24 '11 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 Jun 24 '11 at 11:27
add comment

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

share|improve this answer
add comment

Your Answer

 
discard

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.