解決WordPress封面圖裁切的問題

WordPress文章中的封面圖會依照版型的設定去置換圖片的尺寸,如果你希望解決這個問題,可以這樣做

找出控制封面圖的程式

我從佈景主題的檔案中,找到與封面圖有關的程式,分別是

template-parts/content.php

	<aside class="entry-thumbnail">
		<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_post_thumbnail( 'boston-list-medium' ); ?></a>
	</aside>

template-parts/content-featured.php

	<aside class="entry-thumbnail">
		<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
			<?php the_post_thumbnail( 'boston-featured-medium' ); ?>
		</a>
	</aside>

修改程式

這時候只要做一件事,把 'boston-list-medium' 改成 'full'

改完後存檔後就能顯示原始沒被裁切的圖

如果不想改版型程式,可以使用function

如果不想改佈景主題的原始檔,還有一個進階的作法使用 Function

function set_custom_post_thumbnail_size($html, $post_id, $post_thumbnail_id, $size, $attr) {
    if ($size === 'boston-list-medium') {
        $size = 'full';
        $html = get_the_post_thumbnail($post_id, $size, $attr);
    }
    return $html;
}
add_filter('post_thumbnail_html', 'set_custom_post_thumbnail_size', 10, 5);

後記,範例是使用 boston 這個佈景主題,如果是使用其他的佈景主題則依照實察的值調整

瀏覽次數:54