Support

Account

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

Solved

How to retrieve image field within a user field query?

  • I have a relational / user field to pull a list of links to registered users’ profiles, with also some other custom fields from the users’ profiles below each link. This code works perfectly, except the profile image, which doesn’t print. What am I doing wrong?

    <?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']; // the user name
        $userdesc = $value['user_description']; // user bio from profile
        $position = get_field( 'academic_position', 'user_' . $value['ID'] ); // custom ACF text field
        $affiliation = get_field( 'affiliations', 'user_' . $value['ID'] ); // custom ACF text field
        $pic = get_field( 'pic', 'user_' . $value['ID'] ); // custom ACF pic NOT showing
        $execs[] = sprintf( '<li><a href="%s">%s</a><p>%s</p><p>%s</p><p>%s</p><p>%s</p></li>', $link,  $nicename, $userdesc, $position, $affiliation, $pic ); //this prints the list, except for the image
      }
    
      echo '<h4>Executive Committee</h4>';
      echo '<ul>';
      echo implode( $execs );
      echo '</ul>';
    
    }
    
    ?>

    Any help would be much appreciated. Thanks!

  • Hi @miguelripoll

    My guess is that you’ve set the return value of the image field to “Image object” (that’s the default setting). That means that the $pic variable will be an array of the attachments values such as url, sizes, alt etc.

    Based on the way you print everything now you’d first need to setup the $pic variable as an actual img tag.

    Something like:

    
    <?php
    $pic_object = get_field( 'pic', 'user_' . $value['ID'] ); 
    $pic = '<img src="' . $pic_object['url'] . '" alt="' . $pic_object['alt'] . '" />';
    ?>
    
  • 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>';
    
    }
    
    ?>
  • Sure,

    Change it to this

    
    $pic = ( $pic_object ? '<img src="' . $pic_object['sizes']['thumbnail']. '" alt="' . $pic_object['alt'] . '" />' : '' );
    
  • Works a treat, thanks very much

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

The topic ‘How to retrieve image field within a user field query?’ is closed to new replies.