I have a 'Match' CPT to create posts for each game played by a football team. These posts display details of the opposition team by pulling through information from the appropriate post from another CPT - 'Clubs'. The one bit I've not been able to work out is how to get the thumbnail.

The current code is:

    <h2 class="match-team">
        <span class="match-badge">
        $opposition = get_field('club');
        <img src="https://www.MYDOMAIN.co.uk/images/club/<?php echo $opposition->post_name; ?>.jpg" class="match-badge-img">
        <a class="match-name" href="/clubs/<?php echo $opposition->post_name; ?>/" >
                <?php echo $opposition->post_title;?>
    <?php endif; ?>
    <span class="match-score"><?php the_field('opposition_goals'); ?></span>

The current img src does work IF the club has a unique image that has been uploaded i.e. club 1 has an image or club 13 has an image. But this is not true of all clubs, e.g. club 2 doesn't have an image. Therefore, this returns a broken image link.

Clubs without an image have a generic placeholder as their featured image, so the solution I'm aiming for is to change the img src path so that it pulls in that featured image. I've used this, but to no effect:

<img src="<?php echo wp_get_attachment_url( get_post_thumbnail_id($opposition->post_name) ); ?><?php echo get_the_post_thumbnail($opposition->post_name); ?>" class="match-badge-img">


<?php echo $opposition->the_post_thumbnail('medium', array( 'class' => 'match-badge-img' ) ); ?>

But neither pull through the appropriate thumbnail, which suggests I've gone wrong with something pretty fundamental. Can $opposition-> be used in this context?

1 Answer 1


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( '/' . $image_path );
} elseif ( has_post_thumbnail( $opposition ) ) { // Then the post thumbnail.
    $thumbnail = get_the_post_thumbnail_url( $opposition, 'medium' );

if ( ! empty( $thumbnail ) ) : ?>
    <img src="<?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="<?php echo esc_url( home_url( '/' . $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.

  • 1
    You were right to assume the image file ends in .jpg – and that has solved the issue! Jul 14, 2019 at 16:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.