Change “All Events” backlink on The Events Calendar event page

This is useful if you have The Events Calendar Pro plugin and use the shortcode to display lists of different event categories on different pages. This code changes the link appearing at the top of the events page. It is especially useful if you have excluded the event category from your main events list.

function change_events_link( $link ) {

	global $post;

	if ( isset ( $post ) && $terms = get_the_terms( $post->ID, 'tribe_events_cat' ) ) {

		foreach ( $terms as $term ) {

			$categories[] = $term->slug;
		}
	
		if ( in_array( 'category-slug', $categories ) ) {
	
			$link = site_url( 'category-page-slug' );
		}
	
		elseif ( in_array( 'category-2-slug', $categories ) ) {
		
			$link = site_url( 'category-2-page-slug' );
		}
		return $link;
	}
	else {
		return $link;
	}
}
add_filter( 'tribe_get_events_link', 'change_events_link' );