Get category name (body classes)

Home Forums Calendar Products Events Calendar PRO Get category name (body classes)

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #931518
    vicdilu
    Participant

    Hi,

    I had this code snippet in my theme, in functions.php

    add_filter(‘body_class’,’add_category_to_single’);
    function add_category_to_single($classes) {
    if (is_single() ) {
    global $post;
    $terms = get_the_terms( $post->ID, ‘tribe_events_cat’ );
    foreach ( $terms as $term ) {
    // add category slug to the $classes array
    $classes[]=$term->slug;
    }
    }
    // return the $classes array
    return $classes;
    }

    It was working ok, but after upgrading Events Calendar, Events Calendar Pro and the filter bar addon to the last version (3.9) in no longer working. If I display the php errors, there is this message:

    Warning: Invalid argument supplied for foreach()

    Any help it would be appreciated.

    Thanks in advance,
    Victor

    #931741
    Brian
    Member

    Hello,

    Thanks for using our plugins.

    I took a look at the coding and it appears on the Events Pages the Global $post is not returning anything.

    Not sure why that is no longer showing anything there. We are pretty busy today catching up from the weekend,but maybe to help out tomorrow if you are unable to get it.

    Thanks

    #931970
    vicdilu
    Participant

    Hi Brian,

    I haven’t found a solution for this yet.

    If I can’t fix this, I would have to downgrade to older versions of tribe events plugins, but that isn’t a good solutution.

    Looking foward for your support.

    Thanks,
    Victor

    #932008
    Brian
    Member

    Ok so not much we can do with customizations, but if you use this global instead of $post:

    global $wp_query;

    and this for the ID:

    $wp_query->posts[0]->ID

    That should work.

    I would also add a check if there are terms before going into the foreach loop as a event without terms with cause the foreach loop warning too.

    #932181
    vicdilu
    Participant

    Thanks Brian,

    With your code, it work’s perfectly again.

    I also added a conditional to check if there are terms before the loop.

    Regards,
    Victor

    #932189
    Brian
    Member

    Great, glad it helps, I am going to go ahead and close this ticket, but if you need help on this or something else please post a new ticket.

    Thanks!

Viewing 6 posts - 1 through 6 (of 6 total)
  • The topic ‘Get category name (body classes)’ is closed to new replies.