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();
@BrandenSandahl

This comment has been minimized.

Copy link

BrandenSandahl commented Nov 1, 2019

Hi! Thanks so much for making this.
Where would I add this class in? Would I override a file somewhere?

@barryhughes

This comment has been minimized.

Copy link
Owner Author

barryhughes commented Nov 4, 2019

Hi @BrandenSandahl! This was written over 1 year ago so I can't guarantee it will work on the very latest versions of The Events Calendar (probably it will, but it's not officially supported code so I don't want to make any promises).

However: the basic idea was that you could add this either to your theme's functions.php file or else to a custom plugin (including a mu-plugin). The template override (using the example in the code above, that might be front-page.php in the case of category slug tech-meetups) would live in the theme's root directory, ie:

wp-content/themes/my-theme/front-page.php

Though, that was just an example. Probably you would name it something else. I hope that helps!

@BrandenSandahl

This comment has been minimized.

Copy link

BrandenSandahl commented Nov 5, 2019

Thanks for the reply and help with that.

@BrandenSandahl

This comment has been minimized.

Copy link

BrandenSandahl commented Nov 6, 2019

I got this to work by doing what you mentioned, however it's not loading the CSS styles that typically go along with the event page. Not sure if you've ran into that and might have the fix for it?

Basically I need to filter out all "weekly" events from my normal event list and display them separately, so I've modified my main loop to not show weekly events and am using this class to point weekly events to a modified version of loop.php that only shows weekly events.

@barryhughes

This comment has been minimized.

Copy link
Owner Author

barryhughes commented Dec 2, 2019

Hey, sorry—I was a little behind on my GitHub/Gist notifications so didn't immediately see your follow-up.

I don't recall that being a problem, but every site is different and there are a lot of variables that could influence things (plus, the version of TEC this was written for is no longer current).

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.