Home › Forums › Calendar Products › Events Calendar PRO › tribe category in body class
- This topic has 8 replies, 2 voices, and was last updated 13 years ago by
Barry.
-
AuthorPosts
-
April 3, 2013 at 3:14 am #44726
Nathan
Participanthi, 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 worksIs there a different way to get the post ID of an event post?
thanksApril 3, 2013 at 10:04 am #44753Barry
MemberHi 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.
April 3, 2013 at 10:17 am #44756Nathan
ParticipantHi
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.
April 3, 2013 at 10:59 am #44766Barry
MemberYou’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).
April 3, 2013 at 11:17 am #44768Nathan
Participanti 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;
}April 3, 2013 at 11:20 am #44770Nathan
ParticipantAs 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.April 3, 2013 at 11:36 am #44772Barry
Memberi 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.
April 4, 2013 at 2:03 am #44821Nathan
Participantok 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.April 4, 2013 at 6:26 am #44827Barry
MemberSure, well definitely check if there is a conflict somewhere since, as I say, it works for me.
-
AuthorPosts
- The topic ‘tribe category in body class’ is closed to new replies.
