• Resolved Eric Klingensmith

    (@eric_thread)


    I noticed a lot of people asking this question and no easy solution, so after completing this I figured I’d post it up for everyone. This pretty much functions the same way as the native [event] shortcode except for the fact that instead of looking for an event by ID, it simply pulls the next event. I’ll probably further expand on this with filtering, but for now, here we go:

    <?php
    /**
     * Shows the next event according to given specifications. Accepts any event query attribute.
     * @param array $atts
     * @return string
     */
    function em_get_next_event_shortcode($atts, $format='') {
        global $EM_Event, $post;
    
        // Get the first event in the list
        $events = EM_Events::get( array('limit'=>1) ); // It's an array at this point
    
        // Make sure we got something
        if( !empty($events) ) {
            // Now set the global
            $EM_Event = $events[0];
        }            
    
        // Now continue just like the originaL
        $return = '';
        $the_event = is_object($EM_Event) ? clone($EM_Event):null; //save global temporarily
        $atts = (array) $atts;
        $atts['format'] = ($format != '' || empty($atts['format'])) ? $format : $atts['format'];
        $atts['format'] = html_entity_decode($atts['format']); //shorcode doesn't accept html
        if( !empty($atts['event']) && is_numeric($atts['event']) ){
            $EM_Event = em_get_event($atts['event']);
            $return = ( !empty($atts['format']) ) ? $EM_Event->output($atts['format']) : $EM_Event->output_single();
        }elseif( !empty($atts['post_id']) && is_numeric($atts['post_id']) ){
            $EM_Event = em_get_event($atts['post_id'], 'post_id');
            $return = ( !empty($atts['format']) ) ? $EM_Event->output($atts['format']) : $EM_Event->output_single();
        }
        //no specific event or post id supplied, check globals
        if( !empty($EM_Event) ){
            $return = ( !empty($atts['format']) ) ? $EM_Event->output($atts['format']) : $EM_Event->output_single();
        }elseif( $post->post_type == EM_POST_TYPE_EVENT ){
            $EM_Event = em_get_event($post->ID, 'post_id');
            $return = ( !empty($atts['format']) ) ? $EM_Event->output($atts['format']) : $EM_Event->output_single();
        }
        $EM_Event = is_object($the_event) ? $the_event:$EM_Event; //reset global
        return $return;
    }
    add_shortcode ( 'next_event', 'em_get_next_event_shortcode' );
    ?>

    https://www.ads-software.com/plugins/events-manager/

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thanks for sharing – should help someone else.

    Thread Starter Eric Klingensmith

    (@eric_thread)

    I hope it does. Lord knows the WP community has helped me quite significantly over the years, so much so that I’m finally to a point where I can contribute in return. So I figure it’s about time that I actively make an effort toward giving back.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Next Event Shortcode’ is closed to new replies.