Support

Account

Home Forums General Issues How to echo custom field from media attachment

Solved

How to echo custom field from media attachment

    • zecke

    • February 14, 2020 at 11:08 pm

    Hi

    I added one custom field to media attachment, where I store URL of source of the image, but I want to display that custom field on single post and there is a problem.

    I use this: <?php echo get_field('my_custom_field', 3281); ?> and it works, but only for attachment id=3281. I would like to change that and make it to echo custom field to current media (image) that is attached in post. So I need to change that atachment id dynamically.

    I hope I wrote it clearly πŸ™‚ I`m not a programmer so please help me with that.

  • Are you trying to use the featured image? Or another image? In any case you need to use the WP function that will return the ID that you want. For example

    
    get_post_thumbnail_id();
    
    • zecke

    • February 16, 2020 at 9:58 pm

    I got attached only one picture to every post I made John, so I need to get the id the attachment.

    • zecke

    • February 17, 2020 at 8:12 pm

    Can You please help me combine this code <?php echo get_field('my_custom_field', 3281); ?> with this ? get_post_thumbnail_id();

  • 
    echo get_field('my_custom_field', get_post_thumbnail_id());
    
    • zecke

    • February 18, 2020 at 6:50 pm

    Im sorry but it just doesnt work

  • What does this output in the same place

    
    echo get_post_thumbnail_id()
    
    • zecke

    • February 19, 2020 at 6:42 pm

    John in both cases I get nothing. Zero effect. It displays nothing

  • Well, that means that you post does not have a feature image.

    Try

    
    $attachments = get_attached_media('');
    echo '<pre>'; print_r($attachments); echo '</pre>';
    
    • zecke

    • February 20, 2020 at 8:11 pm

    We`re so close John ! It showed me whole array of stuff.

    For example: [ID] => 3281 – and its what Im looking for.

    Now how to combine this code with the previous one ?

  • Yes, I was just making sure that the post had attachments. What the above does is get all of them, just like get_posts(). Now you need to loop over them and output them.

    • zecke

    • February 21, 2020 at 6:06 pm

    OK. But like I was saying Im not a programmer, so please Can You help me with this ?

  • 
    $attachments = get_attached_media('');
    if ($attachments) {
      foreach ($attachments as $attachment) {
        echo get_field('custom-field-name', $attachment->ID),'<br />';
      }
    }
    
    • zecke

    • February 22, 2020 at 12:04 am

    Wow John. Its working. Thank You ! Im really glad that You wanted to help me. You re very kind:) If You ever will be in Poland write to me I will help You. I can send You lot of useful information about my country.

    Ps. Lets say I will have couple of photos attached to one post. Is there is any chance that You could help me with that ?

  • That code will loop over all of the attachments, you just need to output each one inside the foreach() loop. Attachments are posts and work like posts.

    • zecke

    • February 22, 2020 at 12:23 am

    Ok so my last huge request is… I got this code that displays everything I want under every image attached to my post.

    Here is the code:

    <?php $block = 1;
    $content = apply_filters('the_content', get_the_content());
    $content = str_replace("/></a>", "/></a>
    
    <div class='under-image'>
    
    HERE I CAN PUT WHATEVER I WANT
    
    </div> <br>
    ", $content);
    $content = explode("</p>", $content);
    for($i = 0; $i < count($content); $i++ ) {if ($i == $block) { ?>
    
    <?php } echo $content[$i]. "";} ?>
    

    How Can I combine the code with that one You gave me ?

    • zecke

    • February 25, 2020 at 6:15 pm

    John… will You help me for the last time ? Im almost there πŸ™‚

  • 
    <?php $block = 1;
    $content = apply_filters('the_content', get_the_content());
    $content = str_replace("/></a>", "/></a>
    
    <div class='under-image'>
    
    $attachments = get_attached_media('');
    if ($attachments) {
      foreach ($attachments as $attachment) {
        echo get_field('custom-field-name', $attachment->ID),'<br />';
      }
    }
    
    </div> <br>
    ", $content);
    $content = explode("</p>", $content);
    for($i = 0; $i < count($content); $i++ ) {if ($i == $block) { ?>
    
    <?php } echo $content[$i]. "";} ?>
    
    • zecke

    • February 26, 2020 at 12:30 am

    Im sorry John, but it does not work.

    As an output under every image I get this code instead of custom-field-name

    
    = get_attached_media(''); if () { foreach ( as ) { echo get_field('custom-field-name', ),'
    '; } }
    
  • 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]. "";} ?>
    
    • zecke

    • February 26, 2020 at 3:08 am

    Thanks again. It is working… BUT it`s showing only custom-field-name of the first attached image under every image that I attached to this post.

    What I mean:

    1. First image
    custom-field-name1
    2. Second image
    custom-field-name1
    3. Third image
    custom-field-name1

    And it supposed to show:

    1. First image
    custom-field-name1
    2. Second image
    custom-field-name2
    3. Third image
    custom-field-name3

    I hope I wrote it clearly πŸ™‚

    • zecke

    • February 27, 2020 at 8:37 pm

    Any ideas John ?

  • You need to alter the code to show the fields you want to show

    • zecke

    • February 27, 2020 at 11:42 pm

    But John Your code works great. The problem is that it shows only custom-field-name from the first attached image. Under other attached images I get the same value but it suppose to change

  • I don’t see any reason that it would be doing that. Try echoing out the id as well and see if you are getting values for different attachments.

    
    foreach ($attachments as $attachment) {
        echo $attachment->ID,': ',get_field('custom-field-name', $attachment->ID),'<br />';
      }
    
Viewing 25 posts - 1 through 25 (of 26 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.