Get the Featured Image of a Parent Page in WordPress
When you’re building a number of pages in WordPress that are closely tied to a main, or parent page, tying them together with imagery can help the user know where they are.
This snippet is from the same book project as this snippet that gets the title of the parent page. It allows us to show the same header image on all subsequent chapters of a book (child pages)>
The snippet
<?php echo wp_get_attachment_url( get_post_thumbnail_id($post->post_parent) ); ?>
srcset for the responsive
The beauty of this snippet is that it uses WordPress’ built in responsive image support, introduced in WordPress 4.4. This allows a browser to use the best sized image available using the srcset
attribute, potentially saving bandwidth and speeding up page load times in the process.
Put it in a template file
This code is meant to be placed anywhere in a template file that you’d need to display the Featured Image of a parent page.
Awesome. perfect for a site I’m building for a client. Thanks for sharing it!