1

I'm trying to add a CSS class to some certain pages for some certain menu styling depending on the current alias path. It works well with the following function but I'm wondering is there another way to split the current alias parts so I can use them as arguments? Here's my present function:

function mytheme_preprocess_html(&$vars) {

  $path = drupal_get_path_alias($_GET['q']);
  $aliases = explode('/', $path);

  if (isset($aliases[2]) && $aliases[2] === 'foo') {
    $vars['classes_array'][] = 'bar';
  }
}
0

2 Answers 2

3

You should be using Drupal's arg function: https://api.drupal.org/api/drupal/includes%21bootstrap.inc/function/arg/7

Something like this should work:

function mytheme_preprocess_html(&$vars) {

    if (arg(2, drupal_get_path_alias()) == 'foo') {
        $vars['classes_array'][] = drupal_clean_css_identifier(arg(2, drupal_get_path_alias()));
    }
}

Added drupal_get_path_alias() as an arguement of arg() as suggested by @koivo

0
3

There was one answer from a guy which now is deleted suggesting to use arg(2) instead. I commented after trying it out that arg() doesn't seem to work with alias paths. Well, I now found out it does and that's how by passing the Drupal alias path into the arg() function:

if (arg(2, drupal_get_path_alias()) === 'foo') {
  $vars['classes_array'][] = 'bar';
}

Your Answer

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

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