Help Desk

Display just category name

  • Posts: 28 Topics: 8
    | Permalink

    Hi there,

    This may have been answered already but if so, I can’t find it.

    I’m doing some custom theming and am looking to display the category name on a page (in this case, “Training”)

    I was able to use the following to output the venue and the time, and they worked fine:

    <?php echo sp_get_venue(); ?>
    <?php echo sp_get_address(); ?>

    Then I tried to use this to out put the event’s category:

    <?php echo tribe_get_event_categories(); ?>

    And it echoes back:

    <div>Event Category:</div> <ul class="tribe-event-categories"><li><a href="http://ourdomain.com/calendar/category/training/" rel="tag">Training</a></li></ul>

    How do I just get it to echo back the word “Training” and nothing else (without any of the div, ul, li, a or any other tags) like the venue and address above do? I just need the event category name to display, no html tags at all.

    Thanks for any help.

    Posts: 6531 Topics: 11
    | Permalink
    Correct Answer

    Hey David,

    Glad to help you out with this issue as well!

    Let me start by saying that sp_get_venue is deprecated and should use tribe_get_venue instead. Also use tribe_get_address instead of sp_get_address. Regarding the categories in text format you can add the snippet below to your theme’s (or child theme’s) functions.php file:

    if ( class_exists('Tribe__Events__Main') ){
    
    	/* get event category names in text format */
    	function tribe_get_text_categories ( $event_id = null ) {
    
    		if ( is_null( $event_id ) ) {
    			$event_id = get_the_ID();
    		}
    
    		$event_cats = '';
    
    		$term_list = wp_get_post_terms( $event_id, Tribe__Events__Main::TAXONOMY );
    
    		foreach( $term_list as $term_single ) {
    			$event_cats .= $term_single->name . ', ';
    		}
    
    		return rtrim($event_cats, ', ');
    
    	}
    
    }
    

    Once the code is in place you can call the function in the template: echo tribe_get_text_categories ();.

    Please give a try and let me know if it works for you,
    Best,
    Nico

    Posts: 28 Topics: 8
    | Permalink

    Works perfect, just what I was looking for. Thanks Nico.

    Posts: 6531 Topics: 11
    | Permalink

    Stocked to hear David, thanks for the heads-up 🙂

    I’ll go ahead and close out this thread, but if you need help with anything else please don’t hesitate to create a new one and we will be happy to assist you.

    Best,
    Nico

    | Permalink
    | Permalink