Support

Account

Home Forums ACF PRO ACF Relationship Block Issue Reply To: ACF Relationship Block Issue

  • You have it the other way around: You have to apply $post->ID to the functions in the template, not the foreach.
    The “within-the-loop”-functions, so to speak, such as get_permalink() use the current post to fetch information. That is how it is intended. But you can pass $post->ID as an argument to most of them (some have to be replaced), so they will fetch information on the current post of the foreach.
    Try this:

    <?php
    /**
     * Programmes Block Template.
     *
     * @param   array $block The block settings and attributes.
     * @param   string $content The block inner HTML (empty).
     * @param   bool $is_preview True during AJAX preview.
     * @param   (int|string) $post_id The post ID this block is saved to.
     */
    
    // Create id attribute allowing for custom "anchor" value.
    $id = 'prog-' . $block['id'];
    if ( ! empty( $block['anchor'] ) ) {
    	$id = $block['anchor'];
    }
    
    // Create class attribute allowing for custom "class_name" and "align" values.
    $class_name = 'progBlock';
    if ( ! empty( $block['class_name'] ) ) {
    	$class_name .= ' ' . $block['class_name'];
    }
    
    if ( ! empty( $block['align'] ) ) {
    	$class_name .= ' align' . $block['align'];
    }
    
    if ( $is_preview ) {
    	$class_name .= ' is-admin';
    }
    ?>
    
    <div id="<?php echo esc_attr( $id ); ?>" class="<?php echo esc_attr( $class_name ); ?>">
    
    	<?php
    	$posts = get_field( 'progs' );
    	if ( $posts ) :
    	?>
    
    	<div class="progGrid grid-x grid-margin-x grid-margin-y">
    		<?php foreach ( $posts as $post ) : ?>
    			<div class="card cell small-12 medium-6 large-4">
    				<img role="img" src="<?php echo get_the_post_thumbnail_url( $post , 'foureighty' ); ?>" alt="<?php echo get_the_title( $post ); ?>"/>
    				<div class="progText">
    					<div class="progTitle">
    						<h3 class="blog-title">
    							<a href="<?php echo esc_html( get_permalink( $post ) ); ?>" alt="<?php the_title_attribute( [ 'post' => $post ] ); ?>">
    								<?php echo get_the_title( $post ); ?>
    							</a>
    						</h3>
    					</div>
    					<div class="progExcerpt">
    						<h3 class="blog-title show-for-large">
    							<a href="<?php echo esc_html( get_permalink( $post ) ); ?>" alt="<?php the_title_attribute( [ 'post' => $post ] ); ?>">
    								<?php echo get_the_title( $post ); ?>
    							</a>
    						</h3>
    							<?php echo get_the_excerpt( $post ); ?>
    					</div>
    				</div>
    			</div>
    		<?php endforeach; ?>
    	</div>
    
    	<?php endif; ?>
    </div>