简体   繁体   English

Wordpress 添加响应 srcset header 图像到主题

[英]Wordpress add responsive srcset header image to theme

WP introduced support for srcset in version 4.4 for Thumbnails and Post images. WP 在 4.4 版中为缩略图和发布图像引入了对 srcset 的支持。 But I can't find a way to make the page header responsive.但是我找不到使页面 header 响应的方法。 Here is how I embed the Page header:以下是我如何嵌入页面 header:

<img src="<?php header_image() ?>" alt="">  

This loads the header image (which can be uploaded in the backend > Design > Customise) in an src .这会在src中加载 header 图像(可以在后端 > 设计 > 自定义中上传)。 But I'd rather include all custom image sizes (that I added in functions.php) of this image and put them in an srcset attribute.但我宁愿包含此图像的所有自定义图像大小(我在 functions.php 中添加)并将它们放在srcset属性中。 But there seems to be only one size for the header?但是header好像只有一种尺寸?

It won't be easy, but this is how you make Header images (banner) responsive with a srcset. 这不是一件容易的事,但这就是您使Header图像(横幅)以srcset响应的方式。

ps.: Please fix this, wordpress! ps .:请解决此问题,wordpress! Responsive Headers should be part of the srcset update. 响应头应该是srcset更新的一部分。

Solution: We never use the WP header_image(); 解决方案:我们从不使用WP header_image(); function, but instead only just use the custom header as an "Uploader" for our image that we then embed manually: 函数,但仅将自定义标头用作我们图像的“上传器”,然后将其手动嵌入:

  1. Extract the attachement ID of the Header image 提取标题图片的附件ID
  2. Manually load src and srcset of this attachement ID 手动加载此附件ID的src和srcset

header.php header.php

<?php
// Extract header attachement ID
$data = get_theme_mod('header_image_data');
$data = is_object($data) ? get_object_vars($data) : $data;
$header_id = is_array($data) && isset($data['attachment_id']) ? $data['attachment_id'] : false;
if($header_id) :
    // Set image sources manually
    $src = wp_get_attachment_image_src( $header_id, 'thumbnail-head' )[0];
    $srcset = wp_get_attachment_image_srcset( $header_id, 'thumbnail-head' ); ?>
    <img id="masthead-bg" src="<?php echo $src ?>" srcset="<?php echo $srcset ?>" sizes="100vw" alt="">
<?php endif; ?>

In this example thumbnail-head is my destination image size and aspect ratio. 在此示例中, thumbnail-head是我的目标图像尺寸和纵横比。 You can use whatever size you want (needs to have a fixed aspect ratio). 您可以使用任意大小(需要具有固定的宽高比)。 We now have to add this image size to the functions.php file. 现在,我们必须将此图像大小添加到functions.php文件中。 In order to get larger sizes of this thumbnail (to embed in the srcset) you also have to add more sizes to the functions.php: 为了获得更大的缩略图尺寸(嵌入到srcset中),您还必须向functions.php添加更多尺寸:

functions.php functions.php

add_image_size( 'thumbnail-head', 450, 300, true );   
add_image_size( 'thumbnail-head-2x', 900, 600, true );   
add_image_size( 'thumbnail-head-4x', 1800, 1200, true ); 

My thumbnail size is 450 x 300 pixel (3:2 aspect ratio). 我的缩略图大小为450 x 300像素(长宽比为3:2)。 So I added a 2x and 4x version. 所以我添加了2x和4x版本。 Don't forget to rebuild thumbnails via plugin. 不要忘记通过插件重建缩略图。

Finally it's important to set the dimensions of the custom header image to the largest version of your thumbnail. 最后,将自定义标题图片的尺寸设置为缩略图的最大版本很重要。 This is because WP cuts the image down to this size and creates the other sizes afterwords based on this cut down image. 这是因为WP将图像缩小到此尺寸,并基于该缩小的图像创建其他尺寸的后缀。 In this case search for your custom header in the functions.php and set width and height to 1800 and 1200. We also disable "flex-width" and "flex-height" to force the same aspect ratio as our added image sizes. 在这种情况下,请在functions.php中搜索您的自定义标头,并将width和height设置为1800和1200。我们还禁用了“ flex-width”和“ flex-height”以强制与我们添加的图像尺寸相同的纵横比。

functions.php functions.php

function fs_custom_header_setup() {
    add_theme_support( 'custom-header', apply_filters( 'fs_custom_header_args', array(
        'default-image'          => '',
        'header-text'            => false,
        'default-text-color'     => 'ffffff',
        'width'                  => 1800,
        'height'                 => 1200,
        'flex-width'             => false,
        'flex-height'            => false,
        'wp-head-callback'       => 'fs_header_style',
    ) ) );
}
add_action( 'after_setup_theme', 'fs_custom_header_setup' );

This is now possible using the_header_image_tag();现在可以使用the_header_image_tag(); . .

You might want to use this, to avoid WP from hardcoding a height and width for responsive images:您可能想使用它来避免 WP 对响应图像的高度和宽度进行硬编码:

    the_header_image_tag( array(
        'width'     => "",
        'height'    => "",
    ));

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

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