简体   繁体   English

在Wordpress中的“帖子页面”上显示精选图像

[英]Show featured image on 'Posts Page' in Wordpress

So for all of the other pages on my wordpress site I am able to display the featured image for the page. 因此,对于我的wordpress网站上的所有其他页面,我能够显示页面的特色图像。 However, on the page that all of my posts are displayed the featured image does not display even if it is set. 但是,在显示我的所有帖子的页面上,即使设置了特色图像,也不会显示。

Here is the code I am using to display the featured image on all other pages. 这是我用来在所有其他页面上显示特色图像的代码。

<?php if ( has_post_thumbnail() ): {
    $src = wp_get_attachment_url(get_post_thumbnail_id($post->ID));
}?>

<div class="featured-image-full-width" style="background-image: url( <?php echo $src; ?> ) !important; height: 400px; background-size: cover; background-repeat: no-repeat; background-position: center;"></div>

<?php endif; ?>

This does not work on the page that is chosen to display posts though. 这不适用于选择显示帖子的页面。 Keep in mind that I need to display the featured image as a background image so that it is full width of the page and not stretched. 请记住,我需要将特色图像显示为背景图像,以便它是页面的整个宽度而不是拉伸。 (IE and Edge don't support "object-position" so this is my way of working around that) (IE和Edge不支持“对象位置”所以这是我的解决方法)

Let me know if anything is not clear. 如果有什么不清楚,请告诉我。

After spending the past two hours researching and trying different things I was able to figure out a solution. 经过两个小时的研究和尝试不同的事情,我能够找到一个解决方案。

<?php if(is_home()) {
    $img = wp_get_attachment_image_src(get_post_thumbnail_id(get_option('page_for_posts')),'full');
    $featured_image = $img[0];
}?>

<div class="featured-image-full-width" style="background-image: url( <?php echo $featured_image ?> ) !important; height: 400px; background-size: cover; background-repeat: no-repeat; background-position: center"></div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM