Venue list as short code?

This topic contains 2 replies, has 2 voices, and was last updated by  Marco 4 years, 3 months ago.

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #38609

    Hi Event Organiser community, has one of you managed to create a venue list as short code? There are several forum items around this topic but as far if I have seen with no public solution. I tried with Stephen’s instructions (https://wp-event-organiser.com/forums/topic/listing-venues-with-descriptions/ und https://wp-event-organiser.com/blog/tutorial/creating-your-own-shortcodes/) but I can’t get any further.

    add_shortcode( 'mf_venue_list', 'my_venue_list_shortcode_handler' );
    function my_venue_list_shortcode_handler( $atts, $content = null ){
        $venues = eo_get_venues();
        if( $venues ){
            foreach($venues as $venue){
                $venue_id = (int) $venue->term_id;
                printf('<h2><a href="%s">%s</a></h2>', eo_get_venue_link($venue_id), esc_html($venue->name));
                echo '<p>' . eo_get_venue_description( $venue_id ) . '</p>';
            }
         } 
    }
    

    I understand that I can’t use ‘echo’ or ‘print’…

    $html = '';
    //Generate HTML to replace the shortcode with
    return do_shortocde( $html );

    … but in Stephens snippet we have this mix of HTML and PHP, right?

    At least my short code currently does not give any error, but the name does not help me either. šŸ™ I would like to show all existing venues with their description and perhaps the map on a page.

    Any tips and hints? Thanks, Marco

    • This topic was modified 4 years, 3 months ago by  Stephen Harris.
    • This topic was modified 4 years, 3 months ago by  Stephen Harris.
    Marco
    #38614

    Hi Marco,

    I’ve tidied up the code snippet you posted – but it might not be exactly as you had it.

    As you’ve noted, shortcodes need to return HTML (as a string) rather than print it. There are two ways you can achieve this: 1) using output buffering or (2) just building out the string. The latter you can do as follows:

    add_shortcode( 'mf_venue_list', 'my_venue_list_shortcode_handler' );
    function my_venue_list_shortcode_handler( $atts, $content = null ){
        $html = "";
        $venues = eo_get_venues();
        if( $venues ){
            foreach($venues as $venue){
                $venue_id = (int) $venue->term_id;
                $html .= sprintf('<h2><a href="%s">%s</a></h2>', eo_get_venue_link($venue_id), esc_html($venue->name));
                $html .= '<p>' . eo_get_venue_description( $venue_id ) . '</p>';
    
               $html .= eo_get_venue_map($venue_id);
            }
         } 
    
         return $html;
    }
    

    The eo_get_venue_map function returns the HTML mark-up for a placeholder for the map and should load the scripts necessary for rendering it.

    Stephen Harris
    #38616

    Stephen,
    this is exactly what I needed! Thank you very much!
    It needs some styling and I need to think how I can influence the sorting (we have a main venue, some side venue and a just for info venue that should appear on the bottom of the list), but Iā€™m a huge step further to my desired outcome.
    Thanks!

    Marco
Viewing 3 posts - 1 through 3 (of 3 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.