我想更改主页和博客文章页面缩略图大小。我注意到缩略图大小是通过两个页面上的同一类“large”生成的。
html代码是:
home page code
<img width="750" height="422" src="https://amazquiz.com/wp-content/uploads/2023/11/amazon-fz-points-weekly-qu…" class="attachment-large size-large wp-post-image" alt="amazon-fz-points-weekly-quiz-answer" itemprop="image" decoding="async" srcset="https://amazquiz.com/wp-content/uploads/2023/11/amazon-fz-points-weekly-qu…" sizes="(max-width: 750px) 100vw, 750px">
Blog post code
<img width="750" height="422" src="https://amazquiz.com/wp-content/uploads/2023/11/amazon-fz-points-weekly-quiz-answer.jpg" class="attachment-large size-large wp-post-image" alt="amazon-fz-points-weekly-quiz-answer">
我尝试从 funtion.php 更改它,但没有改变。让我们了解 php 代码编辑。
您需要做什么:
functions.php
文件中以定义新的图像尺寸。实施示例:
function my_custom_image_sizes() {
add_image_size('homepage-thumb', 800, 450, true);
add_image_size('postpage-thumb', 600, 300, true);
}
add_action('after_setup_theme', 'my_custom_image_sizes');
function my_custom_thumbnails($html, $post_id, $post_image_id, $size, $attr) {
if(is_home() && $size == 'large') {
$size = 'homepage-thumb';
} elseif(is_single() && $size == 'large') {
$size = 'postpage-thumb';
}
$image = wp_get_attachment_image_src($post_image_id, $size);
if ($image) {
$html = '<img src="' . $image[0] . '" width="' . $image[1] . '" height="' . $image[2] . '" alt="' . get_the_title($post_id) . '">';
}
return $html;
}
add_filter('post_thumbnail_html', 'my_custom_thumbnails', 10, 5);