Skip to content

Instantly share code, notes, and snippets.

@govindak
Last active April 25, 2019 03:54
Show Gist options
  • Save govindak/7435288 to your computer and use it in GitHub Desktop.
Save govindak/7435288 to your computer and use it in GitHub Desktop.
Get the featured image by page ID in WordPress
<?php
//page id
$page_id = "5"; //example
if (has_post_thumbnail($page_id) ):
$image = wp_get_attachment_image_src( get_post_thumbnail_id($page_id), 'single-post-thumbnail' );
endif;
$image_URI = $image[0];
?>
You can also use an array to fetch images from multiple pages.
<?php
//page id
$page_id = array('5', '10', '17'); //example stores multiple page ID in an array
//render image for each page using foreach conditional loop
foreach($page_id as $id){
if (has_post_thumbnail($id) ):
$image = wp_get_attachment_image_src( get_post_thumbnail_id($id), 'single-post-thumbnail' );
endif;
echo "<img src='".$image[0]."'>";
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment