Add Category name as body_class

Home Forums Welcome! Pre-Sales Questions Add Category name as body_class

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #1023168
    Thibault
    Guest

    Hello,

    The category name isn’t added to body classes on the single-event pages.
    I tried to do it from my function.php
    function category_id_class( $classes ) {
    global $post;
    //testing
    echo $post->ID;
    }
    add_filter( 'body_class', 'category_id_class' );

    But $post->ID isn’t set at that time. Although it is on ‘regular’ WP pages.
    How should I achieve it ?

    Thanks by advance,
    Thib

    #1023203
    George
    Participant

    Hey @Thibault,

    Sorry that this isn’t documented clearly somewhere on our site – we should fix that promptly.

    In the meantime, the thing you have to do here is actually call upon the global $wp_query variable and use its get_queried_object_id() method to, well, get the ID queried object [the event].

    I wrote up code that demonstrates this and that adds Event Category terms to the body_class for you here: https://gist.github.com/ggwicz/fdee77d7d8f54ba0da4b

    Check that out! Try adding that snippet to your theme’s functions.php file and it should do the trick.

    For example, let’s say you have these three categories on a given event:

    • Private Event
    • Concert
    • Free Food

    This code, if added in functions.php, will add these three HTML class names to the <body> tag:

    • tribe-events-cat-private-event
    • tribe-events-cat-concert
    • tribe-events-cat-free-food

    I hope this helps!

    Cheers,
    George

    #1027324
    Thibault
    Guest

    That did the trick, thanks !

    #1028323
    George
    Participant

    Woot! 🙂 Best of luck with your site.

    — George

Viewing 4 posts - 1 through 4 (of 4 total)
  • The topic ‘Add Category name as body_class’ is closed to new replies.