0

I have an Advanced Custom Fields field group with ID group_61318c077c7d6. I've made this field group visible for Block = 'slider'.

In that field group is a field with ID field_61318c078a746, a relationship field with a relationship to a custom post type called templates.

I’d like to create a Gutenberg slider block like this one, which loops over the rows of group_61318c077c7d6 and grabs the image from Image image field of the field_61318c078a746’s related templates.

So, something like:

<div id="<?php echo esc_attr($id); ?>" class="<?php echo esc_attr($className); ?>">
    <?php if( have_rows('group_61318c077c7d6') ) { ?>
        <?php echo 'we have rows';?>
        <div class="slides">
        <?php while( have_rows('group_61318c077c7d6') ) {
            the_row();
        }
        $templates = get_sub_field('field_61318c078a746'); //should return a post object
        if ( $templates ) { ?>
            <div>
            <?php foreach ($templates as $template) {
                $template_featured_image = get_field('Image', $template);
                echo '<img src="' . $template_featured_image . '" />';
            }
            ?>
            </div>
        <?php } ?>
    </div>
    <?php } else { ?>
        <p>Please add some slides.</p>
    <?php } ?>
</div>

When I add the block to a page, I see the group_61318c077c7d6 field group in the sidebar, and I can select 'templates' within the relationship selector, but no images are displayed in the slider block, because <?php if( have_rows('group_61318c077c7d6') ) { ?> is returning false, even though I see this field group in the slider block's properties sidebar.

How do I complete this code to grab the featured image of the field_61318c078a746’s related post, please?

These are the ACF fields of the Templates field group, which are attached to the templates custom post type:

enter image description here

Help appreciated.

2
  • 1
    Just curious to know if there is a specific reason to use the group ids instead of field names to get the values? Also, just to be sure does the block gets printed at all? Do you see the wrapper div on the page? <div id="<?php echo esc_attr($id); ?>" class=""
    – Kumar
    Jun 20, 2023 at 9:17
  • You're right, I don't need to use the group id. I do see the wrapper div. With Caleb's solution below there are no images displaying, they are being output as <img src="">, @Kumar
    – Steve
    Jun 25, 2023 at 23:23

1 Answer 1

1

According to ACF documentation, a foreach loop is to be used to access the items in a Relationship field.

Untested:

$templates = get_field( 'field_61318c078a746' );

foreach ( $templates as $template ) {
    $template_image_id  = absint( get_field( 'Image', $template ) );
    $template_image_url = 'https://example.com/fallback-image.jpg';

    if ( ! empty( $template_image_id ) ) {
        $template_image_url = wp_get_attachment_image_url( $template_image_id, 'large' );
    }

    ...
}

In most use cases you will not need to interact with field groups on the frontend. There are occasions for it, but very few in my experience.

5
  • Thanks Caleb. The problem is that <?php if( have_rows('group_61318c077c7d6') ) { ?><?php echo 'we have rows';?> echoes nothing.
    – Steve
    Jun 19, 2023 at 5:43
  • Please try the code supplied in my answer: you shouldn't need to use the group at all.
    – Caleb
    Jun 19, 2023 at 13:05
  • Thanks. I was wrong; the images are not featured images, but set in a field called Image. At the moment, the slider images are being output as <img src="8514">. Switching to $template_featured_image = get_field('Image', $template); produces <img src="">
    – Steve
    Jun 25, 2023 at 23:27
  • If you have the ID 8514 you can look that up with wp_get_attachment_image_url or wp_get_attachment_image_src etc., or wp_get_attachment_image for the full HTML img tag
    – Rup
    Jun 26, 2023 at 0:56
  • Updated answer.
    – Caleb
    Jun 26, 2023 at 13:09

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.