Welcome to the Geeks & God Static Archive. Read more »

Adding the node type to the body class

Joined: 12/16/2007

Hey, can someone point me in the right direction for dynamically adding the current node-type to the body class? I just need to add icons to the node-title based on content type, and I'd rather not add node-type.tpl.php files just to add an icon.

Thanks!
Tony

Joined: 12/16/2007
I found this...

I found this page in the drupal handbook:
http://drupal.org/node/162485

But it's not working, for me... don't know what I'm doing wrong. I created a template.php file, copied and pasted in the snippet, then added the body class code to my page.tpl.php file, but it just adds blank " " marks instead of the node-type.

I cleared my theme registry, as well, and my browser cache. I know Drupal is recognizing the template.php file, because when I deleted some of the code, I got a parsing error.

Help!

Joined: 10/18/2008
Maybe add print $node->type

Maybe add print $node->type to your node.tpl.php file?

/ * Begin Signature */
It's a strange thing about determined seekers-after-wisdom that, no matter where they happen to be, they'll always seek that wisdom which is a long way off. Wisdom is one of the few things that looks bigger the further away it is.

Joined: 12/16/2007
hmmm....

so you are saying do that in the node.tpl.php file instead of the page.tpl.php file? So, I could add an extra div to it, or, I suppose I could add it as a class to the h2 tag itself... how would I format that if I added it? like this?

<h2 class="<?php print $node->type; ?>">Node Title PHP Code</h2>

Thanks for the tip.

Joined: 12/16/2007
Well, kind of...

I got it to work, I was already printing the node-ID in a div class, so I added your code to the claa, and it worked... but the node title prints in the page.tpl file, not the node.tpl file, so that didn't quite work for my use case. Grrrrr. If I add <?php print $title ?> to the node.tpl instead of the page.tpl, then views titles won't print, I believe. Maybe I'm wrong on that, I'll have to check.

Joined: 12/16/2007
Yep...

I just made the switch and moved <?php print $title ?> into the node.tpl file, and it makes the title go away on my views... how do I put just views titles into my page.tpl.php file?

Joined: 12/16/2007
Advanced CSS Selector?

Here's the structure of the content section of my site now...

<div id="content">
  <div id="drupal-breadcrumbs">
  </div>
  <ul class="tabs primary">
  </ul>
  <h2 class="node-title">Who Ah News</h2>
  <div id="node-9" class="news node">
    <div id="node-content">
    </div>
  </div>
  <div id="comments">
  </div>
</div>

Is there a css selector that will allow me to select an h2 that is followed by a div with a particular class? I know how to select the div that is preceded by a particular tag...

Joined: 10/18/2008
Okay, I'm confused: what's

Okay, I'm confused: what's the issue on the table now?

Did <h2 class="<?php print $node->type; ?>">Node Title PHP Code</h2> work?

As far as printing only the views title in your page.tpl.php file, I suppose you could write something like

<?php if (isset($views)) { print $title } ?>

Then if there's a views object, print the title... I dunno.

/ * Begin Signature */
It's a strange thing about determined seekers-after-wisdom that, no matter where they happen to be, they'll always seek that wisdom which is a long way off. Wisdom is one of the few things that looks bigger the further away it is.

Joined: 12/16/2007
Here's the issue

I haven't tried your views suggestion yet...

The issue is, yes, <h2 class="<?php print $node->type; ?>">Node Title PHP Code</h2> would work if the Node title was printed in the node.tpl file. It's not. It's printed in the page.tpl file, and $node->title is not available in the page.tpl file.

My ultimate work around right now would be a css selector that allows me to select an h2 tag that is adjacent to a div tag containing the class I wanted. Unfortunately, the class, when added to a div in node.tpl, exists after the h2 tag, and my understanding of the adjacent sibling elector is that the adjacent sibling needs to occur before the tag you want to select.

For instance, this would work:

<div id="content">
  <div class="news">
    <span class="messages"></span>
    <span class="tabs"></span>
  </div>
  <h2>Node Title</h2>
  <div class="node-content">
    <p>blah blahb labhabhlabhla</p>
  </div>
</div>

#content .news + h2 {
  background: #000000
  border: solid 1px #555555;
}

While this would not:

<div id="content">
  <span class="messages"></span>
  <span class="tabs"></span>
  <h2>Node Title</h2>
  <div class="node-content news">
    <p>blah blahb labhabhlabhla</p>
  </div>
</div>

#content h2 + .news {
  background: #000000
  border: solid 1px #555555;
}

The first example would select only h2 tags that are adjacent to .news tags, and make the background black. That's basically what I want to do (except I want to add a node-specific icon to each title.)

The second example would select .news classes that are adjacent to h2 tags. Arghh... I think I'm going to have to make a .tpl for each node type... I have 14. That's annoying.

Tony

Joined: 10/18/2008
Humm, seems you've got two

Humm, seems you've got two options, either move the title into node.tpl.php or use jquery...

Personally, I like my titles inside my node templates.

/ * Begin Signature */
It's a strange thing about determined seekers-after-wisdom that, no matter where they happen to be, they'll always seek that wisdom which is a long way off. Wisdom is one of the few things that looks bigger the further away it is.

Joined: 12/16/2007
Title in Node Template...

When I did that, i got lots of weird things like duplicated titles in views, etc... If I use node as the view type rather than fields, then the view tacks on the title, as well as the node template. Any way to fix that to still allow for teasers on a views-generated page?

Joined: 10/18/2008
What did you do? And why did

What did you do?

And why did the handbook thingy not work?

/ * Begin Signature */
It's a strange thing about determined seekers-after-wisdom that, no matter where they happen to be, they'll always seek that wisdom which is a long way off. Wisdom is one of the few things that looks bigger the further away it is.

Joined: 12/16/2007
I haven't finished it yet

I haven't finished the site yet... It's on hiatus while I move to myrtle beach...

I don't KNOW why the handbook page didn't work... that's the correct solution. If you were wiling to take a look at it, I'd give you the info you need off the board.

Blessings,
Tony

Joined: 12/16/2007
Nevermind, argh....

Someone finally answered my post on Drupal.org. The handbook page is for D5. It's marked as D5 in the taxonomy. Anyway, there's a variable already made available to page.tpl.php that does this, no template.php file required: <?php print $body_classes; ?>. Simple. Apparently, you just have to know about this, though, because none of my searches brought up this info.

It all works now.

Had this same problem... thanks for the help

I had this exact same problem and this thread lead me directly to the solution. Thanks.

Here is what I ended up doing:

page.tpl.php
-------------

<?php
print $title
?>

style.css
----------

h1.page-node, h1.page-blog, h1.page-projects, h1.page-image, h1.page-sitemap {

[styles]

}

h1.node-type-blog {

[Different styles]

}

php again...

<?php
print $title
?>

Joined: 02/12/2010
PHP should be simple. It

PHP should be simple. It isn't for me. You should have seen the trouble I went through just to get a favicon to work on my blog.

Mathew Farney | Web Hosting