Last active
April 25, 2019 03:54
-
-
Save govindak/7435288 to your computer and use it in GitHub Desktop.
Get the featured image by page ID in WordPress
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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