Function Name: get_attached_media
The get_attached_media() function is a WordPress function that is used to retrieve attached media from a post or a page. Attached media can include images, videos, audio files, and other media types that are related to the post or page.
This function returns an array of media objects that are attached to the specified post or page. Each media object contains details about the media file, such as the file URL, file type, and file size.
The get_attached_media() function can be useful for displaying a gallery of images or videos that are related to a post or page. It can also be used to retrieve specific media files for other purposes, such as displaying a featured image.
Example Usage:
Let’s say you want to display a gallery of images that are attached to a post with ID 123. You can use the get_attached_media() function to retrieve the media objects, and then loop through them to display the images.
<?php
$media_items = get_attached_media( 'image', 123 );
foreach( $media_items as $media ) {
echo '<img src="' . $media->guid . '">';
}
?>
In this example, we’re using the get_attached_media() function to retrieve all media items of type "image" that are attached to the post with ID 123. We’re then looping through the media items and displaying each image using an HTML img tag.