Skip to main content

Turning Your Events List Into Tables

An events list is built from three formats: a header printed once, an item format repeated for each event, and a footer printed once. Put the opening of a table in the header, one row in the item format and the closing tags in the footer, and the list becomes a table. No PHP is needed.

What you will build​

A table with a row per event and columns for date, event and location.

Before you start

To change the default list for the whole site you need Advanced Mode enabled at the top of Events then Settings then Formatting, which makes the format boxes editable. To make a table in one place only, use the shortcode method and leave the settings alone.

Change the default events list​

  1. Go to Events then Settings then Formatting then Events.

  2. In Default event list format header, enter the opening of the table:

    <table class="my-events-table">
    <thead>
    <tr>
    <th scope="col">Date</th>
    <th scope="col">Event</th>
    <th scope="col">Location</th>
    </tr>
    </thead>
    <tbody>
  3. In Default event list format, enter one row:

    <tr>
    <td>#_EVENTDATES<br />#_EVENTTIMES</td>
    <td>#_EVENTLINK<br />#_EVENTEXCERPT{20,...}</td>
    <td>{has_location}#_LOCATIONLINK, #_LOCATIONTOWN{/has_location}</td>
    </tr>
  4. In Default event list format footer, close the table:

    </tbody>
    </table>
  5. Save the settings and view your events page.

Every list that uses the default format now prints as a table, including [events_list] shortcodes that don't set a format of their own.

Make one table with a shortcode​

The [events_list] shortcode takes the item format as its content, and the header and footer as attributes. HTML inside an attribute is hard to read and some editors alter it, so keep the header and footer short:

[events_list scope="future" limit="20" format_header="<table class='my-events-table'><tbody>" format_footer="</tbody></table>"]
<tr>
<td>#_EVENTDATES</td>
<td>#_EVENTLINK</td>
<td>#_LOCATIONNAME</td>
</tr>
[/events_list]

Add the shortcode in a Shortcode block, or in the code view of the editor, so the table tags are not rearranged.

In PHP the same three formats are arguments:

echo EM_Events::output( array(
'scope' => 'future',
'format_header' => '<table class="my-events-table"><tbody>',
'format' => '<tr><td>#_EVENTDATES</td><td>#_EVENTLINK</td><td>#_LOCATIONNAME</td></tr>',
'format_footer' => '</tbody></table>',
) );

Style the table​

Give the table a class, as above, and style it from your theme's stylesheet or Appearance then Customize then Additional CSS. Don't use the old cellpadding, cellspacing and width attributes.

.my-events-table { width: 100%; border-collapse: collapse; }
.my-events-table th, .my-events-table td { padding: 0.5rem; border-bottom: 1px solid #ddd; text-align: left; vertical-align: top; }

A wide table doesn't fit a phone screen. Keep to three or four columns, or wrap the table in an element with overflow-x: auto.

When to use a template file instead​

If the table needs logic that placeholders and conditional placeholders can't express, override templates/events-list.php as described in Using Template Files and build the rows in PHP.

Test it​

  1. View the page. Each event is a row, and the header row appears once.
  2. View the page source and check that the table opens once and closes once. A table that opens for every event means the opening tags are in the item format instead of the header.
  3. Check an event with no location. Its last cell is empty, not broken.