Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 569765
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T13:19:49+00:00 2026-05-13T13:19:49+00:00

I’m trying to add stylesheets conditionally to my drupal 6 site. I’ve added the

  • 0

I’m trying to add stylesheets conditionally to my drupal 6 site. I’ve added the following code to my template.php file to test it, and it’s not working. The css file does not get added to my site.

function ben_preprocess_page(&$vars) {
drupal_add_css('/sites/all/themes/ben/advice.css','theme','all',FALSE);}

This code works fine when I put it in a custom module I made using hook_init() but I think I need to put it in my template.php file as I want to check the page I’m on.

Any help would be much appreciated.

Ben

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T13:19:49+00:00Added an answer on May 13, 2026 at 1:19 pm

    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/services.css');
          break;
        // TODO: Add other variations ...
        default:
          // Might add a default alternative here
          break;
      }
    }
    

    (NOTE: untested code, beware of typos)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm trying to create an if statement in PHP that prevents a single post
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.