Support

Home Forums Event Espresso Premium EE4 and ACF: show content based on espresso event category

EE4 and ACF: show content based on espresso event category

Posted: January 27, 2020 at 4:54 am


efransen

January 27, 2020 at 4:54 am

Hi,
Due to the fact that we are so happy about EE4, we will be using EE4 all over the website soon. Using ACF and EE4, we would like to show different content depending on the EE-category (‘summerschool’ or ‘courses-seminars’) of the event. I have been trying adding these code u-into the functions.php, but no luck so far…

if (($post->post_type == 'espresso_events') && has_taxonomy('summerschool')){
OR
if ( has_term( 'summerschool', 'espresso_event_categories' ) ) {
OR
if( in_category('summerschool')){

Any suggestions here? Thanks in advanced!


Josh

  • Support Staff

January 27, 2020 at 7:50 am

Hi,

The following is the correct syntax for checking 1) if it’s an Event Espresso event post type and 2) if it has a matching event category:

<?php if ($post->post_type == 'espresso_events' &&
 has_term('summerschool', 'espresso_event_categories' ) ){
	// do something;
}
?>

and if your code is only being injected onto EE event posts, then you can simplify the conditional:

<?php if (has_term('summerschool', 'espresso_event_categories' ) ){
	// do something;
}
?>

Since you mention you’re adding the code into a functions.php file, may I ask how is the code being triggered? Is the code within a function that’s hooked to an action or filter hook?


efransen

January 28, 2020 at 12:57 am

This reply has been marked as private.


Josh

  • Support Staff

January 28, 2020 at 4:08 am

Hi,

The reason the conditional doesn’t work is because it was placed before the function starts. You’ll need to move the conditional so it’s within or inside the function.

Also, there’s no need to include the ACF plugin file. It’s already activated. It would be a good idea to do a check
e.g, if( function_exists()){}
For the_field or another ACF function wherever you’re using them.


efransen

January 28, 2020 at 4:36 am

This reply has been marked as private.


Tony

  • Support Staff

January 28, 2020 at 5:39 am

Might that be the reason?

No, the licensing has no effect on Event Espresso’s functionality other than for updates and support.

The functions you are trying to hook into AHEE_event_details_after_event_date will never be called as they are right now because you are adding your callback function to the hook, within the function you want to callback…. meaning it never added to the callback list in the first place.

With action hooks you do something like:


function my_callback_function() {
    // This is some function that does something
    // You want this function to 'hook into' a hook 'fired' on the page.
}
add_action('{hook-name-you-want-to-hook-into}', 'my_callback_function');

Right now you basically have this:


function my_callback_function() {
    // This is some function that does something
    // You want this function to 'hook into' a hook 'fired' on the page.
    add_action('{hook-name-you-want-to-hook-into}', 'my_callback_function');
}

Which means it’s not hooking into anything as nothing calls my_callback_function. Move that out of the function itself, just below the } will work.

I highly recommend you take a look at how to use hooks, this helped me get to grasps with them a while back:

https://www.smashingmagazine.com/2011/10/definitive-guide-wordpress-hooks/

There are likely others now.

  • This reply was modified 4 years, 2 months ago by  Tony. Reason: Code formatting


Josh

  • Support Staff

January 28, 2020 at 5:46 am

No the license key is definitely not the reason. You will not be able to run updates on more than one site with the key activated. That’s the effect of having that key on two sites.


efransen

January 28, 2020 at 6:07 am

Thanks for all the help, Josh. You made my day! I will certainly look into the link on wp-hooks.

The support post ‘EE4 and ACF: show content based on espresso event category’ 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