Load post with a different template?

Let’s say I have a single.php file with a certain layout (graphic intensive). I want to create a sort of plain-text version of the same page that is only called when the user clicks the provided link. I can create single-plaintxt.php but how would I go about generating a link and/or function that would only load the page contents using that file when clicked?

Thanks!

3 Answers
3

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