Hey,

I'm trying to get my body tag to include an ID that reflects the page's url path. So for example being in foods/snacks/peanuts would print out a body tag like this body id="foods-snacks-peanuts" I found this post but it's for d5 and i couldnt get it to work on my d6 install.

Anybody got any ideas??

Thanks a whole lot!

Peace,

Fernando

Comments

jeeves’s picture

I noticed the Themer Module is supposed to support tokens. You might be able to use that to include the page URL path in the body class.

Sorry I don't have any more information than that. I have only used Themer to assign a unique body class to nodes based on the page alias name.

gtothab’s picture

Hey

thanks for the reply. The themer mod looked promising but installing it made my site insanely slow. I re-installed it, re-downloaded it and nothing. So I never found out how it worked cause navigating from one page to another took like 5 min.

Anybody know why its doing that??

Thanks again.

F

gtothab’s picture

Also, I tried declaring the following variable:

$alias = drupal_get_path_alias($_GET['q']);

And then calling it inside the body's id value. This prints the url path exactly but the problem is that it prints out the forward slashes (i.e. foods/snacks becomes body id="foods/snacks". So the problem is that forward slashes are not valid characters to place inside of there and any styles applied dont stick.

Does anybody know how to swap out the forward slashes for dashes in that php snippet? Or perhaps just removing them altogether producing something like id="foodssnacks" in the above example.

Thanks a bunch

F

gtothab’s picture

A similar solution to this is what ive chosen to do. I've put the node id as the body's id value like this:

body id= "node- print $node->nid "

mndonx’s picture

Did you add $body_classes?

<body class="<?php print $body_classes; ?>">

That won't give you that class built off your URL but there are some things in there that are certainly helpful.

The zen theme comes with it all built in (including body class based on URL) -- I usually start with zen so I can take advantage of that! :)

gtothab’s picture

I have the body_classes var inside my body's class but its not specific enough. Did you say that the zen theme provides a way to add the url path as a class? How so?

Thanks for the reply, im very interested. . .

Fernando

mndonx’s picture

Hi graphbyte -

You'll see on the Zen (Drupal 5) list of features that "page-FULL-URL" is included. I can vouch that carries over to Drupal 6.

I'm not exactly sure how you would go about replicating that -- but if you look here you might get some ideas:

http://mydrupalblog.lhmdesign.com/classes-almost-everything-drupal-theme

I think you have to explode the URL, as documented in the section under "Add unique classes for each page and website section" on that page.

Hope that helps!

gtothab’s picture

Thanks so much,

Im going to look into that for sure.

samwich’s picture

Have you made any progress? I am looking to implement the same type of feature in my site, but am struggling to do it correctly.

http://www.samwirch.com

samwich’s picture

Never mind I figured it out, I was using drupal 6 and used the link posted above. In the comments section there is a modification for drupal 6.

Thanks

http://www.samwirch.com

BrianLewisDesign’s picture

In D6, the $body_classes is defined in core. All you need is to echo:
<body class="<?php print $body_classes; ?>">

And for more classes use themer, and:
http://drupal.org/project/themer
<body class="<?php print themer_body_class(); ?> <?php print $body_classes; ?>">