• Resolved Michael

    (@mischnu)


    Hi there!

    I tried several things but didn’t get this to work:
    On a single events page, is there a way to add the current event category to the body class?

    I tried

    add_filter( 'body_class', 'add_event_category_to_body_class');
    function add_event_category_to_body_class( $classes ) {
      if ( em_is_event_page() ) {
        $classes[] = EM_Events::get('category');
        return $classes;
      }
    }

    but this won’t work. Is EM_Events::get() the right function for this anyway?

    Thanks for any help,
    Michael

Viewing 5 replies - 1 through 5 (of 5 total)
  • caimin_nwl

    (@caimin_nwl)

    Hi Michael,

    Do you want just to detect whether it’s a category page or to detect which category page is being shown?

    Thread Starter Michael

    (@mischnu)

    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 8 years ago by Michael.
    • This reply was modified 8 years ago by Michael.
    caimin_nwl

    (@caimin_nwl)

    Pretty sure you can do this reference $EM_Event->categories by that point of the page.

    Thread Starter Michael

    (@mischnu)

    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.

    caimin_nwl

    (@caimin_nwl)

    As far as I know that’s standard WordPress behavior.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Add event category slug to body class’ is closed to new replies.