admin管理员组

文章数量:1420197

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">
        <?php 
        $opposition = get_field('club');
        if($opposition):
        ?>
        <img src="/<?php echo $opposition->post_name; ?>.jpg" class="match-badge-img">
        </span>
        <a class="match-name" href="/clubs/<?php echo $opposition->post_name; ?>/" >
        <span>
                <?php echo $opposition->post_title;?>
        </span>
        </a>                
    <?php endif; ?>
    <span class="match-score"><?php the_field('opposition_goals'); ?></span>
    </h2>

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">

and

<?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?

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

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">

and

<?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?

Share Improve this question asked Jul 13, 2019 at 8:01 Pete HaymanPete Hayman 719 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 2

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.

本文标签: Show Post Thumbnail In Custom Post From Other CPT