WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] redirect to other "view" of same post (3 posts)

  1. Umbercode
    Member
    Posted 1 year ago #

    Suppose a single post (single.php) has an URL example.com/post-x/
    On this page there is a link which is example.com/post-x/print/
    In other words, the same URL but with an added /print/ slug.
    When I click on that link I want to show the same post, but with a different layout (coupon).

    I suppose I need to use template_redirect so what I have so far is this:

    add_action("template_redirect",  "couponRedirect");
    
    function couponRedirect() {
    	global $wp_query;
    	if ('print' == $wp_query->query_vars['name']) {
          if ($wp_query->is_404) {
             $wp_query->is_404 = false;
          }
          header("HTTP/1.1 200 OK");
          include (TEMPLATEPATH . '/coupon.php');
          exit;
       }
    }

    But this does not display anything (empty page). Now I am guessing I need to include a query? Can someone point me in the right direction as to how to do this?

  2. bcworkz
    Member
    Posted 1 year ago #

    You might look into the Rewrite API. You basically want to send the query variables of any URL with '/print' to your coupon.php template. Your template then uses these variables to properly reconstruct the original post in a manner suitable for your needs.

  3. Umbercode
    Member
    Posted 1 year ago #

    Thanks, I was unaware of this possibility.

Topic Closed

This topic has been closed to new replies.

About this Topic