Support

Home Forums Event Espresso Premium Set EE4 categories as class

Set EE4 categories as class

Posted: March 11, 2015 at 10:21 pm

Viewing 8 reply threads


jamieshek

March 11, 2015 at 10:21 pm

Hi,

How would I set a container elements class with an the events categories?

For example: <article class=”my-event-category”>

Thanks,
Jamie


Dean

March 12, 2015 at 7:38 am

Hi,

If your theme is using post_class() to add classes to the Article tag, then you can use a filter, see here: http://codex.wordpress.org/Function_Reference/post_class#Add_Classes_By_Filters

Please note that Event Categories are in fact Custom Taxonomies (the slug is espresso_event_category).


jamieshek

March 12, 2015 at 6:57 pm

So to clarify if I add a function like this to add the classes I’ll have to use the slug: ‘espresso_event_category’?

Where in the function should that go?

// add category nicenames in body and post class
function category_id_class( $classes ) {
	global $post;
	foreach ( get_the_category( $post->ID ) as $category ) {
		$classes[] = $category->category_nicename;
	}
	return $classes;
}
add_filter( 'post_class', 'category_id_class' );
add_filter( 'body_class', 'category_id_class' );


Lorenzo Orlando Caum

  • Support Staff

March 12, 2015 at 7:32 pm

Hi, are you referring to something like this?

https://eventespresso.com/wiki/useful-php-code-snippets/#category-css

Sample code snippets can be added to your child theme’s functions.php file (do not include the opening php tag) or a site specific plugin:

https://eventespresso.com/wiki/create-site-specific-plugin-wordpress-site/


Lorenzo


jamieshek

March 12, 2015 at 9:39 pm

Thanks. That’s handy. Can they also be added to the body?


jamieshek

March 12, 2015 at 9:54 pm

Alright. I’ve sussed that one out.

Here’s how I’ve added the EE taxonomy to the body class. With help from this post: https://wordpress.org/support/topic/style-taxonomy-posts?replies=3#post-2815963

// add taxonomy term to body_class
function ee_taxonomy_in_body_class( $classes ){
  if( is_singular() )
  {
    $custom_terms = get_the_terms(0, 'espresso_event_categories');
    if ($custom_terms) {
      foreach ($custom_terms as $custom_term) {
        $classes[] =  $custom_term->slug;
      }
    }
  }
  return $classes;
}

add_filter( 'body_class', 'ee_taxonomy_in_body_class' );


jamieshek

March 12, 2015 at 9:55 pm

No my next issue is that I need the taxonomy to carry through to the body class on the registration pages.

Any ideas?


Josh

  • Support Staff

March 13, 2015 at 11:33 am

Hi there,

That’s kind of tricky because the registration page does not have direct access to the event object. This is because the registration checkout process, which we call Single Page Checkout internally, is designed to accommodate more than one event. I got some help from one of the developers who showed me how to get the event object from the transaction in progress. Here’s an example that works in a few methods to get the event category slug.

function jf_ee_return_event_tax_term_on_spco( $classes ){
    // get out if this isn't the reg checkout page
    if (! is_page( 'registration-checkout' ) ){
        return $classes;
    }

    $events = array();
    $checkout = EE_Registry::instance()->SSN->checkout();
    if ( $checkout instanceof EE_Checkout ) {
        $transaction = $checkout->transaction;
        if ( $transaction instanceof EE_Transaction ) {
            foreach ( $transaction->registrations() as $registration ) {
                if ( $registration instanceof EE_Registration ) {
                    $event = $registration->event();
                    if ( $event instanceof EE_Event ) {
                        $events[ $event->ID() ] = $event;
                        $classes[] = $event->first_event_category()->slug();
                    }
                }
            }   
        }
    }
    return $classes;
}

add_filter( 'body_class', 'jf_ee_return_event_tax_term_on_spco' );

You’ll note that there’s a method that’s used in there to grab the first event category in case there’s more than one category assigned to the event.


Josh

  • Support Staff

March 16, 2015 at 5:26 pm

A little update on the above code idea:

One more check would be good in case an event doesn’t have any categories assigned to it:

function jf_ee_return_event_tax_term_on_spco( $classes ){
    // get out if this isn't the reg checkout page
    if (! is_page( 'registration-checkout' ) ){
        return $classes;
    }

    $events = array();
    $checkout = EE_Registry::instance()->SSN->checkout();
    if ( $checkout instanceof EE_Checkout ) {
        $transaction = $checkout->transaction;
        if ( $transaction instanceof EE_Transaction ) {
            foreach ( $transaction->registrations() as $registration ) {
                if ( $registration instanceof EE_Registration ) {
                    $event = $registration->event();
                    if ( $event instanceof EE_Event ) {
                        $events[ $event->ID() ] = $event;
                        $category = $event->first_event_category();
                        if ( $category instanceof EE_Term ) {
                            $category_slug = $category->slug();
                            $classes[] = $category_slug;
                        }
                    }
                }
            }   
        }
    }
    return $classes;
}
Viewing 8 reply threads

The support post ‘Set EE4 categories as class’ is closed to new replies.

Have a question about this support post? Create a new support post in our support forums and include a link to this existing support post so we can help you.

Event Espresso