php 我怎么能减少主页和后页缩略图图像时,相同的缩略图双方

jk9hmnmh  于 11个月前  发布在  PHP
关注(0)|答案(1)|浏览(78)

我想改变主页和博客文章页面缩略图大小。我注意到缩略图大小通过同一类'大'在两个页面上生成。
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代码编辑.

63lcw9qa

63lcw9qa1#

您需要做的:

  • 定义新的图像大小:向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);

字符串

相关问题