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 post featuring a product that I've created using advanced custom fields. I would like to have a link on the post that opens in a new window and displays the same product, just with a different look and info. (Note: I'm specifically looking to use a second template and not a different CSS because different info will be displayed on each template.) I've tried using the code below, but my custom template just returns a blank page. Can anyone suggest a fix?

Here's the link I'm using on the original template.

<a href="<?php the_permalink(); ?>?custom-template=1">Link</a>

Here's the code that I'm putting at the top of the custom template.

if ($_GET['custom-template'] == 1) { include custom-template.php; return; }

Thanks, John

share|improve this question

1 Answer 1

up vote 0 down vote accepted

The way I see it you have two options here:

  1. Pass the id to a custom template and then create a custom wp_query loop and use $_GET['id'] as the id of the loop.

For Example:

<?php
/*
Template Name: Pop Up View
*/
//get id from your url 
$id=$_GET['id'];

//start custom loop

$query = new WP_Query( 'p=$id' );
//loop stuff and page template here
?>

then you would call <a href="custom-item-template?id=<?php the_ID();?>">Link</a>

Or going more with your current code you could do something like

    if ($_GET['custom-template'] == 1) { 
       get_template_part( 'custom-template');
      die(); //or exit(); 
}

This fixed a few problems I see with your current code. One being that return can't be used outside of functions. Also uses get_template_part instead of include with is a more WordPress way to do it.

Both ways should work, hopefully that gets you on the right track.

share|improve this answer
    
Thanks so much. I went with the second option and it works perfectly. –  John Dec 16 '12 at 23:36

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.