Load post with a different template?

you can do it like this:

    //add my_print to query vars
function add_print_query_vars($vars) {
    // add my_print to the valid list of variables
    $new_vars = array('my_print');
    $vars = $new_vars + $vars;
    return $vars;
}

add_filter('query_vars', 'add_print_query_vars');

then add a template redirect based on that query_var:

add_action("template_redirect", 'my_template_redirect_2322');

// Template selection
function my_template_redirect_2322()
{
    global $wp;
    global $wp_query;
    if (isset($wp->query_vars["my_print"]))
    {
        include(TEMPLATEPATH . '/my_print_themplate.php');
        die();

    }
}

create a new file in your theme directory named “my_print_themplate.php”
an paste this code there.

<?php
    define('WP_USE_THEMES', false);
    echo "<h1>printer friendly version:</h1>\n";
    query_posts('p='.$_GET['pid']);
    if (have_posts()){
        while ( have_posts() ) { the_post();
            the_content();
        }
    }else{
    echo 'nothing found';
    }
?>

and now all you have to do is create a link with ?my_print=$post_id in your regular single loop.

hope this helps

Leave a Comment