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


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:

Template Name: Pop Up View
//get id from your url 

//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.

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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