• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

We’ll become available for new work in early October 2023.

Questions? Call 254.340.0086 or send an email to jon@elod.in.

Elodin Design

WordPress Design & Development

  • About
  • Websites
  • Services
  • Process
  • Blog
  • Repos
  • Contact

Add body classes for current page and parent pages

February 18, 2019 by Jon Schroeder Leave a Comment

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!

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About the blog

I’ve always been exceptionally bad at actually sitting down and writing blog entries, so do me a favor – please feel free to comment or drop me a line if you have something you’d like me to write about (or just to say hello).

Browse by category

  • Advanced Custom Fields
  • Gutenberg
  • Perspective
  • Tutorials & how-to guides
  • Updates

Centered and full-justified text on the internet

How to add contextual styling in the Gutenberg editor

User interface is really important

How to connect Gravity Forms with Salesforce web-to-lead using Gravity Forms’ Webhooks addon

Reviewing SpinupWP and why I closed my account

Ready to get started?

There’s no pressure. If you’re interested in building a site, please feel free to reach out – and I’ll talk through your project with you and give you some pointers even if we don’t end up working together.

Reach out

Elodin Design, Inc. (formerly Red Blue Concepts)
Website design & development · Custom WordPress theme and plugin development
3305 Fox Hollow Circle, Waco, TX 76708 · (254) 340-0086
Copyright © 2023 · All rights reserved.