Viewing 12 posts - 1 through 12 (of 12 total)
  • Author
    Posts
  • #633892

    I was wondering if there is a way to add the category slug as css class to the style of the body content element? I need to make a visual distinction between different categories.

    Example:
    <body id=”top” class=”news single single-post postid-90 single-format-standard logged-in admin-bar stretched open_sans customize-support” itemtype=”https://schema.org/WebPage&#8221; itemscope=”itemscope”>

    #633947

    Hey Tracce,

    Thanks for getting in touch with us!

    Yes you absolutely can. You need to turn on the custom CSS class and then you can add any class to the elements you want. You can follow the instructions here: http://kriesi.at/documentation/enfold/turn-on-custom-css-field-for-all-alb-elements/

    Let me know if you need further assistance :)

    Best regards,
    Jordan

    #634257

    I Jordan,
    thanks for the quick replay.
    I think what i needs is slightly different, i’ll try to explain better:
    I need, for all the articles’ pages under a specific category, to have that specific “category slug” as a class of the “body”.

    Example: For the articles in the category NEWS i’ll have
    <body id=”top” class=”NEWS single single-post postid-90 single-format-standard logged-in admin-bar stretched open_sans customize-support” itemtype=”https://schema.org/WebPage&#8221; itemscope=”itemscope”>

    Example: For the articles in the category SPORT i’ll have
    <body id=”top” class=”SPORT single single-post postid-90 single-format-standard logged-in admin-bar stretched open_sans customize-support” itemtype=”https://schema.org/WebPage&#8221; itemscope=”itemscope”>

    is it possible?

    #634958

    Hi,

    Thank you for the info.

    Please add this in the functions.php file:

    // add category class on single post page
    add_filter( 'body_class', 'add_category_class' );
    function add_category_class( $classes ) {
        if( is_single() && has_category('news') ) $classes[] = 'news-cat';
        return $classes;
    }
    

    The code will add the “news-cat” class attribute for single posts included in the category “news”.

    Best regards,
    Ismael

    #988634

    Hi Ismael,

    This works for me but I want to add the function 2 times so I can give the category news the class news-cat and an other page the category water so I can add the class water-cat to that pages.
    If I paste this function 2 times my site is down. How can I fix this?

    #989403

    Hi,

    // add category class on single post page
    add_filter( 'body_class', 'add_category_class' );
    function add_category_class( $classes ) {
        if( is_single() && has_category('news') ) {
     $classes[] = 'news-cat'; }
    elseif( is_single() && has_category('news') ) { 
     $classes[] = 'news-cat'
    }
        return $classes;
    }

    Please use that one and let us know if that works out for you.

    Best regards,
    Basilis

    #989691

    When I add this, my site breaks :(

    add_filter( 'body_class', 'add_category_class' );
    function add_category_class( $classes ) {
        if( is_single() && has_category('consument') ) {
    $classes[] = 'consument-cat'; }
    elseif( is_single() && has_category('projectontwikkelaar') ) {
    $classes[] = 'projectontwikkelaar-cat'
    }
        return $classes;
    }
    #989836

    Hi (Email address hidden if logged out) ,

    There is a semicolumn missing.

    Try like this:

    
    add_filter( 'body_class', 'add_category_class' );
    function add_category_class( $classes ) {
        if( is_single() && has_category('consument') ) {
           $classes[] = 'consument-cat';
        }
          elseif( is_single() && has_category('projectontwikkelaar') ) {
             $classes[] = 'projectontwikkelaar-cat';
         }
        return $classes;
    }
    

    If you need further assistance please let us know.
    Best regards,
    Victoria

    #990126

    Hi Victoria,

    Thanks, kind of stupid I didn’t see this semicolumn by myself haha.
    Next time I need to take a better look!

    #990263

    Hi (Email address hidden if logged out) ,

    You’d be surprised how many times it’s just a semicolumn and how much it can break things :)

    Glad we could help :)

    If you need further assistance please let us know.
    Best regards,
    Victoria

    #995393

    I have this now because I want to add a category for different pages:

    add_filter( 'body_class', 'add_category_class' );
    function add_category_class( $classes ) {
        
        
        if( is_single() && has_category('gasvrij-academy') ) {
           $classes[] = 'gasvrij-academy-cat';
        }
        
        elseif( is_single() && has_category('informatief') ) {
           $classes[] = 'informatief-cat';
        }
        
        elseif( is_single() && has_category('advies') ) {
           $classes[] = 'advies-cat';
        }
        
        elseif( is_single() && has_category('offerte') ) {
           $classes[] = 'offerte-cat';
        }
        
        elseif( is_single() && has_category('afspraak') ) {
           $classes[] = 'afspraak';
        }
        
        
         return $classes;
    }

    But with this code the categories are not added to the pages, is there something wrong in this code?

    #995514

    Hi (Email address hidden if logged out) ,

    The function looks ok, you need to debug it, to check if it fires and if the conditions are met and see what the classes get in the end.

    Best regards,
    Victoria

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