If you want to get the post thumbnail URL, you can use get_the_post_thumbnail_url()
:
<?php $image_path="images/club/" . $opposition->post_name . '.jpg'; // relative to the root directory
if ( @file_exists( ABSPATH . $image_path ) ) { // Check the unique image first.
$thumbnail = home_url( "https://wordpress.stackexchange.com/" . $image_path );
} elseif ( has_post_thumbnail( $opposition ) ) { // Then the post thumbnail.
$thumbnail = get_the_post_thumbnail_url( $opposition, 'medium' );
}
if ( ! empty( $thumbnail ) ) : ?>
<img src="https://wordpress.stackexchange.com/questions/342788/<?php echo esc_url( $thumbnail ); ?>" class="match-badge-img" />
<?php endif; ?>
If you want to get the post thumbnail HTML, you can use get_the_post_thumbnail()
:
<?php $image_path="images/club/" . $opposition->post_name . '.jpg'; // relative to the root directory
if ( @file_exists( ABSPATH . $image_path ) ) : // Check the unique image first. ?>
<img src="https://wordpress.stackexchange.com/questions/342788/<?php echo esc_url( home_url("https://wordpress.stackexchange.com/" . $image_path ) ); ?>" class="match-badge-img" />
<?php elseif ( has_post_thumbnail( $opposition ) ) : // Then the post thumbnail. ?>
<?php echo get_the_post_thumbnail( $opposition, 'medium', [ 'class' => 'match-badge-img' ] ); ?>
<?php endif; ?>
There are also the_post_thumbnail_url()
and the_post_thumbnail()
, but these functions are used for the current post in The Loop and not a custom post object like in your case, the $opposition
.
And in the above examples, I’m assuming the name of the unique image file always ends with .jpg
.