Support

Account

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

Solved

How to echo custom field from media attachment

  • 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();
    
  • I got attached only one picture to every post I made John, so I need to get the id the attachment.

  • 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());
    
  • Im sorry but it just doesnt work

  • What does this output in the same place

    
    echo get_post_thumbnail_id()
    
  • 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>';
    
  • 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.

  • 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 />';
      }
    }
    
  • 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.

  • 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 ?

  • 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]. "";} ?>
    
  • 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]. "";} ?>
    
  • 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 πŸ™‚

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

    __________________________________________________________________________________________
    [url=http://www.alandental.com/category-16-b0-Dental-Handpiece.html][color=#000000]Dental Handpiece[/color][/url]Β |Β [url=http://www.alandental.com/category-55-b0-Portable-X-ray-Machine.html][color=#000000]Portable X-ray Machine[/color][/url]

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

  • 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

Viewing 25 posts - 1 through 25 (of 27 total)

You must be logged in to reply to this topic.