IIRC, the stylesheet links have already been rendered when the *_preprocess_page()
functions get called, and the generated markup got placed within $variables['styles']
. So it is to late to use drupal_add_css()
by then.
You could either assemble the <link ...>
markup for your additions yourself and append it to $variables['styles']
, or you need to find a better place for your call to drupal_add_css()
earlier in the processing chain (probably from within a module).
What place that would be is hard to tell without knowing what you mean exactly by 'check the page I'm on', but if we are talking node pages, hook_nodeapi()
would be a candidate.
EDIT after clarification in comments:
If the decision on what stylesheets to add is based on the path alone, hook_init
(in a custom module) would be a proper place to do it, as the path will not change after that. The only 'tricky' bit in this case would be to get at the clean URLs. If (as I assume) you use clean URLs, you can not use arg(0)
to get the first element of the path, as it would return the first element of the Drupal internal path (e.g. 'node' for node pages). So you have to get the clean URL version first:
// Get current path alias, if any (will return original path, if no alias set)
$path = drupal_get_path_alias($_GET['q']);
// Extract first element
$path_elements = explode('/', $path);
// Do we have at least one element?
if (0 < count($path_elements) {
// Yes, add stylesheet based on that
switch ($path_elements[0]) {
case 'advice':
drupal_add_css('path/to/advice.css');
break;
case 'services':
drupal_add_css('path/to/advice.css');
break;
// TODO: Add other variations ...
default:
// Might add a default alternative here
break;
}
}
(NOTE: untested code, beware of typos)