Site logo

Archived topic

Content Template - Dynamic Container Background color or class

5 replies · Started by Matt on December 6, 2022

Viewing posts 1–6 of 6

Hi Guys,

Just wondering if there is any way to either dynamically set the background color of a container, or set a class on a container for a content template (or the content template wrapper).

I have a content template setup for my archives and want to allow the user to select a background color for one of the inner containers. Ideally I would use an ACF select field to give the user some pre-set options for classes or colours.

Thanks

Hi there,

if the content wrapper is a GB Container Block in the element, then you can add CSS Class to Advanced > Additional CSS Class(es).

Then you can add whatever style to that class.

Does that work for you ?

Hi David,

Not really. I need to do it per-post. So.. on render of the content template (for each post), get the value of the ACF field and either add a class to the article or inline style to the container.

Hi Matt,

Yes, you can do that. Either add an inline style depending on the returned value of your ACF field.

You can basically use the render_block filter to render the Block with your ACF "style". Or you can append a class and utilize this class to associate a specific background color depending on the custom field.

Reference: https://developer.wordpress.org/reference/functions/render_block/

Great.. Thanks for your assistance!

You're welcome, Matt!

This archived topic is closed to new replies.