If I want to add a CSS file to certain pages only I know how to do this with the drupal_add_css function in the template.php file, but can you also do it with the theme.info file? Or is the theme.info only for adding CSS files globally?
2 Answers
Hope this will help you,
In Drupal 7 you can use template_preprocess_page or template_preprocess_node function for your requirement in template.php file as Namari suggested with drupal_add_css.
You cant use theme.info file to particular pages. If you add the CSS in theme.info file then the CSS will included for each page. As you want to include the CSS file for particular pages so better to use template_preprocess_node OR template_preprocess_page.
drupal_add_css : Adds a cascading stylesheet to the stylesheet queue.
/**
* Implement template_preprocess_page()
*
*/
function yourThemeName_preprocess_page(&$variables) {
if (arg(0) == 'node' && is_numeric(arg(1)) && arg(1) == 123) {//check your condition here
//you can also use $node = $variables['node']; $node is the node object where $node->nid is the node id
drupal_add_css(drupal_get_path('theme', 'theme_name') . "/cssfilename.css");
}
}
- You can also use template_preprocess_node
/**
* Implement template_preprocess_node()
*
*/
function yourThemeName_preprocess_node(&$vars) {
$node = $vars['node'];
if($vars['type'] == 'my-node-type' && $node->nid == 123 ) {// check your condition
drupal_add_css(drupal_get_path('theme', 'theme_name') . "/cssfilename.css");
}
}
- If you are checking for a node then better to use template_preprocess_node than template_preprocess_page