Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Hi,
I dont want to change the entire theme. I just want a different stylesheet for different sections.
Is that possible?
Comments
Comment #1
hass CreditAttribution: hass commentedYes, but not with sections module.
Comment #2
jbernal.web.dev CreditAttribution: jbernal.web.dev commentedHi pareen -
I'm new to Sections and have only gotten as far as browsing through the Sections module code in
sections.module
. But I know you don't need Sections to accomplish what you want to do (so my explanation here doesn't involve Sections.) There is a built-in Drupal function to add a CSS file to a theme calleddrupal_add_css()
. When combined with path checking, you can invoke your CSS file for only certain paths. Both path checking anddrupal_add_css()
can be combined in your theme's page preprocessor, oryourthemename_preprocess_page()
to produce the kind of behavior you're looking for.Remember that if your theme name is flowers then the preprocess function would be named
flowers_preprocess_page(&$vars)
You can include preprocess functions in your theme'stemplate.php
file. If your theme doesn't have atemplate.php
file you can simply create the file in your theme's directory.So you would have:
Notice that $vars['styles'] is available to your theme's page.tpl.php and other page template files as simply
$styles
.Comment #3
hass CreditAttribution: hass commentedSend me an email if you need some paid consulting, please.
Comment #4
pareen CreditAttribution: pareen commentedThanks Jose.
It works!
However, I was looking out for some way I could accomplish this without getting into php.
Is there any module that adds css classes based on url/path?
@hass: thanks