tribe category in body class

Home Forums Calendar Products Events Calendar PRO tribe category in body class

Viewing 9 posts - 1 through 9 (of 9 total)
  • Author
    Posts
  • #44726
    Nathan
    Participant

    hi, I am trying to add a body tag for certain event categories.
    I have tried this code in my functions file:

    add_filter(‘body_class’,’conf_class’);
    function conf_class($classes){
    global $post;
    if ( has_term ( ‘conference’, TribeEvents::TAXONOMY, $post->ID ) )
    { $classes[] = ‘event-conference’;}
    return $classes;
    }
    Which doesn’t work. However if i put the post id in manually, ie
    if ( has_term ( ‘conference’, TribeEvents::TAXONOMY, 73 )
    Then it works

    Is there a different way to get the post ID of an event post?
    thanks

    #44753
    Barry
    Member

    Hi Nathan,

    I suspect you are trying to obtain the post ID before the loop has run and so before the $post global has populated. You might need to do things in a different order/run the loop twice.

    #44756
    Nathan
    Participant

    Hi
    This is in the functions file, not in the loop. Other functions seem able to get the post ID using the same filter in my functions file.
    this function works perfectly:
    function category_id_class($classes) {
    global $post;
    foreach((get_the_category($post->ID)) as $category)
    $classes[] = $category->category_nicename;
    return $classes;
    }
    add_filter(‘body_class’, ‘category_id_class’);

    The body already has a class with the tribe event ID, so it must be available when wordpress is writing the body classes.

    #44766
    Barry
    Member

    You’re right – I was making an assumption based on what you were saying about this working when the post ID is set manually, I didn’t actually test it out – so apologies for any confusion 🙂

    On testing it out however I actually find that it works just fine, so, what you’d need to do here is debug your filter function and check out the value of $post->ID (in an instance where it otherwise fails, of course).

    #44768
    Nathan
    Participant

    i did say in the first message it was in the functions file.

    as far as i can see, $post->ID does not return anything for single events.
    i have used the following function which puts the page/post id into the body css for all posts other than tribe events:
    add_filter(‘body_class’,’conf_class’);
    function conf_class($classes) {
    global $post;
    $classes[] = $post->ID;
    return $classes;
    }

    #44770
    Nathan
    Participant

    As an alternative i tried using the conditionals listed on
    https://gist.github.com/jo-snips/2415009
    I can’t get these to work in the functions file (whereas wp conditionals do), though they work fine on actual template files.

    #44772
    Barry
    Member

    i did say in the first message it was in the functions file. as far as i can see, $post->ID does not return anything for single events.

    Sure. Unfortunately (or fortunately, depending on how you see things) if I test out your initial snippet of code by placing it in my theme’s functions.php it produces the expected result – event-conference is added to the body class for that event.

    I’m afraid I can’t really shed any light on why it isn’t working for you, unless perhaps there is a typo in the event category name or something of that nature – or if somewhere along the line there is a conflict with some other code.

    As an alternative i tried using the conditionals listed on https://gist.github.com/jo-snips/2415009 I can’t get these to work in the functions file

    Remember that as with some core WordPress functions you will not be able to use them immediately. For example, you should not use tribe_is_month() before the wp action fires.

    #44821
    Nathan
    Participant

    ok thanks for that.
    There definitely isn’t a typo as if i put in the id of a conference event, it works, and if I change the filter to be ‘post-class’ that works, so I’ll just try see what is causing it not to get the post id at that point.
    I’m trying to use the conditionals in the same way as wp conditionals which do work. (is_singular() works but tribe_is_event() doesn’t in the same body_class filter function.
    I’ll just have to see if it is conflicting with something else.

    #44827
    Barry
    Member

    Sure, well definitely check if there is a conflict somewhere since, as I say, it works for me.

Viewing 9 posts - 1 through 9 (of 9 total)
  • The topic ‘tribe category in body class’ is closed to new replies.