Archived topic
Custom Classes on Elements Layout
3 replies · Started by Matt on December 2, 2020
Hi Guys,
I'm using the 'Elements' module to create 2 layouts.. 1 for logged in users, one for logged out users. I'm wondering if there is any way to apply a custom class to these layouts (preferably on the body tag) so that I can do some syling based on each layout?
Thanks.
Hi,
You can add your custom classname on your <body> tag by using the body_class filter:
https://developer.wordpress.org/reference/hooks/body_class/
Example:
add_filter( 'body_class', function( $classes ) {
return array_merge( $classes, array( 'your-class-name' ) );
} );
How would I make this conditionally apply to one of the 2 layouts created in elements? I don't want the same class on all body tags.
Thanks
You can add in "if" conditions depending.
Example:
add_filter( 'body_class', function( $classes ) {
if(is_user_logged_in()){
return array_merge( $classes, array( 'your-class-name' ) );
} else {
return $classes;
}
} );
What this snippet does is, it checks if the current visitor is logged in. If the visitor is logged in, it adds a custom class. Else(meaning non logged in visitors), it will display the default classes.