• Florent

    (@florentjouetcarredelunecom)


    Hi,

    I want to be able to display one page into different template. This is because I want to simulate multi publication for a post (or a page), so I want to put the page in a different environnement depends on the menu in use.

    Is there a way to achieve this ?

    I thought :
    – use a specific url to force usage of a template : for exemple mytemplate1.php?postID=XX. IS THIS POSSIBLE ? how to do that ? which kind of url ?
    – use an url parameter to detect which template to use

    Those assumptions not seems very clean and not using the WordPress power.
    Is there anyone with a great solution ?

    Regards.
    Florent

Viewing 6 replies - 1 through 6 (of 6 total)
  • Have you reviewed custom page templates?

    Thread Starter Florent

    (@florentjouetcarredelunecom)

    Yes,

    This is not the solution because you could have only one page template per page, and I need to know from which menu the page demande comes from to adapt my template.

    I don’t know what you mean by ‘depending on the menu in use’.

    Do you mean your using wp_nav_menu, and that a particular page might be added to multiple menus – and you need to detect which menu a user clicked on to arrive at the page?

    If thats what you mean, I’m not sure how you might achieve that, unless you filtered the permalinks being generated in that menu and added a query parameter like you said.

    However, if you don’t mean what I described above, you might mean that you have several sites where you’re using the same theme, and want to use different templates depending on which menu (of a pre-determined set) has been assigned to a particular location. If thats what you mean, then you could just use has_nav_menu() to check each location.

    I’m going the guess that second version, since it seems to make the most sense.

    [Code moderated as per the Forum Rules. The maximum number of lines of code that you can post in these forums is ten lines. Please use the pastebin]

    You also asked about using a query string to determine the template. You could so that with something like this, although I wouldn’t really recommend this.

    function filter_page_template($template){
    
            /* Lets see if 'template is set' */
            if( isset($_GET['template']) ) {
    
                /* If so, lets try to find the custom template passed as in the query string. */
                $custom_template = locate_template( $_GET['template'] . '.php');
    
                /* If the custom template was not found, keep the original template. */
                $template = ( !empty($custom_template) ) ?  $custom_template : $template;
            }
    
            return $template;
    }
    add_filter('page_template', 'filter_page_template');
    Thread Starter Florent

    (@florentjouetcarredelunecom)

    Thanks,

    what I mean is using wp_nav_menu, and that a particular page might be added to multiple menus with multiple different views of that article.

    I will try your querystring example. Why did you wrote “although I wouldn’t really recommend this.” ? Is this dangerous ?

    Thanks a lot

    Regards

    Thread Starter Florent

    (@florentjouetcarredelunecom)

    One more thig,

    Is there an information when showing a page, telling to which menu this page is owning ? A specific query that could retrieve this information ?

    Thanks.

    I don’t recommend that because it would allow anyone to simple type in any /<filename> and run any file in your theme folder. It could be a security problem.

    Not sure how you would be able to specify which menu is being clicked – you may need to add a query string param to each menu item. Which would probably mean you need to write your own nav item walker, and depending on how you’de want to do the query string var, you may need to add a custom rewrite rule to capture that variable.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Different Specific Template for the same page’ is closed to new replies.