Performance: Featured images
Featured images impact page load speed and visual stability by affecting image size, loading behavior, and layout shifts.
Jump into concepts and practice - no test required
<?php the_post_thumbnail('medium_large', ['decoding' => 'async']); ?>
<?php the_post_thumbnail('full'); ?>| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Full-size featured image without lazy loading | 1 image node | 1 reflow on image load | High paint cost due to large image | [X] Bad |
| Resized featured image with dimensions | 1 image node | 0 reflows after initial layout | Low paint cost with reserved space | [OK] Good |
has_post_thumbnail() to check if a post has a featured image.the_post_thumbnail() displays the image, not checks it. Other options are not valid WordPress functions.if (has_post_thumbnail()) {
the_post_thumbnail('medium');
} else {
echo 'No image';
}has_post_thumbnail().the_post_thumbnail('medium'), which displays the image in medium size.if (has_post_thumbnail) {
the_post_thumbnail();
}has_post_thumbnail is a function and must be called with parentheses ().the_post_thumbnail() can be called without arguments; the semicolon is present after it./images/default.jpg. Which code snippet correctly does this?has_post_thumbnail() to check if the post has a featured image.the_post_thumbnail(). Otherwise, echo an <img> tag with the default image path.