dragunoff

wpquestions.com, ID = 3166

Oct 19th, 2011
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.01 KB | None | 0 0
  1. function list_events_by_date() {
  2.  
  3.     // vars
  4.     $r = '';
  5.     $start_date = '';
  6.     $multi_day_events = array();
  7.  
  8.     // get all events
  9.     $events = get_posts( array(
  10.         'order' => 'ASC',
  11.         'numberposts' => -1,
  12.         // 'post_type' => 'events',
  13.         'meta_key' => 'start date',
  14.         'orderby' => 'meta_value_num'
  15.         )
  16.     );
  17.  
  18.     // if there are any events
  19.     if ( $events ) {
  20.    
  21.         // get the start date of the first event
  22.         $start_date = strtotime( get_post_meta( $events[0]->ID, 'start date', true ) );
  23.    
  24.         // begin building output
  25.         $r .= '<h2>' . strftime( '%B %e, %Y', $start_date ) . '</h2>';
  26.         $r .= '<ul class="events-list">';
  27.        
  28.         // loop events
  29.         foreach ( $events as $post ) {
  30.        
  31.             // re-assign start date variable
  32.             $old_start_date = $start_date;
  33.  
  34.             // get the start and end dates of the current event
  35.             $start_date = get_post_meta( $post->ID, 'start date', true );
  36.             $end_date = get_post_meta( $post->ID, 'end date', true );
  37.             // convert them to timestamps
  38.             $start_date = $start_date != '' ? strtotime( $start_date ) : $start_date;
  39.             $end_date = $end_date != '' ? strtotime( $end_date ) : $start_date;
  40.            
  41.             /**
  42.              * process current event
  43.              */        
  44.             // check wether we're at a new date
  45.             if( $start_date != $old_start_date ) {
  46.            
  47.                 // start a new section for new date
  48.                 $r .= '</ul>';
  49.                 $r .= '<h2>' . strftime( '%B %e, %Y', $start_date ) . '</h2>';
  50.                 $r .= '<ul class="events-list">';
  51.                
  52.             }
  53.  
  54.             // HTML for each event
  55.             $event_html = '<li><a href="' . get_permalink( $post->ID ) . '">' . get_the_title( $post->ID ) . '</a></li>';
  56.             $r .= $event_html;
  57.            
  58.             // compare start and end date of current event (is it a multi-day event?)
  59.             if ( ( $start_date < $end_date ) && ( $end_date != '' ) ) {
  60.                 // save event for further looping
  61.                 $multi_day_events[] = array (
  62.                     'ID' => $post->ID,
  63.                     'start_date' => $start_date,
  64.                     'end_date' => $end_date,
  65.                     'html' => $event_html,
  66.                 );
  67.             }
  68.            
  69.         }
  70.        
  71.         $r .= '</ul>';
  72.        
  73.         // echo/return the result
  74.         echo $r;
  75.        
  76.     }
  77.  
  78. }
Advertisement
Add Comment
Please, Sign In to add comment