Andrea Mattani
Andrea Mattani

Reputation: 5

ACF Field Wordpress - Custom Shortcode Problems

I have a small problem I wrote a simple shortcode function to display my acf value in the grid of the visual grid composer, this is my simple custom shortcode

function my_module_add_grid_shortcodes( $shortcodes ) {
    $shortcodes['vc_say_hello'] = array(
     'name' => __( 'Say Hello', 'my-text-domain' ),
     'base' => 'vc_say_hello',
     'category' => __( 'Content', 'my-text-domain' ),
     'description' => __( 'Just outputs Hello World', 'my-text-domain' ),
     'post_type' => Vc_Grid_Item_Editor::postType(),
  );
   return $shortcodes;
}

    add_shortcode( 'vc_say_hello', 'vc_say_hello_render' );
    function vc_say_hello_render() {
             if( get_field('item')  ): 
                 the_field('item');  
                else:
                echo "<h2>ITEM LOCKED</h2>"; 
             endif; 

    }

When I call the shortcode in the builder, "ITEM Locked" is displayed, even if the value of the element is set in the post !!!

Upvotes: 0

Views: 766

Answers (1)

Wilco
Wilco

Reputation: 383

Looks like get_field doesn't know where to get it from in the shortcode. Try adding the current post id as the second parameter

add_shortcode( 'vc_say_hello', 'vc_say_hello_render' );
function vc_say_hello_render() {
    $id = get_the_ID();
    if( get_field('item', $id)  ): 
        the_field('item', $id);  
    else:
        echo "<h2>ITEM LOCKED</h2>"; 
    endif; 
}

Upvotes: 1

Related Questions