cliffordp
3/8/2017 - 8:37 PM

Add category name(s) to event title

Add category name(s) to event title

<?php

/**
 * MT - TEC - Prepends ONLY CHILD category name(s) to the event titles
 *
 * from https://gist.github.com/cliffordp/5899add4d18ccda7087534de6a04e52e
 * Adapted from https://theeventscalendar.com/knowledgebase/add-category-prefix-to-event-titles/
 * for https://theeventscalendar.com/support/forums/topic/add-specific-category-prefix-to-event-titles-on-specific-views/#post-1251012
 *
 * @return string
 */
function tribe_events_title_include_cat ( $title, $id ) {

	$separator = ' &raquo; '; // HTML separator between categories and title
	$cats = get_the_terms( $id, 'tribe_events_cat' );
	$is_ajax = defined( 'DOING_AJAX' ) && DOING_AJAX;
	$is_truly_admin = is_admin() && ! $is_ajax;

	if ( tribe_is_event( $id ) && $cats && ! is_single() && ! $is_truly_admin ) {
		$cat_titles = array();

		foreach ( $cats as $i ) {

			if ( empty( $i->parent ) ) {
				continue;
			}

			$cat_titles[] = $i->name;
		}

		if ( ! empty( $cat_titles ) ) {
			$title = implode( ', ', $cat_titles ) . $separator . $title;
		}
	}

	return $title;
}
add_filter( 'the_title', 'tribe_events_title_include_cat', 100, 2 );