2015-08-31 131 views
5

因此,對於我的WordPress站點上的所有其他頁面,我可以顯示該頁面的精選圖片。但是,在顯示我所有帖子的頁面上,即使設置了特色圖像也不會顯示。在WordPress的'帖子頁面'上顯示精選圖片

這是我用來在所有其他頁面上顯示精選圖像的代碼。

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

這不適用於選擇顯示帖子的頁面。請記住,我需要將精選圖像顯示爲背景圖像,以便它是整頁寬度,而不是拉伸。 (IE和Edge不支持「對象位置」,所以這是我的工作方式)

讓我知道如果有什麼不清楚。

回答

5

在過去兩個小時的研究和嘗試不同的事情後,我找到了解決方案。

<?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> 
相關問題