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

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.