admin管理员组

文章数量:1123923

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?

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?

Share Improve this question asked Mar 21, 2024 at 12:06 rimgaudas.jurgaitisrimgaudas.jurgaitis 31 bronze badge 1
  • Is the result in the field an actual shortcode function? – Tony Djukic Commented Mar 27, 2024 at 14:04
Add a comment  | 

1 Answer 1

Reset to default 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;
    }
}
?>

本文标签: pluginsExecuting ACF field as a shortcode