how to get image url in advanced custom field wordpress post

16,097

Solution 1

Try this.

Image field and file field is returning the array so

$image_m =  get_field('slide_description');
$image_a = get_field('audio_file');
echo '<pre>';
print_r($image_m);
print_r($image_a);
echo '</pre>';

Suppose you get the out put as

array(
 ['name'] => 'test';
 ['url'] => 'http://example.mp3';

)

then write <?php echo $image_m['url'];?> instead of <?php the_field('audio_file');?>

Solution 2

Try this

    <?php
    $imgurl = get_field('slide_image',$post->ID);

    if (filter_var($imgurl, FILTER_VALIDATE_URL) === FALSE)
    {
      $imgurl = wp_get_attachment_url($imgurl);
    }
       echo '<img src="' . $imgurl . '" alt="image">';
    ?>
Share:
16,097
Vadivel Murugan M
Author by

Vadivel Murugan M

Updated on June 04, 2022

Comments

  • Vadivel Murugan M
    Vadivel Murugan M almost 2 years

    I have created a custom post type called "albums". I have 5 custom fields in my each post. 3 text fields, 1 image field and 1 file upload field. I'm getting 3 text values correctly in my template page. but I can't get the url part of image and file upload field. here is my code:

    <?php
    
    $posts = get_posts(array(
    'post_type' => 'albums'
    ));
    
    if($posts)
    {
    echo '<ul>';
    
    foreach($posts as $post)
    {
        echo '<li><a href="' . get_permalink($post->ID) . '">' . get_the_title($post->ID) . '</a></li>';
             the_field('album_category'); echo "<br>";
              the_field('album_name'); echo "<br>";
               the_field('slide_name'); echo "<br>";
                the_field('slide_description'); echo "<br>";
                the_field('audio_file'); echo "<br>";
    }
    
    echo '</ul>';
    }
    
    ?> 
    

    this is my current output

    Cuckoo-1
    Birds
    Cuckoo
    66, , 2668245306_027a56fe50_b, , , image/jpeg, http://localhost/YIB/wordpress/wp-content/uploads/2016/03/2668245306_027a56fe50_b-1.jpg, 1024, 768, Array
    sample sample sample sample sample sample sample
    18, , eudynamys-scolopacea, , "eudynamys-scolopacea"., audio/mpeg, http://localhost/YIB/wordpress/wp-content/uploads/2016/03/eudynamys-scolopacea.mp3

  • Maninderpreet Singh
    Maninderpreet Singh about 8 years
    try after remove if statement..is your image field return int value ?
  • Maninderpreet Singh
    Maninderpreet Singh about 8 years
    can u send me screenshot of field in admin setting?
  • Vadivel Murugan M
    Vadivel Murugan M about 8 years
    its working if we use get_field instead of get_the_field