beans_get_post_attachment

Get attachment data.

This function regroups all necessary data about a post attachment into an object.

beans_get_post_attachment( string $post_id, string $size = 'full' )

Return: (object) Post attachment data.

Parameters

NameTypeRequiredDefaultDescription
$post_idstringtrue-The post id.
$sizestringfalse'full'The desired attachment size. Accepts 'thumbnail', 'medium', 'large' or 'full'.

Source

function beans_get_post_attachment( $post_id, $size = 'full' ) {

	$id = get_post_thumbnail_id( $post_id );
	$post = get_post( $id );
	$src = wp_get_attachment_image_src( $id, $size );

	$obj = new stdClass();
	$obj->id = $id;
	$obj->src = $src[0];
	$obj->width = $src[1];
	$obj->height = $src[2];
	$obj->alt = trim( strip_tags( get_post_meta( $id, '_wp_attachment_image_alt', true ) ) );
	$obj->title = $post->post_title;
	$obj->caption = $post->post_excerpt;
	$obj->description = $post->post_content;

	return $obj;

}