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.

I want to be able to render post in two different styles (2 templates).

For example, let's say I have the post ID 133, I would like two URLS to access it and so it renders where different template would apply.

  • lorem.com/render1/133
  • lorem.com/render2/1333

for example... or it could be something like:

  • lorem.com/post/133
  • and lorem.com/post/133?template=2

How would you do it?

share|improve this question
    
Just a heads up, a query var (your ?template=2) example might be safe, but be careful about duplicate content. If WordPress correctly adds the canonical address meta tag, I think you'll be safe, but it's better to avoid the issue than suddenly find your site tanked on Google overnight. –  mrwweb Jan 18 '13 at 16:52
add comment

2 Answers 2

up vote 0 down vote accepted

Your best bet would probably be the Rewrite Endpoints API. The API allows you to create post URLs with endpoints like lorem.com/post/133/json/ or lorem.com/post/133/print/. You'll find useful code examples in the link provided.

share|improve this answer
    
Clean. Perfect. –  flintsburg Jan 20 '13 at 12:10
add comment

If you add a query arg, via add_query_arg(), you could then query that query arg, via get_query_var(), inside of a callback hooked into template_redirect.

(Note: how/why you add the query arg is outside the scope of this answer.)

For example:

function wpse82168_template_redirect() {
    // You'll need to update these conditionals according to your context
    if ( is_single() && get_query_var( 'template' ) && '2' == get_query_var( 'template' ) ) {
        get_template_part( 'template-2' );
    }
}
add_action( 'template_redirect', 'wpse82168_template_redirect' );
share|improve this answer
    
Interesting technic :) Is there a particular explanation for you function name (wpse82168_template_redirect) or it's just random? –  flintsburg Jan 20 '13 at 12:03
    
All declared function names are, ultimately, arbitrary. But in this case, wpse82168 refers to this question, and template_redirect refers to what the function does. The former is a unique prefix that ensures the function is properly namespaced; the latter is inherent documentation, in which simply reading the function name helps explain the purpose of the function. –  Chip Bennett Jan 20 '13 at 13:49
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.