• Resolved Josh Robbs

    (@jwrobbs)


    Goal:

    Display 1 of 2 lines of HTML depending on the event category. It is being displayed by a shortcode added to the Single Event Page area of the Formating section. The shortcode has a simple if/then statement based on the category’s ID (term ID).

    Issue

    I can’t figure out how to get the term ID from within the shortcode. Not sure if the issue is my understanding of Events Manager, WordPress, or PHP. All attempts have resulted in errors or no output.

    The code (1 of many)

    $termlist = get_the_terms( get_the_ID(), 'event-category' );
    echo $termlist[0]->term_id;

    That causes an error.

    Am I approaching the problem wrong? Any input is appreciated.

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter Josh Robbs

    (@jwrobbs)

    Figured it out although I’m guessing there’s a better way.

    EM_Events::get( get_the_ID() ) used on an event returns an array with 3 nested objects and the term_id is in an array inside of all that.

    $eventlistobj = EM_Events::get( get_the_ID() ); //event list
    $eventobj = $eventlistobj[0]; // single event
    
    $eventcategories = $eventobj->categories; //category list
    $eventcategories_terms = $eventcategories->terms; // list of terms
    
    foreach($eventcategories_terms as $eventcategories_term){
    	$eventcategory = $eventcategories_term->term_id; // single term ID
    }

    This works because each event only has 1 category. Should it have multiple, I’m sure this would break.

    • This reply was modified 4 years, 10 months ago by Josh Robbs.
    • This reply was modified 4 years, 10 months ago by Josh Robbs.
    Plugin Support angelo_nwl

    (@angelo_nwl)

    you can try something like this

    
    $EM_Events = EM_Events::get( get_the_ID() );
    
    foreach( $EM_Events AS $EM_Event ){
    
      $EM_Categories = $EM_Event->get_categories();
     
      foreach( $EM_Categories AS $EM_Category){
        echo $EM_Category->term_id;
      }
    
    }
    
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Get an event’s event-category ID in shortcode’ is closed to new replies.