5

I'd like to include a particular CSS file only when the front page is being viewed, since it only applies to the front page, and it's a relatively large file. I know that the solution will involve using drupal_add_css(), but I'm not sure where in my theme's template.php file this would go. Is there a particular hook within my theme that I should be implementing to use this? Thanks.

EDIT: I essentially have the code down, I'm just not sure what hook is available to my theme files to implement. The actual code will look something like...

if (!strlen(request_path())) {
    drupal_add_css(drupal_get_path('theme','mytheme'). '/css/front.css');
}

2 Answers 2

10

Try this:

function mytheme_preprocess_page (&$variables)
{
  if (drupal_is_front_page()) {
    drupal_add_css(drupal_get_path('theme','mytheme'). '/css/front.css');
  } 
}

See template_preprocess_page and drupal_is_front_page for more info.

0

The CSS Injector module allows you to do just that.

2
  • 1
    While this would work, and I certainly appreciate the suggestion, this should be like 5 lines of code in a theme. The only unknown is what hook to use in my template.php file. I'd rather not go the route of enabling a full module for a small, single circumstance if I can avoid it.
    – rybosome
    Commented Jan 3, 2012 at 20:43
  • I agree, I use the same "keep it simple" approach ;-)
    – uwe
    Commented Jan 4, 2012 at 2:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.