0

I have a custom post type, where one of ACF fields is map (we take google maps ). Currently, we are pasting that in text field, but to work, that must be executed as a shortcode. The problem is, that when pasting to single.php and page.php:

 <?php
 // Check if the custom field has a value
 if (get_field('zemelapis')) {
     // Echo the content of the field, this executes the iframe embed
     echo do_shortcode(get_field('zemelapis'));
 }
 ?>

nothing happens. Any suggestions?

1
  • Is the result in the field an actual shortcode function? Commented Mar 27 at 14:04

1 Answer 1

1

you are trying to output the value of an ACF field containing an embed code for a Google Map as a shortcode within your WP theme files . If the shortcode is not being executed, the get_field() function may be returning the raw value of the field rather than parsing it as a shortcode.

Replace your_google_map_shortcode with the actual shortcode name that you're using for embedding the map.

<?php
// Check if the custom field has a value
if (get_field('zemelapis')) {
    // Get the raw value of the field
    $field_value = get_field('zemelapis');

    // Check if the field value contains a shortcode
    if (has_shortcode($field_value, 'your_google_map_shortcode')) {
        // Execute the shortcode
        echo do_shortcode($field_value);
    } else {
        // If no shortcode found, output the raw value
        echo $field_value;
    }
}
?>
2
  • I'm not sure that I have a shortcode name. Basically, the code looks like this: <iframe src="google.com/maps/d/…" width="640" height="480"></iframe> Commented Mar 21 at 15:20
  • Well then you have to first make the shortcode. Commented Mar 26 at 3:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.