Vadivel Murugan M Vadivel Murugan M - 2 months ago 19
PHP Question

how to get image url in advanced custom field wordpress post

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

Answer Source

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');?>