Changing WordPress single.php based on parent/root page

im trying to find a way how to change the single.php template based on the page im coming from.

Lets say i have a single wordpress install with a landing page “Root“, that leads to two different sub pages, i’ll call them “Red” and “Blue“. Both have several child pages and are basically two separate websites.

Read More

Both have the same structure but different css and different navigations.
By checking for the parent/grandparent on each of the child pages i am able to assign the right css and navigation on page load.

Up to this point everything is fine and dandy, but how do i handle this when opening a single post.

Red” and “Blue” are both listing the last 4 posts (both sites share the same news, so no changing by category), but when i click on them wordpress automatically loads the single.php and the default css and navigation of the “Root” page.

I’m looking for a way to be able to check from what page im coming, so im able to assign the right navigation and css. Same problem with the Blog Posts index page, i solved this one by creating two custom blog pages and made one of them the child of “Red” and one of “Blue“.

Simplest solution i can come up with, is to use $_Session, but on the one hand its not bulletproof and on the other the site will be cached and so it wont work anymore.

If anyone has an idea, i’d be grateful.

Related posts

Leave a Reply

1 comment

  1. I think that the easiest way will be forwarding an extra parameter to the request and based on that param, add a class to the html’s body (for example: ).
    The param could be forwarded as a simple $_GET param or save it as a $_session param. In both ways you can keep the param and keep forwarding it to the next pages you visit so your users will have smooth user experience.

    Using $_GET param will make sure you can use any caching system and nothing will break. If you can skip the html’s caching, just go with a simple $_session.