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!
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'] . '" />' : '' );
You must be logged in to reply to this topic.
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!
ACF wouldn’t be so widely used in WordPress if it didn’t have some pretty amazing capabilities. In this article, we look at a few of the features we’ll discuss during “7 things you didn’t know you could do with ACF” at #WPEDecode later this month. https://t.co/5lnsTxp81j pic.twitter.com/Yf0ThPG1QG
— Advanced Custom Fields (@wp_acf) March 16, 2023
© 2023 Advanced Custom Fields.
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.