Getting the ID of any image for use in functions.php

I’ve added two custom fields to attachments on my site, so the user can add a special image credit.

I’m trying to write a function that looks to see if either of the custom fields are populated, and if so echoes the contents.

Read More

The part I’m struggling with is getting the ID of the image into the function – it’s simple enough for featured images using get_post_thumbnail_id() – is there a similar way to get the ID for any attached image?

Here’s what I have so far:

    function mat_image_credit() {

    //this is the bit I'm struggling with:
    $mat_image_id = wp_get_attachment_image( $attachment->ID ); 

    //this is collecting the contents of the custom field and works fine
    $mat_flickr_credit = slt_cf_field_value('flickr_credit', 'attachment', $mat_image_id); 

    $mat_custom_credit = slt_cf_field_value('custom_credit', 'attachment', $mat_image_id);

    //and then this echoes out the contents depending on what we find...
    if (!empty ( $mat_flickr_credit ) ) { 
        echo '<div class="credit">Image by <a href="http://www.flickr.com/photos/' . $mat_flickr_credit . '" target="blank">' . $mat_flickr_credit . '</a></div>'; 
    } elseif ( !empty ( $mat_custom_credit ) ) { 
        echo '<div class="credit">Image by ' . $mat_custom_credit . '</div>'; 
    };

}

Thanks!

Related posts

Leave a Reply

2 comments

  1. Hopefully this helps,

    function mat_image_credit() {
        global $post;
        $args = array(
        'post_type'      => 'attachment',
            'post_parent'    => $post->ID,
            'post_mime_type' => 'image',
            'numberposts'    => -1,
        );
        $imgs = get_posts($args);
        foreach ($imgs as $img) {
    
            $field1 = slt_cf_field_value('flickr_credit', 'attachment', $img->ID);
            $field2 = slt_cf_field_value('custom_credit', 'attachment', $img->ID);
    
            if (!empty ( $field1 ) ) { 
                echo 'Image by ' . $field1; 
            } elseif ( !empty ( $field2 ) ) { 
                echo 'Image by ' . $field2; 
            };
        }
    }
    

    Then in your theme (used within The Loop),

    <?php mat_image_credit(); ?>
    

    The foreach statement is making the assumption that users can attach more than one image and credit attributions per post and that you want them printed in some kind of grouped order.

    The alternative is to do away with the foreach statement and instead pluck the first image from the array of returned attachments,

    function mat_image_credit() {
        global $post;
        $args = array(
        'post_type'      => 'attachment',
            'post_parent'    => $post->ID,
            'post_mime_type' => 'image',
            'numberposts'    => -1,
        );
        $imgs = get_posts($args);
    
            //$imgs[0]->ID grabs first image in the array (array key begining with 0)
            $field1 = slt_cf_field_value('flickr_credit', 'attachment', $imgs[0]->ID;);
            $field2 = slt_cf_field_value('custom_credit', 'attachment', $imgs[0]->ID;);
    
            if (!empty ( $field1 ) ) { 
                echo 'Image by ' . $field1; 
            } elseif ( !empty ( $field2 ) ) { 
                echo 'Image by ' . $field2; 
            };
    
        }
    

    Then in your theme [same as before] (used within The Loop),

    <?php mat_image_credit(); ?>
    

    Beyond this you could extend this function to make it a little dynamic in the sense that you could grab the first, second and third image attachments and use them in different parts of your template.

    Example;

    function mat_image_credit($custom = 0) {
        global $post;
        $args = array( //etc... );
        $imgs = get_posts($args);
    
        $field1 = slt_cf_field_value('flickr_credit', 'attachment', $img[$custom]->ID);
        $field2 = slt_cf_field_value('custom_credit', 'attachment', $img[$custom]->ID);
    
        if (!empty ( $field1 ) ) { 
            echo 'Image by ' . $field1; 
        } elseif ( !empty ( $field2 ) ) { 
            echo 'Image by ' . $field2; 
        };
    }
    

    Then in your theme [same as before] (used within The Loop),

    <?php mat_image_credit();  ?> //returns first image (default $custom = 0)
    <?php mat_image_credit(0); ?> //returns first image
    <?php mat_image_credit(1); ?> //returns second image
    <?php mat_image_credit(2); ?> //returns third image etc...
    

    If you don’t specify a value (integer) then the function by default will assign 0 as the key, which would retrieve the first image in the array of results. Subsequently, for those in which you specify an value, the corresponding key => value will be retrieved.

    Who knows… you might need to retrieve more than one lot of credits in more than one place in a template file. This will help.

  2. I went about it like this in the end:

    function image_credit($id) {
    
    $flickr_credit = slt_cf_field_value('flickr_credit', 'attachment', $id);
    $custom_credit = slt_cf_field_value('custom_credit', 'attachment', $id);
    
    if (!empty($flickr_credit)) {
    $credit = '<div class="credit">Image: <a href="http://www.flickr.com/photos/' . $flickr_credit . '" target="blank">' . $flickr_credit . '</a></div>'; }
    
    elseif (!empty($custom_credit)) {
    $credit = '<div class="credit">Image: ' . $custom_credit . '</div>'; }
    
    echo $credit;
    
    }
    

    Then in my template I can just

    echo image_credit($my_image_variable);