0

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?

1
  • Why don't you use Pages.css file from css folder. It would be easier.
    – Addiss
    Nov 27 '13 at 11:21
1

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");
        }
    }

    /**
     * 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
1

I think it only globally, you should do it the the preprocess page instead.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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