Upcoming events widget and template tags

Support for the WordPress Calendar plugin
stevenjcoates
Posts: 5
Joined: Mon May 26, 2014 2:04 am

Upcoming events widget and template tags

Unread post by stevenjcoates » Mon May 26, 2014 2:09 am

Hi,

Love your work Kieran.

Wondering if it's possible with the Upcoming Events widget to limit the number of events pulled?

I can see in settings a limit of days into the future but from a design perspective I'm hoping to show two columns side by side each with the next three upcoming events from particular categories.

Thanks.

stevenjcoates
Posts: 5
Joined: Mon May 26, 2014 2:04 am

Re: Upcoming events widget and template tags

Unread post by stevenjcoates » Wed Jun 25, 2014 1:17 am

I'm guessing not maybe?

User avatar
Kieran
Posts: 2059
Joined: Sat May 26, 2007 1:55 pm
Location: London
Contact:

Re: Upcoming events widget and template tags

Unread post by Kieran » Wed Jun 25, 2014 3:46 pm

Sorry, didn't catch your message before for some reason. No, this is not possible at the moment because of the way repeating events are determined in the code. Each day into the future has to be "checked" to see if an event will occur on that day, hence why a day count is an option and not a number of events. Code *could* be introduced to "cap" the number returned if it was greater than a given threshold but I don't think this is what you're looking for?

stevenjcoates
Posts: 5
Joined: Mon May 26, 2014 2:04 am

Re: Upcoming events widget and template tags

Unread post by stevenjcoates » Thu Jun 26, 2014 1:02 am

Thanks for getting back to me Kieran,

We're looking to just pull x number of upcoming events regardless of date but that's ok, we'll work out a way.

Cheers.

User avatar
Kieran
Posts: 2059
Joined: Sat May 26, 2007 1:55 pm
Location: London
Contact:

Re: Upcoming events widget and template tags

Unread post by Kieran » Thu Jun 26, 2014 8:16 am

If they're non-recurring events and you only use that type you could modify the code to simply grab all events with a start date greater than or equal to today and limit by number

stevenjcoates
Posts: 5
Joined: Mon May 26, 2014 2:04 am

Re: Upcoming events widget and template tags

Unread post by stevenjcoates » Mon Jul 07, 2014 5:44 am

Unfortunately some are recurring. Thanks anyway.

stevenjcoates
Posts: 5
Joined: Mon May 26, 2014 2:04 am

Re: Upcoming events widget and template tags

Unread post by stevenjcoates » Tue Jul 08, 2014 4:48 am

My clever colleague has come up with a solution so I'll share for anyone who might need this.

Add this to the plugin

Code: Select all

// Print upcoming events limited by set number
// Second parameter defaults to showing 5 events. Can be overriden in function call from page template like below, showing 3
// E.g. echo upcoming_events_limited_by_number('2,4,5,6,8',3);

function upcoming_events_limited_by_number($cat_list = '',$total_events_to_display = '5')
{
  global $wpdb;

  // Find out if we should be displaying upcoming events
  $display = $wpdb->get_var("SELECT config_value FROM ".WP_CALENDAR_CONFIG_TABLE." WHERE config_item='display_upcoming'",0,0);
  
  if ($display == 'true')
    {
	  // Get number of days we should go into the future. 
	  // We still need this to prevent timeout error if no events have been set. Make it a high number like 90 days in Calendar Options.
      $future_days = $wpdb->get_var("SELECT config_value FROM ".WP_CALENDAR_CONFIG_TABLE." WHERE config_item='display_upcoming_days'",0,0);

      $day_count = 1;
	  $total_events_to_display = $total_events_to_display+1; // assign number of events to display
      $count_of_events = 1;
      $output = '';
      while ($count_of_events < $total_events_to_display && $day_count < $future_days+1)
	{
	  list($y,$m,$d) = explode("-",date("Y-m-d",mktime($day_count*24,0,0,date("m",ctwo()),date("d",ctwo()),date("Y",ctwo()))));
	  $events = grab_events($y,$m,$d,'upcoming',$cat_list);
	  usort($events, "time_cmp");
	  if (count($events) != 0) {
	    $output .= '<li class="parent">'.date_i18n(get_option('date_format'),mktime($day_count*24,0,0,date("m",ctwo()),date("d",ctwo()),date("Y",ctwo()))).'<ul>';
	  } 
	  foreach($events as $event)
	    {
			if($count_of_events < $total_events_to_display) { // loop until event limit is reached
			  if ($event->event_time == '00:00:00') {
				$time_string = '<br/> '.__('all day','calendar');
			  }
			  else {
				$time_string = '<br/> '.__('at','calendar').' '.date(get_option('time_format'), strtotime(stripslashes($event->event_time)));
			  } 
				$output .= '<li class="child">'.draw_event($event).$time_string.'</li>';
				$count_of_events++; // increment counter
			}
	    }
	  if (count($events) != 0) {
	    $output .= '</ul></li>';
	  }
	  $day_count = $day_count+1;
	}

      if ($output != '')
	{
	  $visual = '<ul id="upcoming-events">';
	  $visual .= $output;
	  $visual .= '</ul>';
	  return $visual;
	}
    }
}
... and use this in your templates

Code: Select all


// function call wrapped in function_exists() so no errors if plugin deactivated
if ( function_exists( 'upcoming_events_limited_by_number' )) {
	echo '<div class="events">';
	echo upcoming_events_limited_by_number('2,4,5,6,8',3); // First parameter IDs of categories to display from. Second parameter (3) - limit to 3 events
	echo '</div>';
}

Post Reply