简体   繁体   中英

override joomla template.php body background color (site background)

I am using joomla 2.5 with an artisteer template, I have written a custom component which displays and sorts a list of products, the products themselves are also a part of the component and are saved in a database. what I am trying to do is set a different body background for specific products or product categories according to an if condition

if (product_category == 1):
   <body id="background1">
   ...

but the problem is that the <body id="someID"> is loaded in the template.php before the component is loaded, so even if I set a body background in the component, the background will not change. however, if I delete the body css id decleration from the template.php and leave it as <body> instead of <body id="someID"> , then the component <body id...> declaration sets the background correctly, but then any other page which is not related to the component has a blank white background.

what I think I need to do is find a way to set a default background in case a background is not defined elsewhere, or somehow override the current background and make to component load the body background again, I am relativly new to php and css but with a little help and I guidence I think I could find my way :) any help would be very much appreciated!!! thanks!

Try doing something like this:

<?php
$doc = JFactory::getDocument();
  if(product_category == 1){
     $css = 'body {'
           .'background: url(' . JURI::root() . 'components/com_yourcomponent/file.jpg) !important;'
           .'}';
     $doc->addScriptDeclaration($css);
  }    
?>

You will need to change the path of the background to whatever suit your needs.

Hope this helps

In template.php file, before body is loaded, you can do like this

if (JRequest::getCmd('option') == 'com_mycontent'){
    echo '<body id="myid">';
}
else
    echo '<body id="anotherid">';

Hi @marko and @Lodder thank you so much for your reply! Unfortunately the solutions you offered did not work because the stylesheet is loaded prior to the component so any new design changes from the templae code will not affect the template design. and unforunately since the template.php cannot initialize the component veriables (Category_id for example) I cannot create a condition and check what is the category_Id and change the background accordingly... I found a simple solution which is implementing this code inside my component code:

app =& JFactory::getApplication();

if ($category_id == 1) $app->setTemplate("template2");
else ....

$this simply sets a new template according to the category_id, I know this is not memory efficient and the loading time is longer and the worst part is that I need to create a new template for each category... but this does the trick... I'd appreciate if you could think of any better solution?

Thank you so much guys!

Actually, you could just use the built in page class suffix parameter in the menu item to accomplish what you want. If you have a custom template, add this to it:

<?php
$app = JFactory::getApplication('site');
$params =  & $app->getParams('com_content');
$pageclass = trim($params->get('pageclass_sfx'));
?>
<body id="<?php echo $pageclass ? $pageclass : 'default'; ?>">

Most of the reputable template devs include code for the page class suffix, so you may not need to do anything other than add something in the parameter of the page you want to customize.

By adding an ID to the body tag, this insures that you can easily and accurate override any CSS on the page.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM