Select different templates for different event categories

By default, all event views are rendered using the same template (defined in the Events ‣ Settings ‣ Display screen). With some creativity, though, it's possible to tell WordPress to use a completely different template.

In this recipe we are going to pick a different template based on the requested event category. For example, if we have a category called WP Meetups (the slug for which is 'wp-meetups') and someone visits example.com/events/category/wp-meetups we will use the theme's search.php template to render the view (in reality that probably isn't the best choice, but for demo purposes it's fine).

Of course, we're not restricted to using categories: we can test against various properties, such as whether or not past events are being viewed and so on. Categories are just a convenient way of illustrating the basic approach. Here's the code:

<?php
/**
 * Use custom templates for event views when specific categories
 * have been requested.
 */
class Event_Category_Templates {
	/**
	 * This will be true if an event view has been requested.
	 *
	 * @var bool
	 */
	private static $is_tec_tpl = false;

	/**
	 * List of category slugs and the templates we want to use for
	 * each.
	 *
	 * @var array
	 */
	private static $category_templates = [
		// Format: 'category-slug' => 'template-filename.php'
		'wp-meetups'   => 'search.php',
		'tech-meetups' => 'front-page.php'
	];

	/**
	 * Add our hooks...
	 */
	static function setup() {
		add_action( 'tribe_tec_template_chooser', [ __CLASS__, 'listen_for_tec_template' ] );
		add_filter( 'template_include', [ __CLASS__, 'set_template' ], 100 );
	}

	/**
	 * This will only be called if The Events Calendar is setting up
	 * one of its views, so we can take this opportunity to set a flag
	 * indicating an event view has been requested.
	 */
	static function listen_for_tec_template() {
		self::$is_tec_tpl = true;
	}

	/**
	 * Test to see if this is an event view and if we have an alternative template
	 * we wish to use.
	 *
	 * @param $template
	 *
	 * @return string
	 */
	static function set_template( $template ) {
		if ( self::$is_tec_tpl && $alternative = self::alternative_template() ) {
			$template = $alternative;
		}

		return $template;
	}

	/**
	 * Return a valid alternative template or an empty string if one
	 * cannot be found.
	 *
	 * @return string
	 */
	static function alternative_template() {
		$template = '';

		if ( isset( self::$category_templates[ get_query_var( 'term' ) ] ) ) {
			$template = locate_template( 
				self::$category_templates[ get_query_var( 'term' ) ] 
			);
		}

		return $template;
	}
}

Event_Category_Templates::setup();
(View code on Gist)

Notes:

  • Originally written in October 2018
  • Tested with The Events Calendar 4.6.25

As with all of our recipes, please note that we share this in the hope it will be useful but without any guarantees or commitments. If you wish to use it, it is your responsibility to test it first of all and adapt it to your needs (or find someone who can do so on your behalf). We are unable to provide further support in relation to this recipe.