Skip Navigation

[Resolved] Adding body classes based on mutliple checkboxes' values

This support ticket is created 5 years, 2 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Kolkata (GMT+05:30)

This topic contains 2 replies, has 2 voices.

Last updated by Thomas AMX 5 years, 2 months ago.

Assisted by: Minesh.

Author
Posts
#1355249

Hi guys,

I would like to add css classes to the body, based on the values of a custom field. The type of the field is multiple checkboxes.
This way I could quickly apply custom styles from the post editing screen, rather than maintaining long custom css with post-IDs. In the past, I did it with custom meta taxonomies but I want to avoid useless archive pages.

I have found this:

add_filter( 'body_class','my_body_classes' );
function my_body_classes( $classes ) {
	global $post;
    $classes[] = get_post_meta($post->ID, 'wpcf-my-custom-field', true);
    return $classes;
     
}

It works fine when the field is a single checkbox. But with multiple checkboxes field, it will only add "Array" class. I have tried a few things but they either break the site or don't work. I suspect foreach would be required, but I am not able to do it myself. Perhaps you could help me out?

Cheers,
Tom

#1355419

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

You can use the Type's PHP fields API function types_render_field() to get the values of the checkboxes field.

Can you please try to use the following code and try to resolve your issue:

add_filter( 'body_class','my_body_classes' );
function my_body_classes( $classes ) {
    global $post;
     
    $classes = types_render_field( "my-custom-field", array( "separator" => "-" ) ); 
    $classes= explode("-",$classes);
 
    return $classes;
      
}

More info:
=> https://toolset.com/documentation/customizing-sites-using-php/functions/#checkboxes

#1356123

Thank you Minesh, that was very helpful. At first, it didn't work as expected but I made some modifications and as far as I can tell, it works now. It should be $classes[] or otherwise it will remove some of the existing classes.
I also expanded it to archives and term fields. If anyone needs it, here is what I've come up with:

add_filter( 'body_class','my_body_classes' );
  
function my_body_classes( $classes ) {
    if  ( is_singular() )  {
      
    $classes[] = types_render_field( 'my-custom-field-slug', array( "separator" => " " ) ); 
    $classes[] = explode(" ",$classes);
  
     return $classes;
	 }
	  
     if  ( is_archive() )  {
      
    $classes[] = types_render_termmeta( 'my-term-field-slug', array( "separator" => " " ) ); 
    $classes[] = explode(" ",$classes);
  
    return $classes;
	  } 
}

Cheers,
Tom