• Resolved radcgnet

    (@radcgnet)


    I have created a custom field called avatar as image type. I then go to the post and upload an image and save it. I have updated the blog.php with this code:
    echo (get_post_meta($post->ID, ‘avatar’, true) != ”) ? ‘<div class=”avatar”><img fetchpriority=”high” decoding=”async” class=”round” <img src=”‘. get_post_meta($post->ID, ‘avatar’, true) . ‘”></div>’ : “”;
    It returns an id number, not a reference to the image. How do I get it to return the image?

Viewing 1 replies (of 1 total)
  • Yan

    (@yanmetelitsa)

    I think you have to use native get_field() function.

    I will provide more correct code to solve your problem:

    $avatar_id = get_field( 'avatar', $post->ID );

    if ( $avatar_id ) {
    printf( '<div class="avatar">%s</div>',
    wp_get_attachment_image( $avatar_id ),
    );
    }
    1. The best way to print a WordPress image is to use a function like wp_get_attachment_image()
    2. To use it you have to change Return Format property in field settings to Image ID.
Viewing 1 replies (of 1 total)
  • You must be logged in to reply to this topic.