2

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

Cray
  • 5,307
  • 11
  • 70
  • 166
alorange
  • 41
  • 4
  • I tested it also and it seems that it is really only working for the first shortcode. Even if I use different meta fields for every shortcode – Cray Oct 12 '22 at 15:48
  • if you are using ACF Pro you can set each item to display if the block name is correct and then you are able to add as many wordpress blocks as you need – JDawwgy Oct 12 '22 at 16:08
  • I thought so too. I tried it with ACF Pro and 3 different shortcode blocks. It really shows only the content from the first shortcode. Or do I miss something? – Cray Oct 12 '22 at 16:09

1 Answers1

2

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.

Cray
  • 5,307
  • 11
  • 70
  • 166
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/248766/discussion-between-cray-and-alorange). – Cray Oct 12 '22 at 18:34