自定义存档缩略图,而不是最新帖子(Wordpress)的缩略图



我的(类别(存档页面显示该循环中最新帖子的缩略图。现在我不希望这种情况发生,并给我的存档页面一个标准的缩略图。我该怎么做?

在我的标题中,我有:

<?php $thumb = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'full' );?>
<div class="banner-image" <?php if ( has_post_thumbnail() ) { ?>
style="background-image:url('<?php echo $thumb['0'];?>');"
<?php } else { ?>
style="background-image:url('<?php bloginfo('template_directory'); ?>/images/bannershape.svg');" <?php } ?>
</div>

但不幸的是,这行不通。有谁知道我如何为我的存档页面提供标准缩略图,而不是显示最新帖子的缩略图?

编辑:6/30/2020 -我想我误读了你的问题,所以让我补充更多细节。在存档类型页面上,有许多条件标记,例如is_category(((等等(。你可能想要其中之一。您可以转到上面的链接查看所有选项,如果 is_category(( 不适合您的情况。

选项 #1

if (is_category()) {
$img_url = get_template_directory_uri() . '/images/bannershape.svg';
}
else {
$img_url = get_the_post_thumbnail_url($post, 'full') ?: get_template_directory_uri() . '/images/bannershape.svg';
}
<div class="banner-image" style="background-image:url('<?= $img_url ?>');"></div>

选项 #2(不太严格(

$default_img_url = get_template_directory_uri() . '/images/bannershape.svg';
$img_url = is_singular() && ($thumb_url = get_the_post_thumbnail_url($post, 'full')) ? $thumb_url : $default_img_url;
<div class="banner-image" style="background-image:url('<?= $img_url ?>');"></div>

选项 #3不确定,但根据您正在做的事情,您可能可以使用此单行

代码
$img_url = get_the_post_thumbnail_url(null, 'full') ?: get_template_directory_uri() . '/images/bannershape.svg';
  • nullget_the_post_thumbnail_url只会获取当前页面/帖子
  • get_the_post_thumbnail_url"安全"(通过已内置的健全性检查没有错误(在找不到$post或找不到 $_thumbnail_id 时返回FALSE
  • $a ?: $b$a ? $a : $b的速记三元

原始答案:如果你在页面、帖子、术语等,你可以通过以下两个函数获取原始查询对象:get_queried_object(( 或 get_queried_object_id((。这将分别为您提供当前页面/对象(或帖子、术语等(的$object或当前页面(/etc(的$object_id。

最新更新