Last active
August 20, 2025 04:45
-
-
Save chikaibeneme/b70798469a839d8e692b163d1ce7ffc0 to your computer and use it in GitHub Desktop.
This snippet adds event tags to the list view
This file contains hidden or 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
| /** | |
| * Auto-create template override for event tags in list view | |
| */ | |
| // Hook into init to create the template override | |
| add_action( 'init', 'create_event_tags_template_override' ); | |
| function create_event_tags_template_override() { | |
| // Only run once | |
| if ( get_option( 'event_tags_template_created' ) ) { | |
| return; | |
| } | |
| $theme_dir = get_stylesheet_directory(); | |
| $template_dir = $theme_dir . '/tribe/events/v2/list'; | |
| $template_file = $template_dir . '/event.php'; | |
| // Create directories if they don't exist | |
| if ( ! file_exists( $template_dir ) ) { | |
| wp_mkdir_p( $template_dir ); | |
| } | |
| // Create the template file | |
| if ( ! file_exists( $template_file ) ) { | |
| $template_content = '<?php | |
| /** | |
| * View: List Event with Tags | |
| * | |
| * Auto-generated template override for event tags | |
| */ | |
| $container_classes = [ \'tribe-common-g-row\', \'tribe-events-calendar-list__event-row\' ]; | |
| $container_classes[\'tribe-events-calendar-list__event-row--featured\'] = $event->featured; | |
| $event_classes = tribe_get_post_class( [ \'tribe-events-calendar-list__event\', \'tribe-common-g-row\', \'tribe-common-g-row--gutters\' ], $event->ID ); | |
| ?> | |
| <div <?php tribe_classes( $container_classes ); ?>> | |
| <?php $this->template( \'list/event/date-tag\', [ \'event\' => $event ] ); ?> | |
| <div class="tribe-events-calendar-list__event-wrapper tribe-common-g-col"> | |
| <article <?php tribe_classes( $event_classes ) ?>> | |
| <?php $this->template( \'list/event/featured-image\', [ \'event\' => $event ] ); ?> | |
| <div class="tribe-events-calendar-list__event-details tribe-common-g-col"> | |
| <header class="tribe-events-calendar-list__event-header"> | |
| <?php $this->template( \'list/event/date\', [ \'event\' => $event ] ); ?> | |
| <?php $this->template( \'list/event/title\', [ \'event\' => $event ] ); ?> | |
| <?php $this->template( \'list/event/venue\', [ \'event\' => $event ] ); ?> | |
| </header> | |
| <?php $this->template( \'list/event/description\', [ \'event\' => $event ] ); ?> | |
| <?php $this->template( \'list/event/cost\', [ \'event\' => $event ] ); ?> | |
| <?php | |
| // Add event tags here | |
| $tags = get_the_terms( $event->ID, \'post_tag\' ); | |
| if ( ! empty( $tags ) && ! is_wp_error( $tags ) ) { | |
| $tags = array_values( array_filter( $tags, function( $term ) { | |
| return $term instanceof WP_Term; | |
| } ) ); | |
| if ( ! empty( $tags ) ) { | |
| $tag_links = []; | |
| foreach ( $tags as $tag ) { | |
| $link = tribe_events_get_url( [ | |
| \'tag\' => $tag->slug, | |
| \'post_type\' => \'tribe_events\', | |
| \'eventDisplay\' => \'default\' | |
| ] ); | |
| if ( ! is_wp_error( $link ) ) { | |
| $tag_links[] = \'<a href="\' . esc_url( $link ) . \'" rel="tag" class="tribe-event-tag-link">\' . esc_html( $tag->name ) . \'</a>\'; | |
| } | |
| } | |
| if ( ! empty( $tag_links ) ) { | |
| echo \'<div class="tribe-events-calendar-list__event-tags">\'; | |
| echo \'<span class="tribe-event-tags-label">\' . esc_html__( \'Tags:\', \'the-events-calendar\' ) . \'</span> \'; | |
| echo \'<span class="tribe-event-tags">\' . implode( \', \', $tag_links ) . \'</span>\'; | |
| echo \'</div>\'; | |
| } | |
| } | |
| } | |
| ?> | |
| </div> | |
| </article> | |
| </div> | |
| </div>'; | |
| file_put_contents( $template_file, $template_content ); | |
| // Mark as created | |
| update_option( 'event_tags_template_created', true ); | |
| } | |
| } | |
| // Add CSS styling | |
| add_action( 'wp_head', 'add_event_tags_list_view_styles' ); | |
| function add_event_tags_list_view_styles() { | |
| if ( tribe_is_event_query() && tribe_is_list_view() ) { | |
| ?> | |
| <style> | |
| .tribe-events-calendar-list__event-tags { | |
| margin-top: 8px; | |
| margin-bottom: 8px; | |
| font-size: 0.85em; | |
| color: #666; | |
| line-height: 1.4; | |
| } | |
| .tribe-event-tags-label { | |
| font-weight: 600; | |
| margin-right: 5px; | |
| color: #333; | |
| } | |
| .tribe-event-tag-link { | |
| color: #0073aa; | |
| text-decoration: none; | |
| transition: color 0.2s ease; | |
| background: #f7f7f7; | |
| padding: 2px 6px; | |
| border-radius: 3px; | |
| margin-right: 3px; | |
| display: inline-block; | |
| margin-bottom: 3px; | |
| } | |
| .tribe-event-tag-link:hover { | |
| color: #005a87; | |
| background: #e7f3ff; | |
| text-decoration: none; | |
| } | |
| .tribe-event-tags { | |
| font-style: italic; | |
| } | |
| </style> | |
| <?php | |
| } | |
| } | |
| ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment