How to create 'Local' filters for WordPress srcset sizes in template parts?

I've read a bunch of tutorials on how to create filters for srcsets and srcset sizes, but they all appear to be applied globally, with a fixed width or set of widths for sizes.

The trouble is, I pull in content from posts and pages in template parts throughout my sites, and these typically display the featured image in a much smaller context.

So although it's properly using Wordpress's responsive images, the width by default is set to 100vw, so even if it's never displayed at larger than 1/3rd of the screen width, it'll still be loading a srcset intended for full-width screens.

How can I create individual filters (or any other appropriate method), for each template part so I can control more precisely the sizes in my srcsets?

Topic responsive filters images templates Wordpress

Category Web

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.