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>';
}
?>
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.