Hi Michael,
Do you want just to detect whether it’s a category page or to detect which category page is being shown?
Hi caimin,
Neither. I am on a single events page and want to detect which category (or categories) the shown event is assigned to. Then, I want the slug of that categories to be added to the body class.
Say, I’ve an event that belongs to the categories ‘Classical Music’ and ‘Concert’, then the body tag should get those added additional to its other classes. Like:
<body class="classical-music concert event-template-default single single-event postid-196429 *and-so-on...*">
-
This reply was modified 9 years, 1 month ago by
Michael.
-
This reply was modified 9 years, 1 month ago by
Michael.
Pretty sure you can do this reference $EM_Event->categories by that point of the page.
In my function.php, $EM_Event->categories didn’t work. But with a little trying, I came to this solution:
add_filter( 'body_class', 'add_event_category_to_body_class');
function add_event_category_to_body_class( $classes ) {
if ( em_is_event_page() ) {
global $post;
$EM_Event = em_get_event($post->ID, 'post_id');
$classes[] = $EM_Event->output('#_CATEGORYSLUG');
return $classes;
}
}
This does exactly what I asked for, except one thing: if there’s an event that has assigned more than one category, this will only output one. I think it’s always the one with the highest ID. Don’t know if that’s an error in my function or if $EM_Event->output(‘#_CATEGORYSLUG’) does only return one category.
As far as I know that’s standard WordPress behavior.