Skip to content

Instantly share code, notes, and snippets.

@stephenharris
Forked from stephenh1988/body-class-event.php
Created February 19, 2013 18:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stephenharris/4988316 to your computer and use it in GitHub Desktop.
Save stephenharris/4988316 to your computer and use it in GitHub Desktop.
/**
* 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