Support

Account

Home Forums Add-ons Flexible Content Field Flexible Content – Map – functions.php

Solved

Flexible Content – Map – functions.php

  • Hey there,

    I’m trying to set up a function in my theme that is supposed to load the dependencies for the Google Maps field only, if this field is used on a page.
    This works fine for the following field setup on a page:

    Locations (Repeater)

    • City (Text)
    • Map (Google Map)
    • Address information (WYSIWYG)
    • Backgroundimage (Image)

    The code I use in the functions.php is as follows:

    function acfgmaps_scripts() {
        if(!empty(have_rows('locations',$post_id))){
    			$apiKey='xxx';
    				wp_enqueue_script('googleMapsAPI', 'https://maps.googleapis.com/maps/api/js?key='.$apiKey, array('jquery'), '1.0', true);
    				wp_enqueue_script('maps-helper', get_template_directory_uri() . '/inc/google-maps-helper.js', array('jquery'), '1.0', true);
        }
    }
    add_action( 'wp_enqueue_scripts', 'acfgmaps_scripts' );

    Now my problem is, that I want to use the Flexible Content field to produce most of the content. And I don’t get this function to work as soon as the repeater field is inside the Flexible Content field. Is it related to the fact, that the fields are inside the “(get_)row_layout”? Any hints how to solve this?

    Thanks a lot in advance!

  • Yes, Once you are already inside a loop on a repeater (a flex field is also a repeater) you no longer need to supply a post ID.

    
    if (have_rows('flex-field-name', $post_id)) {
      while (have_rows('flex-field-name', $post_id)) {
        the_row();
        // you no longer need to provide the post ID
        // nested repeater
        if (have_rows('locations')) {
          while (have_rows('locations')) {
            the_row();
            // etc....
          }
        }
      }
    }
    
  • Great! I totally forgot about that because I was working on the templates in the last couple of days.

    Thanks so much! 🙂

Viewing 3 posts - 1 through 3 (of 3 total)

The topic ‘Flexible Content – Map – functions.php’ is closed to new replies.