开发者

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 conte开发者_JAVA百科nt. 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?

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!


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;
    }
}
?>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜