Last active
August 3, 2021 09:58
-
-
Save cliffordp/9994cf05f6475922f451eaeacef2cd3c to your computer and use it in GitHub Desktop.
The Events Calendar: v2 Views: Include the next 3 upcoming events after each post unless it's a TEC post or archive.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* The Events Calendar: v2 Views: Include the next 3 upcoming events after each post unless it's a TEC post or archive. | |
* | |
* Tested working with TEC v5.1.1. | |
* Outputs a portion of v2's List View's HTML rendering without full styles/scripts. | |
* But this isn't the best way to use v2's views. For a lot of scenarios, it's better to just create your own new view. | |
* | |
* @link https://gist.github.com/cliffordp/9994cf05f6475922f451eaeacef2cd3c This snippet. | |
* @link https://github.com/mt-support/tribe-ext-events-happening-now A more recommended way to customize v2 views or craft your own. | |
* | |
* @param string $content | |
* | |
* @return string | |
*/ | |
function cliff_add_tec_v2_list_view_on_post_loop( $content ) { | |
if ( | |
! function_exists( 'tribe_events_views_v2_is_enabled' ) | |
|| empty( tribe_events_views_v2_is_enabled() ) | |
|| ! class_exists( '\Tribe\Events\Views\V2\Views\List_View' ) | |
|| ! function_exists( 'tribe_is_event_query' ) | |
|| tribe_is_event_query() | |
) { | |
return $content; | |
} | |
// Get next 3 upcoming events. | |
$events = tribe_events() | |
->where( 'starts_after', 'now' ) | |
->take( 3 ); | |
// Get the List View template class. | |
$template = new \Tribe\Events\Views\V2\Template( | |
new \Tribe\Events\Views\V2\Views\List_View() | |
); | |
// Build the output. | |
// Based on the-events-calendar/src/views/v2/list.php | |
$list_html = ''; | |
foreach ( $events as $event ) { | |
$list_html .= $template->template( | |
'list/event', | |
[ 'event' => $event ], | |
false | |
); | |
} | |
if ( $list_html ) { | |
$list_html = sprintf( '<div class="tribe-events-calendar-list">%s</div>', $list_html ); | |
} | |
return $content . $list_html; | |
} | |
add_filter( 'the_content', 'cliff_add_tec_v2_list_view_on_post_loop' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment