Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
An example of using a different theme template for different event categories.
<?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();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.