Displaing info from custom fields in all images' HTML (1 post)

  1. readydesigns
    Posted 4 years ago #

    I've created a custom field within the image upload screen (info first found here). The field stores the name of a photographer to give him/her credit. Everything's fine there.

    What I've been trying to do is get this information into every image's title data across the site. I want my field for Image Credit to append itself to all image titles across the site, so each image title would read something like "My Title (by Photographer Name)".

    Thanks to some help from the folks over at StackExchange, I've gotten something that's almost working, but for some reason it's only affecting featured images. If the post has no featured image, it doesn't affect any images for that post.

    function filter_image_title($attr, $attachment = null){
        //Find photo credit with $attachment->ID
        $attachment_credit = get_post_meta($attachment->ID, '_waz-image-credit', true);
        //Store original image info
        $attr['title'] = get_post($attachment->ID)->post_title;
        $attr['alt'] = get_post_meta($attachment->ID, '_wp_attachment_image_alt', true);
        //If a credit has been added to the image, add this to the title
            $attr['title'] .= ' (' . 'Photographed by' . ' ' . $attachment_credit . ')';
        return $attr;
    add_filter('wp_get_attachment_image_attributes', 'filter_image_title', 10, 2);

    As far as I can tell, it should be affecting every image, but isn't. Is this a bug or am I using the wrong filter? The complete discussion (which amounts to me and one answer, but has a bit more history of the problem) can be found here.

Topic Closed

This topic has been closed to new replies.

About this Topic