Output the description of a Types image field

Images added to the Media Gallery can include a description field, but there is no way to output this description for a Types image field.

The following registers a shortcode ‘description’ which takes the URL of the image as an attribute and outputs the description for that image.

/**
 * Custom shortcode to output description of image field
 */
add_shortcode( 'description', function( $atts=[] ){

	$output = "";

	if ( isset( $atts['url'] ) ) {

		$img_id = attachment_url_to_postid( $atts['url'] );

		$img = get_post( $img_id );

		if ( !is_null( $img ) ) {
			$output = $img->post_content;		
		}
	}

	return $output;
});

/* To be used like so:

[description url=""]

*/

Let us know if this snippet is not working for you:

This snippet doesn’t work
0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments