I recently ran into a need I don’t think I’d seen before: adding a body class for the current page and for any parent pages.
This could be needed for a number of reasons:
- Hiding and showing menu items conditionally based on the page the user is on
- Changing styles for specific pages and their children (for example, a site with several sections which each have a different logo or color scheme)
- Hiding certain sections of specific pages or ancestors (please note that for this one in particular, there’s probably a better way than just hiding the content).
Here’s the snippet I found and modified just a tad from Tim Niell. This actually works pretty perfectly for my use case (thanks, Tim!):
Just pop it into your functions.php file, and be sure to use your own prefix!
Leave a Reply