Skip to content

Instantly share code, notes, and snippets.

@stephenh1988
Created November 28, 2012 12:47
Show Gist options
  • Save stephenh1988/4161014 to your computer and use it in GitHub Desktop.
Save stephenh1988/4161014 to your computer and use it in GitHub Desktop.
Add classes to the body element of the page for event pages
/**
* A function which adds classes to event pages corresponding to the event
* E.g. adds 'eo-event-cat-[cat slug]' class for each category the event belongs to.
* Adds 'eo-event-venue-[venue slug]' if the event has a venue
* Adds time based class: eo-event-future/past/running for future/past/running events
*
* @url http://wordpress.org/support/topic/problem-with-highlight-in-menu-change-url-of-events?replies=8#post-3458829
* Not tested
*/
add_filter( 'body_class', 'my_event_organiser_add_classes_to_event_body');
function my_event_organiser_add_classes_to_event_body($classes){
if( !is_singular('event') )
return $classes;
//Add category classes
$cats= get_the_terms(get_the_ID(), 'event-category');
if( $cats ){
foreach ($cats as $cat)
$classes[] = esc_attr('eo-event-cat-'.$cat->slug);
}
//Add venue class
if( eo_get_venue_slug() )
$classes[] = esc_attr('eo-event-venue-'.eo_get_venue_slug());
$start = eo_get_the_start(DATETIMEOBJ);
$end= eo_get_the_end(DATETIMEOBJ);
$now = new DateTime('now',eo_get_blog_timezone());
//Add 'time' class
if( $start > $now ){
$classes[] = 'eo-event-future';
}elseif( $end < $now ){
$classes[] = 'eo-event-past';
}else{
$classes[] = 'eo-event-running';
}
return $classes;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment