Saturday, May 21, 2016

WordPress get featured image by post id

In WordPress there is featured image for Post and Pages and Custom Post types.

To get the feature image of an post or page or custom post type use the following code.

<?php if (has_post_thumbnail( $post->ID ) ):
$image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' );
 endif;
?>

Now in $image you have all the feature image information. To display the image you can do it as follows:

<img src="<?php echo $image[0]; ?>" />

And you are done. Now feature image will be displayed.

No comments:

Post a Comment

MS SQL : How to identify fragmentation in your indexes?

Almost all of us know what fragmentation in SQL indexes are and how it can affect the performance. For those who are new Index fragmentation...