Hi,

I am trying to find a conditional statement or some other method to change the css based on what content type is shown. Does anyone know how to do this?

Comments

jaypan’s picture

Try adding it in hook_node_load(), checking the type of the node (the nodes are will come in the $nodes array).


Contact me to contract me for D7 -> D10/11 migrations.
Or anything really. I'm friendly, and open to work or conversation.
peniel1127’s picture

/**
 * add in file: in template.php 
 */
function THEME_preprocess_node(&$variables) {
  $node = $variables['node'];
  if($node->type == $node_type) {
    drupal_add_css(path_to_theme(). "/css/pages-view.css", "theme");
    $variables['styles'] = drupal_get_css();
  }
}
mauhg’s picture

Nice! it work for me very well!!! until i saw the 404 page and the user page
don't use the preprocess_node function, some ideas?

shadcn’s picture

404 and user pages do not use preprocess_node because they are ... not nodes :)

You're looking for template_preprocess_page.

mauhg’s picture

so i can use this conditionals in the preprocess of page instead of the node preprocess?
but how i know the type of "page" that they are?

shadcn’s picture

Install the devel module and then

function YOURTHEME_preprocess_page(&$vars, $hook) {
  dsm($vars); //this holds values you can use for logic
}

Flush the cache. You will now see variables you can use on the page. http://awesomescreenshot.com/0d0d3j59f

Let me know if this helps.

mauhg’s picture

nice thank you, but i still have the problem that all the pages are of type pages, and i need to add
a especific css to the diferents types of content! but i feel i'm a litle closer to get it!

shadcn’s picture

This will add a unique class to each page :

function YOURTHEME_preprocess_page(&$vars, $hook) {
    // Add unique class for each page.
    $path = drupal_get_path_alias($_GET['q']);
    $vars['page_classes'][] = drupal_html_class('page-' . $path);
}

and this for node types :

function YOURTHEME_preprocess_node(&$vars, $hook) {
    $vars['page_classes'][] = drupal_html_class('node-type-' . $vars['type']);
}

then in template.php

<body class="<?php print implode(' ', $page_classes); ?>" >
mauhg’s picture

i'm going to do it and see what happen!
thank's!!!

gmcp’s picture

Where do I paste the functions?
thx