(just got reccommended to post this over here from stack overflow…)
I am just starting with WordPress (defecting from Joomla)- having a bit of difficulty getting started.
One thing I am trying to do is make a custom plugin that allows the user to add custom backgrounds to the site, based on the active menu item.
So what I am trying to do is get the active menu id (like ‘menu-item-xx’ that is added to the nav li’s) and active parent menu id so I can add these to the body classes.
Is this something I can get from anything built into WordPress or is this something I will need to make a custom for?
Any pointers most welcome…
UPDATE
I have solved this now with a custom function that you can find below
From my comment earlier: Use the
body_class()
function in yourheader.php
file or wherever your body tag is, e.g.<body <?php body_class(); ?>>
. This will give you an output with a bunch of classes on the body that you can then use in your CSS. For example,<body class="page page-id-114 page-parent page-template-default logged-in admin-bar">
. You can also use thebody_class
filter to add more classes. If you want to add the slug, for instance, you wouldglobal $post
and then use$post->post_name
.Ok I have sussed out a function to achieve this. I am at the very start of my journey with WordPress so I am not sure about the quality or efficiency of this code. But here it is:-
You add that to your templates ‘functions.php’ and also somewhere after the function add a filter to call it with:-
What this does?
What this does is get the active menu ids and adds them to the body classes in ascending order
Why would I need this?
Coming from a CMS background I am used to pages being defined by the menu. With this function the active menu item id and/or its parent item id is added to the body classes so you can add page wide styles based on the active menu item. So for example you may have a menu structure like this:-
->menu-item-1
->menu-item-2
->->menu-item-3
Using this function if you are viewing the page ‘menu-item-3’ the classes ‘menu-item-2’ and ‘menu-item-3’ are added to the body tag in that order. What this means is that you can have a css rule that targets body.menu-item-3 that can fall back to its parent items rule in body.menu-item-2 and ultimately to body if there are no styles for either. What I am using this for, for example, is to have a default background colour for the whole page (body) a background colour for the whole page when menu-item-2 is active (body.menu-item-2) and a different background colour for the page when menu-item-3 is active (body.menu-item-3).
Hope that helps someone 😉
I have tried getting the Page slug but it seems to be empty in wp_head but you can get the page id and assign it to the body class like so:
.
After you got a class you can assign a background using css like this:
.
ps. it was fun finding the solution 🙂 – hope this helps,
cheers, Sagive
.
As for Dynamic Menu Highlighting- here is a code that would do that:
Ok my last shot was a while ago now and I was a bit of a novice- I look at that now and its embarassing! I really did not get WordPress then!
This is what you need…
returns the current item id along with parents and ancestors 😉