get_the_post_thumbnail_url
Using get_the_post_thumbnail_url – a function in WordPress that is used to retrieve the URL of a thumbnail (preview) image associated with a particular post or post:
<div class="post-item"> <a href="<?php the_permalink($post->ID) ?>"> <img src="<?php echo get_the_post_thumbnail_url( $post->ID, 'thumbnail'); ?>" width="180" height="150" class="post-item-image" alt="<?php the_title(); ?>" title="<?php the_title(); ?>" /> <div class="post-item-title"> <?php echo get_the_title($post->ID) ?> </div> </a> </div>
get_the_post_thumbnail
Using get_the_post_thumbnail – returns the HTML code of the image that was set as the thumbnail for the specified post. If the thumbnail has not been set, the function will return an empty value:
$title = get_the_title(); $params = array('class' => 'whyweimages', 'title' => $title , 'alt' => $title); echo get_the_post_thumbnail($post->ID, 'medium', $params);
Custom example with srcset
:
Image example with srcset:
if (has_post_thumbnail()) { ?> <img class="lazyload recent-posts-item-img" alt="<?php esc_html(the_title()); ?>" title="<?php esc_html(the_title()); ?>" srcset="<?php echo get_the_post_thumbnail_url(get_the_ID(), 'thumbnail'); ?> 150w, <?php echo get_the_post_thumbnail_url(get_the_ID(), 'medium'); ?> 280w" sizes="(min-width: 768px) 280px, 150px" src="<?php echo get_the_post_thumbnail_url(get_the_ID(), 'medium'); ?>"> <?php } else {} ?>
With function:
add to your theme function.php file:
function custom_vchitel_post_thumbnail() { if (post_password_required() || is_attachment() || !has_post_thumbnail()) { return; } $title = get_the_title(); $params = array('class' => 'vchitel-thumbnail', 'title' => $title, 'alt' => $title); echo get_the_post_thumbnail(get_the_ID(), 'medium', $params); }
in the template:
<?php custom_vchitel_post_thumbnail(); ?>