Hi,

Sorry for my bad English... I am still a Drupal learner and I am having difficulties to find out how to use different background images for each menu item (parent) and their respective children.
I will try to explain better. I didn't use primary links or secondary links.

Menu:

Item 1 (this item will go to a page with "background-image1.jpg")
subitem1 (this child will use the same "background-image1.jpg", as it's parent)
subitem2 (same thing)
subitem3 (same thing)
Item 2 (this item will go to a page with "background-image2.jpg")
subitem1 (this child will use the same "background-image2.jpg", as it's parent)
subitem2 (same thing... and so on...)
Item 3 (background-image3.jpg)

My first attempt is based on node-paths. I put this code in my page.tpl.php like this:

<body class="<?php echo phptemplate_body_class($left, $right); ?>" id="<?php echo $node->path ?>">

I set the ids in my css. For example:

Let's say $node->path for Item1 = item1

<body id="item1">

And the CSS for it is:

body#item1 {background-image: url(background-image1.jpg)}

This approach worked. However, the subitems have different node paths and I could not find anything that would say subitem1 (for example) is a child of Item1.

At least, if I got something like item1/[path-to-subitem1] from the urls I think it could be easier. Or, if there are other ways...

Thanks,
Andre

Comments

mndonx’s picture

Hi - I responded to a post by someone with a similar question here:

http://drupal.org/node/698636#comment-2535058

The part of the function that reads: $vars['body_classes'] .= ' ' . YOURTHEME_id_safe('section-' . $section) . ' '; will give you a class specific to the first arg in your URL, so it should emulate the concept of sections - as long as the url paths are structured like nested sections, like: http://example.com/mysection and http://example.com/mysection/mysubsection.

Let em know if that makes sense.

Amanda

marcanth’s picture

Hi,

Thanks for answering. Well, I'm having difficulties regarding url paths as sections. After I put your code on template.php and added echo $body_classes in page.tpl.php, I found new classes on body. However, only on subitems. For example:

Menu is:

who we are (url = whoweare)
sub who we are (url = subwhoweare)

So, for the subwhoweare page I have the following code:

<body class="lateral-esquerda not-front logged-in page-node node-type-page one-sidebar sidebar-left page-subwhoweare  section-subwhoweare " id="subwhoweare">

The new classes are page-subwhoweare and section-subwhoweare. However, "subwhoweare" is url for the "sub who we are" page. So I cannot set CSS rules yet because they are based on the parent item.

I don't know how to structure my paths as sections. Later, I've found about the section module and installed it, but it didn't help me much. It allows me to create a section and choose a theme to apply... Could you enlighten me a little more?

Thanks,
Andre

mndonx’s picture

Hi Andre - Ah, I think I forgot to ask how your URLs are set up. You will need to make sure your URLs match the menu structure. So, your URL should look like http://www.example.com/whoweare and http://www.example.com/whoweare/subwhoweare. That will allow you to grab the parent menu item as a class on your body_classes. Does that make sense?

Amanda

marcanth’s picture

Hi Amanda

Thanks for answering. The URLs are like this:

http://www.exaple.com/?q=whoweare and http://www.example.com/?q=subwhoweare. What do I need to do to make them match the menu structure? I am pretty sure that if I've got to do this, I could grab the parent menu item.

One thing I forgot to mention is: When I created the "subwhoweare" page I set the URL to just "subwhoweare". Then, I created the submenu item and set it to this address. Was this method correct? Or, in this case I should have set the page URL as "whoweare/subwhoweare"?

Andre

mndonx’s picture

You can enable Clean URLs by going to /admin/settings/clean-urls. Then when you add a submenu item, yes, set the URL to whoweare/subwhoweare.

marcanth’s picture

Hi

Well, thanks. It is working now. :) Indeed, after I manually set the URL (when creating a new page) to whoweare/subwhoweare I've got a class in $body_classes that allowed me to find info for the parent menu item and set a CSS rule.

I was just wondering... If there is a way to do this in a little more... automatic way... I will explain:

  • When creating a new page, I set a URL like: subwhoweare.
  • After that, I create a new menu item. It's URL will be subwhoweare.
  • Now, since I set this menu item as a child of the whoweare menu item, the system (a function to preprocess, maybe?) could automatically "reset" this URL subwhoweare to whoweare/subwhoweare.

Does this make sense? Would this be too difficult to do? If something like this could be done, I think it would be great and make things easier for people that will add content.

Thank you, Amanda!

Andre

mndonx’s picture

Hi Andre - You know that you can add the page to the menu from the node form, correct? You don't have to go into the menus to add it. It's usually a collapsed item on the node form. I'm not sure of a way to automate the generation of URLs based on how they are positioned in the menu system, but perhaps there is a way...

marcanth’s picture

Oh yes, Amanda, I know that. Thanks. :) At the beginning I wasn't sure if it was exactly the same thing... started to work this way and continued... But I will do it the way you mentioned... it is easier...

While doing some search, I found these 2 links:

custom_url_rewrite_outbound
custom_url_rewrite_inbound

Could these be of help? I am not an expert about re-writting functions in Drupal or preprocessing...

Thank you,
Andre

mndonx’s picture

Yep - me neither. I'm afraid that's out of my depth!

marcanth’s picture

Hi Amanda

Well, you showed me one way to do what I was trying to achieve. :) Now, I will continue searching and learning more and more. Drupal is a great tool, and full of interesting things...

Thanks for your help.
Andre

mzwyssig’s picture

Hi,

Does somebody know how to do that without having the structure within the URL ?

example.com/page1 and example.com/page2 are in menu "category1" and example.com/page2 & example.com/page3 are in menu "category2" and i'd like category1 and category2 to appear in the body classes... Is that possible ?

Thanks

M