[Resolved] Custom Classes on Elements Layout

Home Forums Support [Resolved] Custom Classes on Elements Layout

Home Forums Support Custom Classes on Elements Layout

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #1564774
    Matt

    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.

    #1564929
    Elvin
    Staff
    Customer Support

    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' ) );
    } );

    A wise man once said:
    "Have you cleared your cache?"

    #1564931
    Matt

    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

    #1564985
    Elvin
    Staff
    Customer Support

    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.

    A wise man once said:
    "Have you cleared your cache?"

Viewing 4 posts - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.