I have a view that sorts by a taxonomy term. After the view layout, i need to find a specific way to have that page that's clicked on to be styled with a different background. For instance, this view will show a background of a Football, but I also have a view for other sports. Is there a way to determine the page name? Right now, its just a node-page-profile. Here's the link.



Ok, you want to expose your taxonomy to your nodes to change the background.

Using the Context Module

Add a condition Taxonomy and select your term (football) and the reaction Theme HTML add the class (football). As far as I got here you will need one rule for each term, you can't use a token directly.

Using a custom module

You can do it with a custom module or just throw a preprocess_page it in your theme:

function my_theme_preprocess_page(&$vars, $hook) {
  if (module_exists('taxonomy') && $vars['node']->nid) {
    foreach (taxonomy_node_get_terms($vars['node']) as $term) {
      $vars['node_terms'] .= ' taxonomy-'.strtolower(eregi_replace('[^a-z0-9]', '-', $term->name));

Got from: https://www.drupal.org/node/67587

BTW you should really change the path for the nodes! I know it is still in dev but don't forget it!

| improve this answer | |
  • the page-profiles-football is the view page itself. I need to somehow add a style to the pages that are clicked on. – fitnessCraze Jul 28 '15 at 19:28
  • Give us some specific links so we can see which links your are talking about – Lauro S. Jul 28 '15 at 19:31
  • just click on one of the links in the view layout. its a grid layout. click on the athletes name. – fitnessCraze Jul 28 '15 at 20:26
  • Yeah, you will actually go to each node, not the view itself. Now I got it, you want to expose the taxonomy of the node as a class, right? I will change my answer! You can do it easily with Context module or using a custom module (or even in your theme). – Lauro S. Jul 28 '15 at 21:24
  • Ok. so i'm using the context module and i have $term-name in the header. I just need to know how to make it the term name that selected. take a look. dev-httpwwwrestechsyscom.pantheon.io/content/carina-marsh – fitnessCraze Jul 30 '15 at 12:12

You can apply different background for different pages using body class which will be unique for each pages. The way you have done right now is correct.

body.page-profiles-football .container{background-image:url(''); }
| improve this answer | |

There is many ways to do this , the most easy way is to use the classes name that added to your body tag , so you do in your css

.page-node-52 #main-content{
  background: ....

this classes changes based on the current node ID

Another way you can add image field to node and update the background and in node.tpl file you render that image as background.

also you can add any class to the body array based on page_preprocess in your template.php checkout Drupal API - template_preprocess_page

| improve this answer | |

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.