Home › Forums › General Issues › How to echo custom field from media attachment › Reply To: How to echo custom field from media attachment
I’m not sure what you’re using here to create the content, you need to inject your content into the string.
<?php
ob_start();
$attachments = get_attached_media('');
if ($attachments) {
foreach ($attachments as $attachment) {
echo get_field('custom-field-name', $attachment->ID),'<br />';
}
}
$image_content = ob_get_clean();
$block = 1;
$content = apply_filters('the_content', get_the_content());
$content = str_replace("/></a>", "/></a>
<div class='under-image'>
'.$image_content.'
</div> <br>
", $content);
$content = explode("</p>", $content);
for($i = 0; $i < count($content); $i++ ) {if ($i == $block) { ?>
<?php } echo $content[$i]. "";} ?>
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!
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 Privacy Policy. If you continue to use this site, you consent to our use of cookies.