Heather
Heather

Reputation: 305

Add page class suffix based on active item in Joomla 3.1.1

I want to add a page class to my body tag (or a div) based on the page I am viewing. I found a tutorial but it's not working 100%, it adds id="default" to every page. I need each page to generate a special class or id.

This is what I have so far, plugged into index.php of my template:

<?php
  $itemid = JRequest::getVar('Itemid');
  $menu = &JSite::getMenu();
  $active = $menu->getItem($itemid);
  $params = $menu->getParams( $active->id );
  $pageclass = $params->get( 'pageclass_sfx' );
?>
</head>
<body id="<?php echo $pageclass ? htmlspecialchars($pageclass) : 'default'; ?>">

PS. My search engine friendly URLs aren't working so I had to turn them off.

Upvotes: 0

Views: 4434

Answers (2)

user1046503
user1046503

Reputation: 343

This example is code for Joomla 2.5.x and higher:

<?php
  $app = JFactory::getApplication();
  $menu = $app->getMenu()->getActive();
  $pageclass = ''; // Notice how the variable is empty first. Then place the code below.

  if (is_object($menu))
    $pageclass = $menu->params->get('pageclass_sfx');
?>

Ref: http://docs.joomla.org/Using_the_Page_Class_Suffix_in_Template_Code

Upvotes: 1

Joe Conlin
Joe Conlin

Reputation: 5994

Here is how I add page classes to all of my pages:

In the beginning of my index.php file, I add the following php:

if (is_object($menu))
    $pageclass = $menu->params->get('pageclass_sfx');

Then, I use the following for the new opening body tag:

<body class="<?php echo $pageclass ? htmlspecialchars($pageclass) : 'default'; ?>">

This sets the body class to "default" by default if a Page Class is not set (see below).

Once the above is done, changing the body class is easy. In the Joomla admin, open the menu item you want to set the page class on, click on Advanced Options and scroll down/expand Page Display Options. Enter the class name in Page Class and that's it!

Upvotes: 0

Related Questions