Quantcast
Channel: Joomla! Forum - community, help and support
Viewing all articles
Browse latest Browse all 1854

General Questions/New to Joomla! 4.x • Adding body class to template

$
0
0
I used to be able to modify any J3 template with this code to add a body class to the template:

Code:

First add this code above the doctype:<?php  $app = JFactory::getApplication();  $menu = $app->getMenu()->getActive();  $pageclass = '';  if (is_object($menu))    $pageclass = $menu->params->get('pageclass_sfx');?>(Many templates have a place to add head code via the Joomla admin)And this code to your body:<body class="<?php echo $pageclass ? htmlspecialchars($pageclass) : 'default'; ?>">Anything without a page class will use a class of "default", anything with the page class defined will appear as the class entered into “Page Class” in the Menu Manager.
Having a unique body class on each page is extremely useful. I've done some searching but come up dry with a solution for J4/5. Using the above code in J4 throws this error:

Code:

0 - Cannot access protected property Joomla\CMS\Menu\MenuItem::$params
screen-grab- 2024-05-20 at 12.51.48 PM.jpg
Anyone got a fix for this? Sorry for my lack of knowledge of PHP and Javascript, I'm just a lowly html/css geek! ;-)

Statistics: Posted by vincebodie — Mon May 20, 2024 7:55 pm



Viewing all articles
Browse latest Browse all 1854

Trending Articles