The Events Calendar Shortcode & Block Shortcode

Below, you’ll find a detailed guide on how to add the The Events Calendar Shortcode & Block Shortcode to your WordPress website, including its parameters, examples, and PHP function code. Additionally, we’ll assist you with common issues that might cause the The Events Calendar Shortcode & Block Plugin shortcode not to show or not to work correctly.

Before starting, here is an overview of the The Events Calendar Shortcode & Block Plugin and the shortcodes it provides:

Plugin Icon
The Events Calendar Shortcode & Block

"The Events Calendar Shortcode & Block is a dynamic WordPress plugin that provides shortcodes & blocks to list your events, making event management a breeze."

★★★★✩ (64) Active Installs: 20000+ Tested with: 6.3.2 PHP Version: 7.4
Included Shortcodes:
  • [ecs-list-events]

The Events Calendar Shortcode & Block [ecs-list-events] Shortcode

The-events-calendar-shortcode plugin’s shortcode, ‘ecs-list-events’, fetches and displays a list of events. It checks if the events calendar plugin method exists and retrieves the events. It allows customization of events listing through various attributes like ‘cat’ for categories, ‘month’ for specific month, ‘limit’ for number of events to display, and ‘eventdetails’ to show event details. It also allows to sort events by ‘startdate’ or ‘enddate’, and order them in ascending or descending order. It also enables to filter events by past or upcoming, by specific author, and by event taxonomy. It provides options to display or hide event thumbnail, excerpt, venue, and a link to view all events. In case no events are found, it displays a custom message.

Shortcode: [ecs-list-events]

Parameters

Here is a list of all possible ecs-list-events shortcode parameters and attributes:

  • cat – specifies the event category
  • month – filters events by month
  • limit – sets the maximum number of events to display
  • eventdetails – controls the display of event details
  • time – filters events by past or upcoming time
  • past – filters events by past time
  • venue – controls the display of event venue
  • author – filters events by author
  • schema – controls the output of schema markup
  • message – sets the message to display when no events are found
  • key – sets the event date filter
  • order – sets the order of the events
  • orderby – sets the property to order events by
  • viewall – controls the display of the view all link
  • excerpt – controls the display of event excerpts
  • thumb – controls the display of event thumbnails
  • thumbsize – sets the size of event thumbnails
  • thumbwidth – sets the width of event thumbnails
  • thumbheight – sets the height of event thumbnails
  • contentorder – sets the order of event content elements
  • event_tax – filters events by taxonomy

Examples and Usage

Basic example – Displaying a list of events with default settings.

[ecs-list-events]

Advanced examples

Displaying a list of events from a specific category, limiting the number of events to 10, and ordering them by their start date in descending order.

[ecs-list-events cat='music' limit='10' order='DESC' orderby='startdate']

Displaying a list of events from multiple categories, showing only past events, including the venue details, and displaying a custom message when there are no events.

[ecs-list-events cat='music,art' past='true' venue='true' message='Sorry, there are no past %1$s.']

Displaying a list of events for the next month, including the event thumbnail and excerpt, and showing a ‘View All’ link at the end of the list.

[ecs-list-events month='next' thumb='true' excerpt='true' viewall='true']

PHP Function Code

In case you have difficulties debugging what causing issues with [ecs-list-events] shortcode, check below the related PHP functions code.

Shortcode line:

add_shortcode( 'ecs-list-events', [ $this, 'ecs_fetch_events' ] );

Shortcode PHP function:

function ecs_fetch_events( $atts ) {
            /*
             * Check if events calendar plugin method exists
             */
            if ( !function_exists( 'tribe_get_events' ) ) {
                return '';
            }

            global $post, $ecs_last_event_count;
            $output = '';

            $atts = shortcode_atts( apply_filters( 'ecs_shortcode_atts', [
                'cat' => '',
                'month' => '',
                'limit' => 5,
                'eventdetails' => 'true',
                'time' => null,
                'past' => null,
                'venue' => 'false',
                'author' => null,
                'schema' => 'true',
                'message' => 'There are no upcoming %1$s.',
                'key' => 'End Date',
                'order' => 'ASC',
                'orderby' => 'startdate',
                'viewall' => 'false',
                'excerpt' => 'false',
                'thumb' => 'false',
                'thumbsize' => '',
                'thumbwidth' => '',
                'thumbheight' => '',
                'contentorder' => apply_filters( 'ecs_default_contentorder', 'title, thumbnail, excerpt, date, venue', $atts ),
                'event_tax' => '',
            ], $atts ), $atts, 'ecs-list-events' );

            // Category
            if ( $atts['cat'] ) {
                if ( strpos( $atts['cat'], ',' ) !== false ) {
                    $atts['cats'] = explode( ',', $atts['cat'] );
                    $atts['cats'] = array_map( 'trim', $atts['cats'] );
                } else {
                    $atts['cats'] = [ trim( $atts['cat'] ) ];
                }

                $atts['event_tax'] = [
                    'relation' => 'OR',
                ];

                foreach ( $atts['cats'] as $cat ) {
                    $atts['event_tax'][] = [
                        'relation' => 'OR',
                        [
                            'taxonomy' => 'tribe_events_cat',
                            'field' => 'name',
                            'terms' => $cat,
                        ],
                        [
                            'taxonomy' => 'tribe_events_cat',
                            'field' => 'slug',
                            'terms' => $cat,
                        ],
                    ];
                }
            }

            // Past Event
            $meta_date_compare = '>=';
            $meta_date_date = current_time( 'Y-m-d H:i:s' );

            if ( $atts['time'] == 'past' || !empty( $atts['past'] ) ) {
                $meta_date_compare = '<';
            }

            // Key, used in filtering events by date
            if ( str_replace( ' ', '', trim( strtolower( $atts['key'] ) ) ) == 'startdate' ) {
                $atts['key'] = '_EventStartDate';
            } else {
                $atts['key'] = '_EventEndDate';
            }

            // Orderby
            if ( str_replace( ' ', '', trim( strtolower( $atts['orderby'] ) ) ) == 'enddate' ) {
                $atts['orderby'] = '_EventEndDate';
            } elseif ( trim( strtolower( $atts['orderby'] ) ) == 'title' ) {
                $atts['orderby'] = 'title';
            } else {
                $atts['orderby'] = '_EventStartDate';
            }

            // Date
            $atts['meta_date'] = [
                [
                    'key' => $atts['key'],
                    'value' => $meta_date_date,
                    'compare' => $meta_date_compare,
                    'type' => 'DATETIME',
                ],
            ];

            // Specific Month
            if ( 'current' == $atts['month'] ) {
                $atts['month'] = current_time( 'Y-m' );
            }

            if ( 'next' == $atts['month'] ) {
                $now = current_datetime();
                $atts['month'] = $now->modify( 'first day of this month' )->modify( 'first day of next month' )->format( 'Y-m' );
            }

            if ( $atts['month'] ) {
                $month_array = explode( '-', $atts['month'] );

                $month_yearstr = $month_array[0];
                $month_monthstr = $month_array[1];
                $month_startdate = date( 'Y-m-d', strtotime( $month_yearstr . '-' . $month_monthstr . '-01' ) );
                $month_enddate = date( 'Y-m-01', strtotime( '+1 month', strtotime( $month_startdate ) ) );

                $atts['meta_date'] = [
                    'relation' => 'AND',
                    [
                        'key' => $atts['key'],
                        'value' => $month_startdate,
                        'compare' => '>=',
                        'type' => 'DATETIME',
                    ],
                    [
                        'key' => $atts['key'],
                        'value' => $month_enddate,
                        'compare' => '<',
                        'type' => 'DATETIME',
                    ],
                ];
            }

            $atts = apply_filters( 'ecs_atts_pre_query', $atts, $meta_date_date, $meta_date_compare );
            $args = apply_filters( 'ecs_get_events_args', [
                'post_status' => 'publish',
                'hide_upcoming' => true,
                'posts_per_page' => $atts['limit'],
                'tax_query' => $atts['event_tax'],
                // Likely want to revamp this logic and separate the ordering from the date filtering
                'meta_key' => ( ( trim( $atts['orderby'] ) and 'title' != $atts['orderby'] ) ? $atts['orderby'] : $atts['key'] ),
                'orderby' => ( $atts['orderby'] == 'title' ? 'title' : 'event_date' ),
                'author' => $atts['author'],
                'order' => $atts['order'],
                'meta_query' => apply_filters( 'ecs_get_meta_query', [ $atts['meta_date'] ], $atts, $meta_date_date, $meta_date_compare ),
            ], $atts, $meta_date_date, $meta_date_compare );

            $posts = tribe_get_events( $args );
            $posts = apply_filters( 'ecs_filter_events_after_get', $posts, $atts );

            $ecs_last_event_count = 0;

            if ( $posts or apply_filters( 'ecs_always_show', false, $atts ) ) {
                $output = apply_filters( 'ecs_beginning_output', $output, $posts, $atts );
                $output .= apply_filters( 'ecs_start_tag', '<ul class="ecs-event-list">', $atts, count( (array) $posts ) );
                $atts['contentorder'] = explode( ',', $atts['contentorder'] );

                foreach ( (array) $posts as $post_index => $post ) {
                    setup_postdata( $post );
                    $event_output = '';

                    if ( apply_filters( 'ecs_skip_event', false, $atts, $post ) ) {
                        continue;
                    }
                    $ecs_last_event_count++;
                    $category_slugs = [];
                    $category_list = get_the_terms( $post, 'tribe_events_cat' );
                    $featured_class = ( get_post_meta( get_the_ID(), '_tribe_featured', true ) ? ' ecs-featured-event' : '' );

                    if ( is_array( $category_list ) ) {
                        foreach ( (array) $category_list as $category ) {
                            $category_slugs[] = ' ' . $category->slug . '_ecs_category';
                        }
                    }
                    $event_output .= apply_filters( 'ecs_event_start_tag', '<li class="ecs-event' . implode( '', $category_slugs ) . $featured_class . apply_filters( 'ecs_event_classes', '', $atts, $post ) . '">', $atts, $post );

                    // Put Values into $event_output
                    foreach ( apply_filters( 'ecs_event_contentorder', $atts['contentorder'], $atts, $post ) as $contentorder ) {
                        switch ( trim( $contentorder ) ) {
                            case 'title':
                                $event_output .= apply_filters( 'ecs_event_title_tag_start', '<h4 class="entry-title summary">', $atts, $post ) .
                                                 apply_filters( 'ecs_event_list_title_link_start', '<a href="' . tribe_get_event_link() . '" rel="bookmark">', $atts, $post ) . apply_filters( 'ecs_event_list_title', get_the_title(), $atts, $post ) . apply_filters( 'ecs_event_list_title_link_end', '</a>', $atts, $post ) .
                                                 apply_filters( 'ecs_event_title_tag_end', '</h4>', $atts, $post );
                                break;

                            case 'thumbnail':
                                if ( self::isValid( $atts['thumb'] ) ) {
                                    $thumbWidth = is_numeric( $atts['thumbwidth'] ) ? $atts['thumbwidth'] : '';
                                    $thumbHeight = is_numeric( $atts['thumbheight'] ) ? $atts['thumbheight'] : '';

                                    if ( !empty( $thumbWidth ) && !empty( $thumbHeight ) ) {
                                        $event_output .= apply_filters( 'ecs_event_thumbnail', get_the_post_thumbnail( get_the_ID(), apply_filters( 'ecs_event_thumbnail_size', [ $thumbWidth, $thumbHeight ], $atts, $post ) ), $atts, $post );
                                    } else {
                                        if ( $thumb = get_the_post_thumbnail( get_the_ID(), apply_filters( 'ecs_event_thumbnail_size', ( trim( $atts['thumbsize'] ) ? trim( $atts['thumbsize'] ) : 'medium' ), $atts, $post ) ) ) {
                                            $event_output .= apply_filters( 'ecs_event_thumbnail_link_start', '<a href="' . tribe_get_event_link() . '">', $atts, $post );
                                            $event_output .= apply_filters( 'ecs_event_thumbnail', $thumb, $atts, $post );
                                            $event_output .= apply_filters( 'ecs_event_thumbnail_link_end', '</a>', $atts, $post );
                                        }
                                    }
                                }
                                break;

                            case 'excerpt':
                                if ( self::isValid( $atts['excerpt'] ) ) {
                                    $excerptLength = is_numeric( $atts['excerpt'] ) ? intval( $atts['excerpt'] ) : 100;
                                    $event_output .= apply_filters( 'ecs_event_excerpt_tag_start', '<p class="ecs-excerpt">', $atts, $post ) .
                                                     apply_filters( 'ecs_event_excerpt', self::get_excerpt( $excerptLength ), $atts, $post, $excerptLength ) .
                                                     apply_filters( 'ecs_event_excerpt_tag_end', '</p>', $atts, $post );
                                }
                                break;

                            case 'date':
                                if ( self::isValid( $atts['eventdetails'] ) ) {
                                    $event_output .= apply_filters( 'ecs_event_date_tag_start', '<span class="duration time">', $atts, $post ) .
                                                     apply_filters( 'ecs_event_list_details', tribe_events_event_schedule_details(), $atts, $post ) .
                                                     apply_filters( 'ecs_event_date_tag_end', '</span>', $atts, $post );
                                }
                                break;

                            case 'venue':
                                if ( self::isValid( $atts['venue'] ) and function_exists( 'tribe_has_venue' ) and tribe_has_venue() ) {
                                    $event_output .= apply_filters( 'ecs_event_venue_tag_start', '<span class="duration venue">', $atts, $post ) .
                                                     apply_filters( 'ecs_event_venue_at_tag_start', '<em> ', $atts, $post ) .
                                                     apply_filters( 'ecs_event_venue_at_text', __( 'at', 'the-events-calendar-shortcode' ), $atts, $post ) .
                                                     apply_filters( 'ecs_event_venue_at_tag_end', ' </em>', $atts, $post ) .
                                                     apply_filters( 'ecs_event_list_venue', tribe_get_venue(), $atts, $post ) .
                                                     apply_filters( 'ecs_event_venue_tag_end', '</span>', $atts, $post );
                                }
                                break;

                            case 'date_thumb':
                                if ( self::isValid( $atts['eventdetails'] ) ) {
                                    $event_output .= apply_filters( 'ecs_event_date_thumb', '<div class="date_thumb"><div class="month">' . tribe_get_start_date( null, false, 'M' ) . '</div><div class="day">' . tribe_get_start_date( null, false, 'j' ) . '</div></div>', $atts, $post );
                                }
                                break;
                            default:
                                $event_output .= apply_filters( 'ecs_event_list_output_custom_' . strtolower( trim( $contentorder ) ), '', $atts, $post );
                        }
                    }
                    $event_output .= apply_filters( 'ecs_event_end_tag', '</li>', $atts, $post );
                    $output .= apply_filters( 'ecs_single_event_output', $event_output, $atts, $post, $post_index, $posts );
                }
                $output .= apply_filters( 'ecs_end_tag', '</ul>', $atts );
                $output = apply_filters( 'ecs_ending_output', $output, $posts, $atts );

                if ( self::isValid( $atts['viewall'] ) ) {
                    $output .= apply_filters( 'ecs_view_all_events_tag_start', '<div class="ecs-view-all-events"><span class="ecs-all-events">', $atts ) .
                               '<a href="' . apply_filters( 'ecs_event_list_viewall_link', tribe_get_events_link(), $atts ) . '" rel="bookmark">' . apply_filters( 'ecs_view_all_events_text', sprintf( __( 'View all %s', 'the-events-calendar' ), tribe_get_event_label_plural_lowercase() ), $atts ) . '</a>';
                    $output .= apply_filters( 'ecs_view_all_events_tag_end', '</span></div>' );
                }
            } else {
                $output .= '<div class="ecs-no-events">' . apply_filters( 'ecs_no_events_found_message', sprintf( _x( $atts['message'], 'A message to indicate there are no upcoming events.', 'the-events-calendar' ), tribe_get_event_label_plural_lowercase() ), $atts ) . '</div>';
            }

            wp_reset_postdata();

            return $output;
        }

Code file location:

the-events-calendar-shortcode/the-events-calendar-shortcode/the-events-calendar-shortcode.php

Conclusion

Now that you’ve learned how to embed the The Events Calendar Shortcode & Block Plugin shortcode, understood the parameters, and seen code examples, it’s easy to use and debug any issue that might cause it to ‘not work’. If you still have difficulties with it, don’t hesitate to leave a comment below.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *