If you want to exit the loop after you find an attachment, use break
.
// Define $programme and pull up $attachments as above
$attachment_url="";
foreach ( $attachments as $attachment ) {
if ($attachment->post_title == $programme) {
$attachment_url = wp_get_attachment_url($attachment->ID);
break;
}
}
if ( $attachment_url ) {
echo $attachment_url;
} else {
echo '/images/nontvimg.jpg';
}
Related Posts:
- wp_get_attachment_image_src calling wrong location
- How To Retrieve An Image Attachment’s Alt Text?
- Programmatically adding images to media library
- Programmatically adding images to the media library with wp_generate_attachment_metadata randomly fails
- getting attachement images src and add classes
- Uploaded images don’t show in Media Library if there are special characters in IPTC Keywords
- How can I bulk delete media and attachments using WP-CLI?
- Check if post has attachments (not image)
- Validate alt text for attachments?
- complex restriction of items in media library
- Edit image / Delete image link
- Applying automatic link class to images embedded to posts
- media_sideload_image file name?
- Get all posts (of any post type) an attachment is used in
- Unattaching unlinked images
- How can I hide media library images from general users?
- Show Post ID in “Find Posts or Pages” box in Media Library?
- get post attachments of a particular size
- Change image link: wp_get_attachment_link
- Gel all image from certain post type
- Making images from single.php pointing to an attachment .php template
- Can i hide the attachments from media which I uploaded from front end?
- Get the attachment URL on single.php
- wp_get_attachment_image_src always returns false
- Exact image sizes
- Get post embedded image caption
- Get the size (size in Kb or MB) of an featured image?
- Regenerate missing _wp_attachment_metadata
- query attachment images … if no attachments -> get attachments of parent page?
- Edit default image attachment size
- Perform a custom (bulk) action in media view
- Change code to display image attachment page
- Attachment 0×0 size error [closed]
- Add image attachment to post via coding?
- Removing width and height attributes from wp-get-attachment
- How to retrieve alt attribute for an attachment (uploaded image)?
- Get post attachment images dimension and use in embed code
- use wp_get_attachment_image() to show attachments
- Adding width and height to wp_get_attachment_image_src
- Get the bare URL for an attachment
- Display attachment resolution size
- How to check if there is image title, caption, description?
- Where does the gallery shortcode gets its attachments from?
- How to remove attachments size attribute [duplicate]
- How to redirect ‘file url’ to ‘post url’ i.e. in attachment.php
- Control attachments menu order with jQuery Sortable
- Is it possible to share media files between multiple WordPress sites?
- Open the Attachment Details popup
- Check what attached image size is used
- How do I get all attachments that are used in the current post, not just the children?
- Clean up images, delete unused image sizes attached to a post type
- Add attachment for all images in post
- Proper use of wp_get_attachment_image?
- how to get random media id from media gallery
- Get title of post image is attached to
- Get images attached to a specific page
- Correct attachment size
- Post Gallery list attachments except the one used as post thumbnail
- WordPress Attachments with old images
- Get image attachment to a post outside the loop?
- How do I intercept and modify the functionality of wp_get_attachment_image()?
- Upload multiple image with media_handle_upload with multiple file field [duplicate]
- Unable to get wp_get_attachment_url to show
- Get Attachment Image Class by attachment id
- Possible to add “Even/Odd Classes” to image attachments via wp_get_attachment_image?
- changing the default picture size link url
- Auto delete content in specific folder inside media library
- Media Library Mess After FTP … /uploads
- Media item permalink goes to source file instead of attachment.php
- How to view exact image dimensions of a media in Admin view?
- Filter not applied to all image sizes
- Get attachment URL from attachment Link in WordPress
- Removing images deleted from uploads folder, from Media Library
- Display setting ‘size’ not available in image settings modal
- WP media attachment location vs permalink
- Removing delete permanently button in uploading files media
- Retrieve ONLY mp3s and set a variable for use in a playlist called by single.php template?
- Hide Image Container if No Image Attachments
- Incomplete attachment page
- Is there a way to add Reattach and Unattach links in image gallery?
- How to retrieve “custom url” from image attachment in wordpress
- Retreive tags from attached post for media on page
- list of attachments: wp_get_attachment_link works, wp_get_attachment_image does not
- How to retrieve images contained in a post
- What is the most efficient way to retrieve a group of image URL’s from different attachment ID’s of a specific crop size?
- How to save media-form on custom tab
- get_image_tag filter not working
- Change attachment inside the_content using filters
- Bulk delete media by year
- How can I use an image from an external source without uploading it to the media library
- Media library upload hook
- remove missing image attachments
- custom fields for attachments?
- ID of images from gallery
- Return Attachments from Custom Post Type
- How to upload WordPress Images to remote web server and display those images in WordPress gallery [closed]
- Is there a function for getting the post/page that an attachment is related to?
- wp_mail Allow Remote Attachments from CDN
- Get all image in media Gallery with alt/title?
- When inserting attachments into a blog post, how can I get WordPress to use the full filename with the extension?