• Hi,
    I try to dynamically create a map with markers for all my Custom Post Types with adress.

    If I put this :

    <?= do_shortcode('[leaflet-map fitbounds]'); ?>
                    <?= do_shortcode('[leaflet-marker address="57000 Metz"]'); ?>
                    <?= do_shortcode('[leaflet-marker address="12 promenade des Canaux 54 000 Nancy"]'); ?>

    It works but it’s not dynamic.

    If I put this :

     <?php
                    $args = array(
                        'post_type' => 'franchise',
                        'posts_per_page' => -1,
                        'post_status' => 'publish'               
                    );
                    $the_query = new WP_Query($args);
                    if ( $the_query->have_posts() ){
                        while ( $the_query->have_posts() ){
                            $the_query->the_post(); 
                            $adresse = get_field('adresse').' '.get_field('code_postal').' '.get_field('ville');
                            print(do_shortcode('[leaflet-marker svg color="#fff" background="#000" iconClass="fas fa-map-marker" address="'.$adresse.']')); ?>
                        <?php }
                        wp_reset_postdata();            
                    } ?> 

    It is not working, I think js is called before my loop is ended.

    How can I do that ?

    Thanks

    • This topic was modified 4 years, 8 months ago by ysty.
Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Author bozdoz

    (@bozdoz)

    Are you also calling ‘leaflet-map’ somewhere before the markers in the second example? Is anything output? Do you see anything in the browser developer console (try to right-click and inspect element, near where the map should be)? Maybe try printing the shortcodes without running do_shortcode just to see what shortcodes are being dynamically generated.

    Thread Starter ysty

    (@ysty)

    Thanks for your quick reply.

    I already tried to just print shortcode and everything is ok.

    Leaflet-map is called only this time. The second code section is my message is the full code.

    It’s no error or message in the console.

    The map is displayed, but there’s only one marker and not at the right place.

    Should I do it in another way ?

    • This reply was modified 4 years, 8 months ago by ysty.
    Thread Starter ysty

    (@ysty)

    You can see the generated map Here

    All markers are added but at the some wrong place (I think they are placed at the default map address).

    • This reply was modified 4 years, 8 months ago by ysty.
    • This reply was modified 4 years, 8 months ago by ysty.
    • This reply was modified 4 years, 8 months ago by ysty.
    Plugin Author bozdoz

    (@bozdoz)

    Looks like there’s an issue with one of your addresses:

    e l'Homme 57000 Metz'

    You should be able to fix this by escaping the quote with $adresse = addslashes($adresse);

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Dynamically add [leaflet-marker]’ is closed to new replies.