phpwordpressadvanced-custom-fieldsshortcodeacfpro

2 or more ACF on one page Wordpress


I have products. For example:
Name: One ; ID: 4500
Name: Two ; ID:4551
Name: Three ; ID:4627

I create one page with:

[acf field="name" post_id="4500"]
[acf field="name" post_id="4551"]
[acf field="name" post_id="4627"]

As result, I get only first ONE.


 [acf field="name" post_id="4500"]
 [acf field="name" post_id="4500"]

Result is : ONE ONE


Solution

  • You could make your own shortcode. Add the following snippet to your functions.php in your theme:

    function acf_custom_shortcode( $atts ) {
    
        ob_start();
    
        $atts = shortcode_atts(
            array(
                'field'        => '',
                'post_id'      => false,
            ),
            $atts,
            'acf_custom'
        );
    
        // Try to get the field value.
        $value = get_field( $atts['field'], $atts['post_id'] );
    
        echo $value;
        return ob_get_clean();
    
    
    }
    add_shortcode( 'acf_custom', 'acf_custom_shortcode' );
    

    After that, use acf_custom as short code. Like this:

    [acf_custom field="name" post_id="4500"]
    [acf_custom field="name" post_id="4551"]
    [acf_custom field="name" post_id="4627"]
    

    I've tested it and it works fine.