Home Forums Front-end Issues How to get acf user image in search results?


How to get acf user image in search results?

  • I’ve added a custom field to the wp user profile page to hold an image.
    the image in acf returns the object

    and when I call it in a template everything works great.
    I cannot get the image to display in the search results page

    here’s my snippet.
    I call the featured image of posts, then I try calling to see if the user image exists…but thats part of the problem and then if no featured or user image then show the no logo image.

    <?php if ( has_post_thumbnail() ) {?>
    <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_post_thumbnail('img200200') ?></a>
    <?php } elseif ( get_field('field_525edf98cd7a2') ) { 
    global $post;
    $author_id = get_the_author_meta( 'ID' );
    $author_badge = get_field('image_a_la_une', 'user_'. $author_id ); // image field, return type = "Image Object"
    <img src="<?php echo $author_badge['url']; ?>" alt="<?php echo $author_badge['alt']; ?>" />				
    <?php } else {?>
    <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><img src="<?php bloginfo('template_url');?>/images/no-logo.jpg"></a>
    <?php } ?>

    any help would be much appreciated

    • Elliot

    • February 14, 2014 at 9:18 am

    Hi @aronblack

    Lets first debug your code:
    1. Please remove global $post; – this is not needed
    2. Debug $author_id
    3. Debug $author_badge

    You can debug like so:

    echo '<pre>';
    	print_r( $author_badge );
    echo '</pre>';
    die; ?>
  • Hi Eliot,
    Thank you for taking the time to help me with my problem
    I tried what you suggested, iremoved the global $post and for both variables, author_badge and author_id
    Both returned empty or blank.

    I then did a var_dump on them
    and for author_id the result was string(0) “”
    and for author_badge the result was bool(false)

    I use the same code on a static template outside the loop and it works as expected.

    Is it because Im in the wp loop of trying to access acf custom user data?

    • Elliot

    • February 17, 2014 at 10:02 am

    Hi @aronblack

    Thanks for the info. The issue is due to your author ID variable being blank.

    Perhaps you are using the wrong function to get the post’s author?


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

You must be logged in to reply to this topic.

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.