Support

Home Forums Event Espresso Premium Working with Event Espresso Event ID

Working with Event Espresso Event ID

Posted: September 9, 2023 at 2:25 pm


Jonathan Kane

September 9, 2023 at 2:25 pm

Hi,

I am trying to understand how to call an Event Category ID or Event Category Name into an if statement as part of a WP custom template.

Normally for a non-custom post type, I might do something like this to change the background of a banner based on category.

How can I do this within this custom event taxonomy? I am sure it’s a simple answer that I just didn’t find.

Thanks,
Jon


<div class="es_single_event_banner_image">
                      <?php if ( in_category( 'CatName###' )) : ?>
assets/images/banner-bg-A.png">
<?php elseif ( in_category('###')) : ?>
assets/images/banner-bg_B.png">
<?php else : ?>
assets/images/banner-bg-C.png">
<?php endif; ?>

</div>


Jonathan Kane

September 9, 2023 at 2:34 pm

My preference would be to do this by Event Category ID.

Basically:

If Event Cat ID is 91, do this… if Event Cat ID is 92, do that


Tony

  • Support Staff

September 11, 2023 at 3:47 am

Hi Jon,

So, within WordPress you have ‘Taxonomies’ which are basically just a way to organize content. You have default taxonomies built into WordPress and those are ‘Categories’, ‘Tags’ and ‘Post Formats’ and when you set a category on a standard WordPress post it will be using the ‘Category’ taxonomy, each different category you add to that is a ‘term’ within that taxonomy.

So a post ‘category’, is a term within the category taxonomy.

An EE event category is a term within the espresso_event_categories custom taxonomy.

Your opening posts suggest you know most of the above, so why am I telling you this? Because in_category() just checks for terms within the default ‘Category’ taxonomy.

Looking through in_category() it just uses has_category() (we are almost there I promise!), which is basically just this:

has_term( $category, 'category', $post );

So that’s checking for ‘$categrorywithin the 'category' taxonomy for the$postobject passed to it and hopefully from that you can see how you can use that to get what you need, even more so when you know the EE event custom taxonomy isespresso_event_categories`.

So to use your example above has_term( 'CatName###', 'espresso_event_categories', $post );

And yes, you can pass the term_id in place of the name there.

—-

Side note, there always multiple ways to do things and if you have the above, I assume you have the EE_Event object (If you have the WP_Post object for an EE event you should also have the EE_Event object assigned to that or again there’s multiple ways of getting that ;))

If you want to look at another way of doing this you could use:

$event_categories = $event->get_all_event_categories()

(Assuming the EE_Event object is within $event)

The support post ‘Working with Event Espresso Event ID’ 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