Random WordPress Function

Learn about a new WordPress function every day!


Function Signature:

wp_get_attachment_caption

Function Description:

Retrieves the caption for an attachment.

Function Examples:

⚠️ Examples below are generated with GPT-3 once every hour. Do not take them too seriously.
Consider them as some extra input in your learning process - reason about them. Will it work? What could fail?
// Example 1: Basic usage of wp_get_attachment_caption to display the caption of an image attachment
$image_id = 123;
$caption = wp_get_attachment_caption($image_id);
echo $caption;
// Example 2: Checking for empty caption before displaying using wp_get_attachment_caption
$image_id = 456;
$caption = wp_get_attachment_caption($image_id);
if(!empty($caption)){
    echo $caption;
} else {
    echo "No caption available for this image.";
}
// Example 3: Using wp_get_attachment_caption within a loop to display captions for multiple image attachments
$gallery_images = get_posts(array('post_type' => 'attachment', 'post_mime_type' => 'image'));
foreach($gallery_images as $image){
    $caption = wp_get_attachment_caption($image->ID);
    if(!empty($caption)){
        echo "

" . $caption . "

"; } }