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

We’re currently booked and will be ready for new projects in late April 2023.

Questions? Call 254.340.0086 or send an email to [email protected].

Elodin Design

WordPress Design & Development

  • About
    • About Jon
    • Why ‘Elodin Design’
  • 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.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Cookie settingsACCEPT
Privacy & Cookies Policy

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these cookies, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may have an effect on your browsing experience.
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information.
Non-necessary
Any cookies that may not be particularly necessary for the website to function and is used specifically to collect user personal data via analytics, ads, other embedded contents are termed as non-necessary cookies. It is mandatory to procure user consent prior to running these cookies on your website.
SAVE & ACCEPT