Support

Account

Home Forums General Issues Relationships works fine on post type but not on block?

Solving

Relationships works fine on post type but not on block?

  • I have a custom post type (team members), I am outputting some info from this on the blog post type using a relationship field with no problems. But I also want to use relationships to echo some team member content in a custom block on another page. But the identical code won’t work inside a block?

    How do I get it to work / I assume relationship fields do work inside blocks?

    Full block code below:

    
    <?php
      $class_name = 'team';
      $id = 'team-' . $block['id'];
    
      if (!empty($block['anchor'])) {
        $id = $block['anchor'];
      }
    
      if (!empty($block['className'])) {
        $class_name .= ' ' . $block['className'];
      }
    
      $therapists =  get_field('bingowings');
    
    ?>
    
    <?php var_dump($therapists); ?>
    
    <section id="<?php echo $id; ?>" class="<?php if( get_field('divider_line') ): ?><?php echo 'dividerline'; ?><?php endif; ?> container <?php echo $class_name; ?>">
      <?php if( $therapists ): ?>
    		<?php foreach($therapists as $post):?>
    
    		<?php setup_postdata($post);?>
    
        <?php 
        
          $name  = get_field ('name');
          $about  = get_field ('about');
        
          $image = get_field('photo'); $imagesize = 'full';
          if( $image ) {
              echo wp_get_attachment_image( $image, $imagesize );
          }
        
          $acone = get_field('accreditation_logo_one'); $aconesize = 'full';
          if( $acone ) {
              echo wp_get_attachment_image( $acone, $aconesize );
          }
          $actwo = get_field('accreditation_logo_one'); $actwosize = 'full';
          if( $actwo ) {
              echo wp_get_attachment_image( $actwo, $actwosize );
          }
          $acthree = get_field('accreditation_logo_one'); $acthreesize = 'full';
          if( $acthree ) {
              echo wp_get_attachment_image( $acthree, $acthreesize );
          }
        
        ?>
    
        <div class="row">
          
          <div class="col-md">
            <img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt']; ?>" />
          </div>
        
          <div class="col-md vertical-center">
            <h2><?php echo $name; ?> <?php get_svg('/assets/svg/accredited_1.svg'); ?></h2>
    
            <?php
    						$creds = get_field('accreditation');
    						if( $creds ): ?>
    					<ul>
                <?php foreach( $creds as $cred ): ?>
    						  <li><?php echo $cred; ?></li>
    						<?php endforeach; ?>
    					</ul>
    					<?php endif; ?>
    
            <p><?php the_field('name')?></p>
    
            <span><?php get_svg('/assets/svg/dotlinesm.svg'); ?></span>
    
            <div class="row">
          
              <div class="col-md-3">
                <p><strong>Accreditations:</strong></p>
              </div>
            
              <div class="col-md-9">
                <ul class="acr">
                <?php if( get_field('accreditation_logo_one') ) { ?>
                  <li><img src="<?php echo $acone['url']; ?>" alt="<?php echo $acone['alt']; ?>" /></li>
                <?php } ?>
                <?php if( get_field('accreditation_logo_two') ) { ?>
                  <li><img src="<?php echo $actwo['url']; ?>" alt="<?php echo $actwo['alt']; ?>" /></li>
                <?php } ?>
                <?php if( get_field('accreditation_logo_two') ) { ?>
                  <li><img src="<?php echo $acthree['url']; ?>" alt="<?php echo $acthree['alt']; ?>" /></li>
                <?php } ?>
                </ul>
              </div>
    
            </div>
          
          </div>
    
        </div>
    
        <?php endforeach; ?>
    
        <?php wp_reset_postdata(); ?>
    
    <?php endif; ?>
      
      <?php if( get_field('divider_line') ): ?><span class="divider"><?php get_svg('/assets/svg/dottedline.svg'); ?></span><?php endif; ?>
    
    </section>
    

    Thanks in advance!

  • I have got this to work by not using the <?php setup_postdata($post);?> and instead using the ‘$featured_post->ID’ method.

    Still confused by the need to use two different methods for different parts of the site, as I said my code above works fine on the blog single.php template but not when used in a block – if anyone knows why I’d love to know.

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.