I don’t need this whole mess of classes like this…
<body class="page page-id-829 page-template page-template-page-template-portfolio-php portfolio">
I’d like something like this…
<body class="portfolio">
Is there a filter snippet somewhere that has a list of all the classes and I can then just uncomment the stuff I don’t want to see in the body class.
Thanks.
You can configure the
$whitelist
array in this function to filter out all other unwanted classes.Just an addition to @Geert answer (added a blacklist too) 🙂
Please be so nice to mark @Geert s answer as solution (not this one).
I would recommend merely omitting the
<?php body_class(); ?>
template tag, if you have no need for its output.Just apply
class="portfolio"
hard-coded into the<body>
tag.Just place the classes of your css that you want to remove in $ class_delete
This will give every page a body class of only ‘portfolio’. The first argument is the array of generated body classes that would normally appear. The second argument is an array of classes passed into the body class function (e.g.
body_class('portfolio');
would make the second argument in this functionarray( 'portfolio' )
).If you only want to remove a specific class from body Tag you should to this:
While the solutions above enabled me to put something together that worked in the case where the classes were being added via body_class(), I had another situation where none of them worked at all because the classes were being added by JavaScript.
Additionally I only needed to remove the classes on specific pages and could not do this by modifying or overriding the JS without potentially impacting other pages.
To solve this I added the following to the functions.php of the child theme…
The above jQuery solution worked perfectly for my use case. For those who need/want a JavaScript only solution, I put this together…
And for the sake of sharing, here is what I put together for the body_class() use case using the ideas of those who posted before me.
The main difference with this code VS the posted snips is that this code enables whitelisting and blacklisting at the same time.
In the case I built this for I needed to restrict the body classes to a specific set globally and then remove others on specific pages.