Monday, 6 January 2014

Joomla changing background image depending on menu item

So I want to change the background picture depending upon the page you're on. When I first looked at the problem I thought it'd be easiest to create a Page Class on the menu item, and then within the content area, make an item which appears behind everything (z-depth) and fills the whole screen. The proved quite difficult.

Turns out there is a much easier way, still using the Page Class on each menu item. Found the answer here in a forum, and here in a Joomla Help Page.

Basically, you can use PHP to find the page that is being displayed, and put the class suffix anywhere in the document, i.e. in the
  1. put this code inside the head tag:
    1. <?php $app = JFactory::getApplication(); $menu = $app->getMenu()->getActive(); $pageclass = '';   if (is_object($menu)) $pageclass = $menu->params->get('pageclass_sfx'); ?>
  2. Put this in the tag you want a class to. I.e. the body tag:
    1. <body id="<?php echo $pageclass ? htmlspecialchars($pageclass) : 'default'; ?>">
You can also add another stylesheet to keep the css even more streamlined. Sorted! (oh, by the way, I havent tested any of this!!!!)


Well, after leaving it a while I came up with a problem. The specific page I want to change is a "featured-blog" menu item, so when I put the Page Class, I have to put a space to separate it from the word 'featured-blog'.

But alas, the pageclass picks this up! I had to get a php line in which trimmed the spaces from the pageclass:

$pageclass = trim($pageclass);

No comments:

Post a Comment