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.

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!

share|improve this question
add comment

2 Answers

up vote 6 down vote accepted

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

share|improve this answer
    
This is great!!! Couple of things tho... First there's a typo, you spelled /my_print_template with an H in the function (/my_print_themplate). No biggie, I found that easy. Now, the only issue I'm having with this is the print function pulls several posts into the page. I tried to limit it to by using posts_per_page to just 1 but it wasn't the correct post. Seemed like it was the most recent post posted. How do I pass the correct post ID variable so it is reflected in the print template? THANKS!!! –  RodeoRamsey Feb 18 '11 at 19:32
    
Hello Bainternet, very good tutorial! Is it possible use more templates? I mean my_print_themplate.php, my_print_themplate2.php, my_print_themplate3.php. I need 5 version printable for my posts. thanks for your help! –  user4894 Apr 26 '11 at 7:35
    
Hi @Vincent and welcome to the WPSE community. The answer box you've just used is intended for answers only - this is an important aspect of this site's usability. Your post is more like a comment or a question thus it may be considered inappropriate here. Please help the site moderators and delete it from here yourself. Thanks. (You need at least 50 reputation to be able to post comments everywhere) See: FAQ: Why are some questions or answers removed? and FAQ: Reputation –  Michal Mau Apr 26 '11 at 12:05
add comment

I just slightly modified @Bainternet's answer.

Using a switch on post_type, it's even possible to redirect to different templates. The default will let wordpress ignore the my_print parameter and go on as usual.

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"]))
    {
        switch ($wp_query->post->post_type) {
        case "page" :
            include(TEMPLATEPATH . '/my_print_page_themplate.php');
            die();
        case "portfolio" :
            include(TEMPLATEPATH . '/my_print_portfolio_themplate.php');
            die();
        case "post" :
            include(TEMPLATEPATH . '/my_print_post_themplate.php');
            die();
        default:
            // load as usual
        }
    }
}
share|improve this answer
    
die() seems a bit harsh. break perhaps? –  GhostToast Jan 22 at 17:35
    
@GhostToast if you don't call die() after your custom /my_print_* template, wordpress will follow its initial route. And you'll end up with the custom template and the default template concatenated. –  Daishi Kaszer Mar 26 at 2:55
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.