0

Using two permalinks for one post

I am developing a wordpress website for a client, the client want to show 2 separate templaes for post details the scenario will be.

user clicks on post and our default single.php will load up, there have a play button when user click on play button then instead of post name in permalink I want to add /play/post-name and need to load second template for that.

searched on internet and found some suggestions like Maintaining two permalink structures and https://wordpress.org/support/topic/multiple-posts-in-1-permalink but these are not working and also these are trying to add at the end of the link but i need it to before post-name

any help will be appreciated.

thanks

Actually this is a game website and from posts listing page when user click on game image he will see a single page with game description and a play button when user click on play button he can then move to the actual game, so the post is same but we need seperate link for details and play pages.

thanks

3
  • 1
    Is the content different for both URLs? If the content is the same you should avoid differents URLs for SEO reasons. In the other hand, if the content is different, your concept about two permalinks for one post is wrong because it is not one post. Please, edit the question and add more information.
    – cybmeta
    Dec 29, 2014 at 21:40
  • What you described is two pages , one with details and one with game . if the page and the game in the same page , you can add hashtag marker , like this for example , example.com/game1#play ,to play . example already used is in comments , when you click on the view comments its go straight to comments ,adding further html and jequry you can hide the details when #play is fired. Dec 30, 2014 at 15:43
  • No I just want to wordpress url rewriting so I can change the template file according to the url Dec 30, 2014 at 15:48

1 Answer 1

0

I'll preface this by saying I think it makes much more sense to have play appended to the end of the permalink, which you could easily achieve with add_rewrite_endpoint, but in the interest of answering the question as it was asked...

First you'll need to add a query var that you'll later check to know when to load your other template:

function wpd_query_var( $query_vars ){
    $query_vars[] = 'is_play';
    return $query_vars;
}
add_filter('query_vars', 'wpd_query_var');

Next you'll need a rewrite rule to handle incoming requests and set the proper query for your single post, and also set the above query var flag to load your template later.

function wpd_post_rewrite(){
    add_rewrite_rule(
        'play/([^/]+)/?$',
        'index.php?name=$matches[1]&is_play=1',
        'top'
    );
}
add_action( 'init', 'wpd_post_rewrite' );

Lastly, the filter to check if the query var is set and load the other template in that case:

function wpd_play_template( $single_template ){
    global $wp_query;
    if ( isset( $wp_query->query_vars['is_play'] ) ) {
        $single_template = locate_template( 'play_template.php', false );
    }
    return $single_template;
}
add_filter( 'single_template', 'wpd_play_template' );

Remember to flush your rewrite rules after changing them so the new rule gets picked up.

2
  • Hi, i can check that in a moment and get back to you. thanks Dec 31, 2014 at 13:00
  • Implemented and it worked :) Dec 31, 2014 at 15:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.