Displaying the Category name?

Home Forums Calendar Products Events Calendar PRO Displaying the Category name?

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #1216781
    Kay
    Participant

    Hi – I am creating a custom theme and just need to be able to list the category name(s) – Something similar to

    get_the_category()

    I did find that

    tribe_get_event_taxonomy()

    does output the category names, but it does it as an html string. What I’m looking for is either an array of categories, or just the name of the first category in the array (no HTML).

    Thanks!

    #1217046
    George
    Participant

    Hey @Jamesstaub,

    While our ability to assist with custom coding-related questions is limited, I hope I can point you in the right direction on this and help you move forward with your goals.

    I would recommend using a WordPress core function here directly, instead of our plugins’ own internal helper functions. Specifically I would recommend the wp_get_object_terms() function, which you can read about here → https://codex.wordpress.org/Function_Reference/wp_get_object_terms

    This function lets you get a lot more control over the specific term data you need or want. So you could do something like this, for example:


    $event_cats_args = array(
    'orderby' => 'name',
    'order' => 'ASC',
    'fields' => 'all'
    );

    $event_cats = wp_get_object_terms( $event_id, array( 'tribe_events_cat' ), $event_cats_args );

    ↑ With this code, the $event_cats should contain term data for all of the terms attached to the specified event.

    You can var_dump() the $event_cats variable and read the WP Codex guide I shared above to learn more about all of the term data you have and how to use it. Here’s an example that just echos a list of category names on an event, with no HTML or links, etc.:


    // Assume $event_id exists from somewhere above in this hypothetical code.

    $event_cats = wp_get_object_terms( $event_id, array( 'tribe_events_cat' ) );

    if ( empty( $event_cats ) ) {
    return;
    }

    if ( is_wp_error( $event_cats ) || ! is_array( $event_cats ) ) {
    return;
    }

    // Echo the names of the categories attached to this event.
    foreach( $event_cats as $cat ) {
    echo esc_html( $cat->name ) . '<br>';
    }

    You’ll have to take the reins from here, but I hope this helps! 😀

    — George

    #1228270
    Support Droid
    Keymaster

    Hey there! This thread has been pretty quiet for the last three weeks, so we’re going to go ahead and close it to avoid confusion with other topics. If you’re still looking for help with this, please do open a new thread, reference this one and we’d be more than happy to continue the conversation over there.

    Thanks so much!
    The Events Calendar Support Team

Viewing 3 posts - 1 through 3 (of 3 total)
  • The topic ‘Displaying the Category name?’ is closed to new replies.