Skip to content

Instantly share code, notes, and snippets.

@cliffordp cliffordp/functions.php
Last active May 22, 2020

Embed
What would you like to do?
The Events Calendar: v2 Views: Include the next 3 upcoming events after each post unless it's a TEC post or archive.
<?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
You can’t perform that action at this time.