Support » Themes and Templates » Using different page templates with the same page content

  • I’m doing a project where the same page content is going to be displayed to different audiences and need to be able to display the same content using different page templates. The reason for the different page templates is so I can include a different sidebar for each audience.

    Given the three audiences [Users, Developers, and Managers], I want to show a sidebars like so:

    For Users:
    – Foo
    – Bar

    For Developers
    – Bar
    – Bat
    – Baz

    For Managers
    – Foo
    – Baz
    – Bink

    I’ve setup three page templates based on my index.php template and included the appropriate sidebar templates. However, when I write a page, I have to associate it with a single page template.

    What I would like to do is create duplicate /index.php (not template index.php, but wp-root/index.php) files named /users.php, /developers.php and /managers.php and pass the appropriate template name to them. That way all the browsing of done while using that copy of index.php will being using the right page template.

    Is this possible?

Viewing 12 replies - 1 through 12 (of 12 total)
  • Certaintly but I don’t know more than that there is probably some sort of template_redirect hook to be utilized in a plugin. So given a user level, fetch the correponding template.

    Here goes in Codex; even has the example that you need. (scroll down till you see template_redirect)

    Hmm… So you’re saying I need to write a plugin to do this?

    That sample code is pretty obtuse. I get what it’s showing (how to use the hook in a really basic manner), but it doesn’t give me the bigger picture.

    The determination of user level will not be by any login criteria, but simply by which way they are looking at the site. The site includes a horizontal navbar under the banner that gives the options [User, Developer, Manager]. I want each of those to link to /user.php, /developer.php and /manager.php, respectively, so that each type of site viewing user can have the appropriate sidebar. Hence my desire to have multiple copies of the /index.php that will call the correct template.

    I hope that clarifies what I’m trying to do. Any additional pointers in the right direction would be much appreciated.

    1. Create 3 Page Templates: user.php, developer.php, and manager.php; each with the appropriate sidebar.

    2. Create 3 Pages: user, developer, and manager; assign the corresponding template.

    I can’t load codex, but the reference for Pages would be here likely

    OK. That works for the main page of each section.

    However, there are other pages (see OP above) that are common to all of those sections. When I’m viewing the ‘bar’ page via the User page, I want the user sidebar to show, but when I’m viewing it via the Developer page, I want the Developer sidebar to show. When viewing the Manager page, I want the Manager sidebar to show (which doesn’t include the ‘bar’ page.)

    Basically, what I want to be able to do is pass a parameter to tell WP which template to use. I’m just not sure where I could do this.

    Showing a different sidebar on a page is quite simple.
    1. (very rudimentar) = to have the sidebar in the Page template
    2. (more elegant) = put an if statement that would cause to have different sidebars for different pages > see this thread:

    I’d like to implement an elegant solution like that, but I don’t have a one to one relationship between pages and templates/sidebars. Currently, the only reliable way I can think of doing this is to embed a query string in the HREFs in each specific sidebar that would tell an if/then/else or case statement which sidebar to pass.

    Does WP eat all the query string name/value pairs, or can I pass a variable like ?template=users and then place the following code in my index.php template?

    if ($template = “user”) {
    require (‘user-sidebar.php’);
    elseif ($template = “dev”) {
    require (‘dev-sidebar.php’);
    } elseif ($template = “mgmt”) {
    require (‘mgmt-sidebar.php’);
    } else {

    Perhaps my php foo is weak. (Who am I kidding? I know it’s weak.) Can I get direct access to the query string as passed by the browser? If so, would someone be so kind as to remind me how to do this? It’s been years since I did any PHP coding.

    Thanks for the pointer in the right direciton, I’m pretty sure I just need one more hint and I should be good to go.

    … but I don’t have a one to one relationship between pages and templates/sidebars.
    I don’t get what you saying. You should be able to do what Moshu suggested.

    Let me see. So you want to have one page template with 3 variant of sidebars, correct?
    So you will have one Page Template with the if-else statement.
    Note, however, that you will still create 3 Pages from WP admin panel, yet all of them are assigned with the same Page Template.

    EDIT: Sorry, I get the context now, keep going.

    So, the code would be
    $template = $_GET[ 'template' ]

    That did the trick. Huge thanks to both Moshu and Alphaoide.
    If either of you ever find yourselves in Providence, RI, drop me a line and pints (or whatever your favorite poison is) will be on me.

    Here’s the conditional code for reference:

    $template = $_GET[‘template’];
    if ($template == “user”) {
    include(TEMPLATEPATH . ‘/user-sidebar.php’);
    } elseif ($template == “dev”) {
    include(TEMPLATEPATH . ‘/dev-sidebar.php’);
    } elseif ($template == “mgmt”) {
    include(TEMPLATEPATH . ‘/mgmt-sidebar.php’);
    } else {

Viewing 12 replies - 1 through 12 (of 12 total)
  • The topic ‘Using different page templates with the same page content’ is closed to new replies.