1

How can i write css for content of the dynamic url page like 'http://mysitename/user/23/prayerrequests'. Here 23, assume user id. I want to effect conent div css of this dynamic url's.

2
  • What do you mean URL specific ? Do you want the CSS to change for every user ?
    – GoodSp33d
    Jul 17, 2012 at 6:48
  • yaa kantu, dynamically user number changes...like user/2/prayerrquest, user/3/prayerrequests etc..like that
    – Srinu
    Jul 17, 2012 at 6:58

3 Answers 3

5

you can use the following, it implements drupal_add_css().

if($_GET['q']=="user/23/prayerrequests") {
  drupal_add_css(path_to_theme() . '/custom_stylesheet.css', 'theme', 'all');
}

Edit 1:
If you want this to be able to do it for all users, you could use me aliases module. It enables you to use me in place of %uid.
So, the code would become

// "me" from the code is automatically replaced by uid of current logged in user.
if($_GET['q']=="user/me/prayerrequests") {
  drupal_add_css(path_to_theme() . '/custom_stylesheet.css', 'theme', 'all');
}

Edit 2: You can use the variable global $user in the above code. You'll not need to install me aliases module.

//replace THEME with your theme name
function THEME_preprocess_page(&$variables) {
  global $user;
  if($user->uid == 0) {
    return;
  }
  elseif ($_GET['q']=="user/".$user->uid."/prayerrequests") {
    drupal_add_css(path_to_theme() . '/custom_stylesheet.css', 'theme', 'all');
}

Don't forget to clear the caches.

10
  • Indrock, Can i use '%' in the place 23 for dynamic change and where this line has to write..in page.tpl?
    – Srinu
    Jul 17, 2012 at 6:58
  • I'm not so sure. Look for the edit, I've suggested a work around for the same.
    – AjitS
    Jul 17, 2012 at 7:04
  • 2
    put above code in THEME_preprocess_page(&$variables) located in your theme template.php file, don't forget to change THEME with your actual theme_name
    – svetlio
    Jul 17, 2012 at 7:18
  • Is there any other option apart from 'me' ..placeholder like * or % etc...........
    – Srinu
    Jul 17, 2012 at 9:51
  • 1
    Great Indrockk...You are Rock............
    – Srinu
    Jul 17, 2012 at 10:24
1

If you want to load a different stylesheet for every user, then this would work:

function MYTHEME_preprocess_page($variables) {
  if (arg(0) == 'user' && is_int(arg(1)) && (empty(arg(2)) || arg(2) == 'view') {
    drupal_add_css(path_to_theme() . '/user-' . arg(1) . '.css';
  }
}

However, if what you're looking for is a dynamic stylesheet, I'd recommend against that — but it can be achieved with Less or Sassy.

4
  • 1
    This will help to use different CSS for different users of the site. But, I think OP is looking for a solution to add the same CSS to a specific page only, for all users.
    – AjitS
    Jul 17, 2012 at 10:14
  • Then drupal_add_css is the solution, anyway. But if OP wants to add a custom css for all pages, based on logged in user, just use global $user and get the $user->id. Beware that $user->id = 0 for anonymous users. Jul 17, 2012 at 12:54
  • yes, I have that implemented in my answer.
    – AjitS
    Jul 17, 2012 at 13:06
  • Why don't you just use body classes? It's far easier and better to theme pages when you include the current path and all arguments there. Jul 17, 2012 at 15:03
1

If your not looking to write your own module, you can also look into something like Themekey:

ThemeKey allows you to define simple or sophisticated theme-switching rules which allow automatic selection of a theme depending on current path, taxonomy terms, language, node-type, and many, many other properties.

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.