Best method to show "this week" Mon-Fri

WordPress Event Management, Calendars & Registration Forums General Question Best method to show "this week" Mon-Fri

This topic contains 3 replies, has 2 voices, and was last updated by  andytc 10 years, 7 months ago.

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #7470

    Hi , new to the forums and in need of some insight !

    I’m trying to show events that are running “this week” , but only show weekdays Mon – Fri , no weekends.

    I would also like the “no events” to show on days when there are no events.

    I’ve tried to edit the template for the widget-event-list.php , but getting a bit lost. I did get it working with a shortcode , but couldn’t include the “no events” bit , so thinking it would be better to edit the template.

    I’m using the template from Stephen on GitHub – custom event list widget

    Can anyone show me what code to add to that file to acheive my goal of showing events that are running “this week” ?

    last question , on the event widget , there is a field for “Template (leave blank for default)” Does this mean i can point the widget to a custom template with a unique name ? if so , where should that be paced – in my theme ?

    Every bit of help i get , i learn from 🙂 and thank you in advance !

    andytc
    #7478

    Hi Andy,

    I quickly threw this together. It requires php5.3+ (relative date format handling), but could be reworked to support 5.2 as well. I’ll let the comments do the explaining, but hopefully the logic is clear.

    Just chuck this in a utility plug-in (or you theme’s functions.php) and then use the shortcode wherever. I’ve briefly tested this on a dev-site. Hope it helps!

    //Usage: [my_mon_fri_events]
    add_shortcode( 'my_mon_fri_events', 'my_mon_fri_events_shortdcode_handler');
    function my_mon_fri_events_shortdcode_handler(){
    
        $html = '';
    
        //In php 5.3:
        $events = eo_get_events( array(
                'event_start_after' => 'monday this week',
                'event_start_before' => 'friday this week',
                'posts_per_page' => -1,
        ));
    
        $days_of_week = array(
                1 => 'Monday',
                2 => 'Tuesday',
                3 => 'Wednesday',
                4 => 'Thursday',
                5 => 'Friday',
        );
    
        $event_index = 0;
    
        //Do one day at a time
        for( $day_of_week = 1; $day_of_week <= 5; $day_of_week ++ ){
    
            //Display day title e.g. 'Monday'
            $html .= '<li>' . $days_of_week[$day_of_week]; //Start the day
    
    
            //Does the day have any events.
            if( !isset( $events[$event_index] ) ){
                //We've run out of events...
                $events_on_today = false;
    
            }else{
                $event = $events[$event_index];
                $event_day = (int) eo_get_the_start( 'N', $event->ID, null, $event->occurrence_id );
                //Does the next event happen on this day?
                $events_on_today = ( $day_of_week == $event_day );
            }
    
            //If this day has events, display them.
            if( $events_on_today ){
    
                $html .= '<ul>';
    
                //Display events for this day. Compares integer representation of days ( 1=Mon,...,7=Sun)
                while( $event_day === $day_of_week ){
    
                    //Display this event
                    $html .= sprintf(
                            '<li> <a href="%s">%s</a> on %s </li>',
                            get_permalink( $event->ID ),
                            get_the_title( $event->ID),
                            eo_get_the_start( get_option('date_format'), $event->ID, null, $event->occurrence_id )
                    );
    
                    //Move onto the next event
                    $event_index++;
    
                    //Set $event_day for the next event
                    if( isset( $events[$event_index] ) ){
                        $event = $events[$event_index];
                        $event_day = (int) eo_get_the_start( 'N', $event->ID, null, $event->occurrence_id );
                    }else{
                        $event_day = false;
                    }
                }
    
                $html .= '</ul>';
    
            }else{
                $html .= '<ul><li>No events</li></ul>';
            }
    
            $html .= '</li>'; //Close the day
        }
    
        return $html;
    }
    
    Stephen Harris
    #7479

    R.e. the template field for the widget. That accepts a format string (not a file name). It’s explained in more detail here: http://wp-event-organiser.com/documentation/widgets/

    Stephen Harris
    #7480

    Thanks Stephen , that’s working perfectly for me ! just added some styles and it’s all good 🙂

    andytc
Viewing 4 posts - 1 through 4 (of 4 total)
To enable me to focus on Pro customers, only users who have a valid license for the Pro add-on may post new topics or replies in this forum. If you have a valid license, please log-in or register an account using the e-mail address you purchased the license with. If you don't you can purchase one here. Or there's always the WordPress repository forum.