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?
1 Answer
Reset to default 2If 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
版权声明:本文标题:Show Post Thumbnail In Custom Post From Other CPT 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745324772a2653539.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论