Bird
0
0

You want to show a featured image only if it exists, otherwise show a default image located at /images/default.jpg. Which code snippet correctly does this?

hard📝 Application Q15 of 15
Wordpress - Content Management
You want to show a featured image only if it exists, otherwise show a default image located at /images/default.jpg. Which code snippet correctly does this?
Aif (has_post_thumbnail()) { the_post_thumbnail(); } else { echo '<img src="/images/default.jpg" alt="Default">'; }
Bthe_post_thumbnail() ?: '<img src="/images/default.jpg" alt="Default">';
Cif (the_post_thumbnail()) { the_post_thumbnail(); } else { echo '<img src="/images/default.jpg" alt="Default">'; }
Dhas_post_thumbnail() ? the_post_thumbnail('/images/default.jpg') : '';
Step-by-Step Solution
Solution:
  1. Step 1: Check condition for featured image existence

    Use has_post_thumbnail() to check if the post has a featured image.
  2. Step 2: Display featured image or default image

    If true, display with the_post_thumbnail(). Otherwise, echo an <img> tag with the default image path.
  3. Final Answer:

    if (has_post_thumbnail()) { the_post_thumbnail(); } else { echo '<img src="/images/default.jpg" alt="Default">'; } -> Option A
  4. Quick Check:

    Check existence, show image or default [OK]
Quick Trick: Use has_post_thumbnail() with if-else for fallback image [OK]
Common Mistakes:
  • Using the_post_thumbnail() in condition instead of has_post_thumbnail()
  • Passing image path as argument to the_post_thumbnail() incorrectly
  • Not providing an else fallback for missing images

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Wordpress Quizzes