Opens in a new tab

Adding a custom body class in the Bricks Builder theme for WordPress

February 16, 2024  By 

After years of faithfully building sites on Genesis and Beaver Theme going to a theme that has just what you need and no more has presenting the odd challenge or two.

Adding a custom body class is something that I’ve just expected to come already set up with a theme but it seems not. I’ll be creating a place here where I can leave all of my snippets of code for my own use in future but also it might just help out others too.

I needed to create a body class that a client could choose to add to a post in order to show or hide the featured image. When the class noimage was to be added the feature image wouldn’t show.

I started by enabling custom fields in my post preferences. You could also do this using Advanced Custom Fields.

I created a class called body_class in my post and gave it a value of noimage

I then added the following code to my theme funtions.php

add_filter( 'body_class', 'navitas_body_class' );

function navitas_body_class( array $classes ) {
	$new_class = is_single() ? get_post_meta( get_the_ID(), 'body_class', true ) : null;

	if ( $new_class ) {
		$classes[] = $new_class;
	}

	return $classes;
}

And that’s it. There were so many over complicated versions of this around. I hope this help someone else out.

Ready to find out more

Working with us is designed to feel collaborative, manageable and focused on creating clarity rather than overwhelm.

Discovery & Clarity

We start by understanding your business, your audience and where your current website may be creating confusion or friction.

Structure & Messaging

We create a strategic structure and messaging approach designed to help visitors move from curiosity towards confidence.

Design & Development

Your website is then designed and built with clarity, performance, trust and conversion in mind.

5 website leaks costing coaches clients

FREE DOWNLOAD

The 5 Website Leaks Costing Coaches Clients

GRAB YOUR COPY NOW

We’ll never share your information. You’ll also receive resources & exclusive offers. Privacy Policy

Secret Link