Independent Editorial DeskWordPress Releases, Builds, and Operations
Back to Archive
Implementation Notes

WordPress wp_get_attachment_image Example: Render Media Safely in Themes

Render WordPress attachment images with responsive attributes, alt text, and safe template output using wp_get_attachment_image.

Published

April 26, 2026

Reading Time

2 min read

Updated

April 26, 2026

Responsive WordPress media rendering with attachment image output and theme-safe image handling.
Build PatternImplementation Notes

Implementation Notes

Extension points, code paths, and implementation choices that should survive contact with production.

Best For

WordPress developers, agencies, and technical teams building custom plugin or theme functionality with cleaner operational defaults.

Primary Topics

Implementation Notes

Editorial Focus

Build Pattern: Extension points, code paths, and implementation choices that should survive contact with production. Updated on April 26, 2026.

Full Report

Last reviewed: April 26, 2026

Rendering image attachments by hand with custom <img> markup usually looks simple, but it often throws away responsive attributes that WordPress can generate automatically. The result is worse performance, weaker accessibility, and more manual attribute handling than necessary.

This guide shows how to use wp_get_attachment_image() so themes and plugins render attachment images with proper size handling, alt text, and loading hints.

Render an attachment with a registered size

<?php
echo wp_get_attachment_image(
	$attachment_id,
	'large',
	false,
	array(
		'class' => 'hero-media',
	)
);

Using a registered size is better than guessing dimensions manually. WordPress can then generate the correct image variant, classes, srcset, and sizes metadata automatically.

Override alt text or loading attributes when needed

echo wp_get_attachment_image(
	$attachment_id,
	'medium_large',
	false,
	array(
		'alt'           => 'Operations dashboard screenshot',
		'loading'       => 'lazy',
		'fetchpriority' => 'low',
	)
);

The attribute array lets the template stay deliberate without dropping down to handcrafted <img> tags for every variant.

Check for missing attachments

$image_html = wp_get_attachment_image( $attachment_id, 'thumbnail' );

if ( '' === $image_html ) {
	return;
}

echo $image_html;

The function returns an empty string on failure. Handle that case cleanly instead of assuming the attachment ID is always valid.

Why this is safer than manual image HTML

  • WordPress can add responsive srcset and sizes attributes.
  • Theme code stays shorter and less error-prone.
  • Attachment alt text can flow from the media library instead of being rebuilt ad hoc.
  • Loading and decoding hints can be controlled through the attribute array.

Production checklist

  • Prefer registered image sizes over arbitrary width/height arrays.
  • Handle the empty-string failure case.
  • Set or review alt text intentionally.
  • Use the attribute array for class, loading, decoding, and fetch priority overrides.
  • Do not rebuild image markup manually unless the output truly requires it.
  • Test large media blocks on mobile and desktop layouts.

Common mistakes

  • Passing the parent post ID instead of the attachment ID. The helper expects an attachment.
  • Ignoring alt text. Media library metadata still needs editorial review.
  • Hardcoding full image URLs in templates. That fights WordPress image optimization.
  • Using unregistered custom dimensions everywhere. Registered sizes are easier to optimize and reason about.
  • Forcing eager loading broadly. Reserve high-priority loading for truly critical images.

Related reading

If the attachment is created by plugin uploads, pair this with the media upload guide. If the image is displayed as a featured visual in templates, keep the output aligned with the template hierarchy article.

References and further reading

Popular Guides

Popular WordPress guides to read next.

These articles connect recurring production concerns: implementation details, updates, troubleshooting, recovery paths, and operational cleanup.

Continue Reading

More from the archive.

Diagnostic dashboard scene representing a WordPress Site Health review before major updates.
01Build Pattern
Implementation Notes

Build Pattern

Extension points, code paths, and implementation choices that should survive contact with production.

May 21, 2026 · 3 min read

WordPress Site Health Check Before Major Updates: What to Review First

A pre-update WordPress Site Health checklist covering loopbacks, connectivity, debug settings, and environment readiness.

Structured data and route review scene representing permalink validation after a WordPress migration.
02Build Pattern
Implementation Notes

Build Pattern

Extension points, code paths, and implementation choices that should survive contact with production.

May 21, 2026 · 3 min read

WordPress Permalink Checklist After Migration: Catch URL Problems Early

A post-migration WordPress permalink checklist for checking rewrite rules, post URLs, archives, and redirect noise.

Technical media workspace representing image preparation and optimization before upload to WordPress.
03Build Pattern
Implementation Notes

Build Pattern

Extension points, code paths, and implementation choices that should survive contact with production.

May 21, 2026 · 3 min read

WordPress Image Optimization Checklist: What to Fix Before Upload

A practical WordPress image optimization checklist covering dimensions, compression, formats, and Media settings before upload.