Support » Plugin: Theater for WordPress » Display 'today' or 'tomorrow' in event listings

  • Resolved chrislondon

    (@chrislondon)


    Hi. Is there any way that I can add an option to display the words ‘today’ or ‘tomorrow’ instead of the event date using wpt_events?

    What I’d ideally like is to be able to use a shortcode that looks something like this:

    [wpt_events]{{day}} {{datetime|date(g.ia)}} {{title|permalink}} {{prices}}[/wpt_events]

    And it would output something like this:

    Today 5.00pm ‘Event 1’ £5.00
    Tomorrow 3.00pm ‘Event 2’ £6.00
    1 August 2.00pm ‘Event 3’ £5.00

    So if an event falls on the current date, I can display the date as ‘today’, if it’s the current date + 24 hours it displays ‘tomorrow’, but any other date is displayed in full. I would only want this to be an option, so I can also use the default WordPress date formats if I want to.

    I found some code about doing the same thing for post dates:
    https://wordpress.org/support/topic/how-to-display-today-and-tomorrow-instead-of-post-dates

    So I could adapt that. I think I’d need to write an extra function for wpt_events.php but I could really use some advice on how to approach it. Can you point me in the right direction?

    Thanks!

    https://wordpress.org/plugins/theatre/

Viewing 7 replies - 1 through 7 (of 7 total)
  • Plugin Author Jeroen Schmit

    (@slimndap)

    Hi Chris!

    You need to use the wpt/event/startdate filter.

    See https://github.com/slimndap/wp-theatre/blob/master/functions/wpt_event.php#L1259

    Thanks a lot!

    Can you tell me how I convert $startdate to another date format, so that I can write a conditional statement to see if the event date matches the current date?

    Also, can you control the date format through the filter? I can add a date format to the placeholder in the shortcode on my page, but it looks like that overrides anything I put in the filter.

    Plugin Author Jeroen Schmit

    (@slimndap)

    You can use $event->datetime() to get a timestamp value that you can use for comparisons.

    Thanks, Jeroen. I think I’m nearly there, but I’ve still got two problems.

    1. I can’t seem to get $event->datetime() to work properly. This is how I used it in my theme functions file: $eventdate = date('Y-m-d',$event->datetime());. It generated this error: ‘Call to a member function datetime() on a non-object in […]functions.php’.

    As an experiment, I tried adding the code directly to theatre/functions/wpt_event.php, just to see if I could make it work. This is what I used:

    $eventdate = date('Y-m-d',$this->datetime());
    $today = date( 'Y-m-d' );

    And then I added conditional statements using the two variables to compare dates. It worked fine. But, of course, I want to leave my core files untouched and use the filter to modify the original function in my functions.php file instead. If I simply transfer the code above to my functions.php file, I generate this error: ‘Using $this when not in object context in […]functions.php’.

    2. When I made it work by temporarily hacking wpt_event.php, I noticed that filtering $startdate also affected instances where I’d used {{dates}} elsewhere on my site. It looks like $startdate is used to determine $dates in wpt_production.php. I would prefer not to apply the same filter to {{dates}}. To avoid that, could I write a completely new function and create a new filter (for example {{day}}) without hacking the core plugin files?

    If not, I’ll have to use a conditional statement to restrict the filter to my homepage. Although there are still a couple of instances of {{dates}} on my homepage that would be affected, I think they’re ones I could live with.

    Any advice would be gratefully received. Thanks for your patience!

    Chris

    Plugin Author Jeroen Schmit

    (@slimndap)

    Try something this in your functions.php:

    function wpt_reformat_day($value, $field, $event) {
      // set the value of the {{day}} placeholder to the start date.
      $value = $event->startdate();
    
      $eventdate = date('Y-m-d',$event->datetime());
      $today = date( 'Y-m-d' );
    
      // Add your conditionals here to replace $value with 'today' or 'tomorrow'.
    
      return $value;
    }
    add_filter('wpt_event_day', 'wpt_reformat_day', 10, 3);

    This code will add

    Fantastic. That works perfectly. Thanks so much!

    I have also a question with this, how a get weekday before our date such as: wo.21-09-2016 or vrij.27.11.2016?

    Thanks Anyway!

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Display 'today' or 'tomorrow' in event listings’ is closed to new replies.