Title: Dynamically add [leaflet-marker]
Last modified: March 2, 2020

---

# Dynamically add [leaflet-marker]

 *  [ysty](https://wordpress.org/support/users/ysty/)
 * (@ysty)
 * [6 years, 3 months ago](https://wordpress.org/support/topic/dynamically-add-leaflet-marker/)
 * 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 6 years, 3 months ago by [ysty](https://wordpress.org/support/users/ysty/).

Viewing 4 replies - 1 through 4 (of 4 total)

 *  Plugin Author [bozdoz](https://wordpress.org/support/users/bozdoz/)
 * (@bozdoz)
 * [6 years, 3 months ago](https://wordpress.org/support/topic/dynamically-add-leaflet-marker/#post-12499176)
 * 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](https://wordpress.org/support/users/ysty/)
 * (@ysty)
 * [6 years, 3 months ago](https://wordpress.org/support/topic/dynamically-add-leaflet-marker/#post-12499190)
 * 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 6 years, 3 months ago by [ysty](https://wordpress.org/support/users/ysty/).
 *  Thread Starter [ysty](https://wordpress.org/support/users/ysty/)
 * (@ysty)
 * [6 years, 3 months ago](https://wordpress.org/support/topic/dynamically-add-leaflet-marker/#post-12499205)
 * You can see the generated map [Here](https://cube-escape.fr/preprod/devenir-franchise/)
 * All markers are added but at the some wrong place (I think they are placed at
   the default map address).
    -  This reply was modified 6 years, 3 months ago by [ysty](https://wordpress.org/support/users/ysty/).
    -  This reply was modified 6 years, 3 months ago by [ysty](https://wordpress.org/support/users/ysty/).
    -  This reply was modified 6 years, 3 months ago by [ysty](https://wordpress.org/support/users/ysty/).
 *  Plugin Author [bozdoz](https://wordpress.org/support/users/bozdoz/)
 * (@bozdoz)
 * [6 years, 3 months ago](https://wordpress.org/support/topic/dynamically-add-leaflet-marker/#post-12499482)
 * 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.

 * ![](https://ps.w.org/leaflet-map/assets/icon-256x256.png?rev=1693083)
 * [Leaflet Map](https://wordpress.org/plugins/leaflet-map/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/leaflet-map/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/leaflet-map/)
 * [Active Topics](https://wordpress.org/support/plugin/leaflet-map/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/leaflet-map/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/leaflet-map/reviews/)

 * 4 replies
 * 2 participants
 * Last reply from: [bozdoz](https://wordpress.org/support/users/bozdoz/)
 * Last activity: [6 years, 3 months ago](https://wordpress.org/support/topic/dynamically-add-leaflet-marker/#post-12499482)
 * Status: not resolved