Get single WordPress gallery image with given title

I am having users of a WordPress blog upload a single image to different post’s galleries with the name “banner”. Basically, every post will have an image named “banner” uploaded to its gallery and this image needs to be displayed on the post’s page outside of the article content. So, how can I get the URL to display an image in a post’s single.php template?

Can I iterate through the images of a given post’s gallery and find the one with the correct title somehow?

Read More

I’ve searched through the WordPress codex docs and haven’t found anything on a way to do this, just information on displaying galleries of photos. Note that I’m already using WordPress’s post thumbnail feature for something else.

Thanks!

Related posts

Leave a Reply

1 comment

  1. I think I finally figured it out. The part I couldn’t figure out was using the vaguely named get_children() function.

    <?php
    have_posts(); //must be in the loop
    the_post(); //set the ID
    $images =& get_children(array('post_mime_type' => 'image', 'post_parent' => get_the_ID()));
    rewind_posts(); //rewind for the actual loop
    
    $image_url = null;
    foreach($images as $image) {
        if($image->post_title == 'banner') {
            $image_url = $image->guid;
            break;
        }
    }
    ?>