Support

Account

Home Forums ACF PRO How to retrieve image field within a user field query? Reply To: How to retrieve image field within a user field query?

  • That works! thanks a bunch! Only problem: if there is no image, it throws a php error. Is there any way we can get the image conditionally, so that if the image field is empty it displays a default image (or nothing), instead of an error? My code now is this:

    <?php
    $values = get_field( 'execs','options' );
    if ( $values ) {
      $execs = array();
      foreach ( $values as $value ) {
        $link = get_author_posts_url( $value['ID'] ); //get the url
        $nicename = $value['display_name'];
        $userdesc = $value['user_description'];
        $position = get_field( 'academic_position', 'user_' . $value['ID'] ); // custom ACF text
        $affiliation = get_field( 'affiliations', 'user_' . $value['ID'] ); // custom ACF text
        $pic_object = get_field( 'user_image', 'user_' . $value['ID'] );
        $pic = '<img src="' . $pic_object['sizes']['thumbnail']. '" alt="' . $pic_object['alt'] . '" />';
        $execs[] = sprintf( '<li><a href="%s">%s</a><p>%s</p><p>%s</p><p>%s</p>%s</li>', $link,  $nicename, $userdesc, $position, $affiliation, $pic ); // listing
      }
    
      echo '<h4>Executive Committee</h4>';
      echo '<ul>';
      echo implode( $execs );
      echo '</ul>';
    
    }
    
    ?>