@alasdair17 This is a great idea to handle lightbox captions on mobile. maybe @arminunruh has already integrated an feature like "show caption beneath image on mobile" when the lightbox is turned off on mobile?
Another way would be to get into lightbox settings and set "Image File Caption" as the Default Caption for the Lightbox. You can then give your Image in the Media Section a Description that will be used as Lightbox caption. The following code that goes into Custom CSS/HTML in the Head section will do the rest.
<script>
function addCaptionsForMobile() {
// Check if the HTML tag has the class "phone-size"
if (jQuery("html").hasClass("phone-size")) {
// Select all divs with the class 'img' that have a data-lightbox-caption attribute
jQuery(".img[data-lightbox-caption]").each(function() {
var $div = jQuery(this);
var captionHTML = $div.attr("data-lightbox-caption");
if (captionHTML) {
// Check if the caption is already appended to avoid duplication
if ($div.find(".custom-caption").length === 0) {
// Create a new element for the caption and add it to the div
var $caption = jQuery('<div class="custom-caption"></div>').html(captionHTML);
$div.append($caption);
}
}
});
} else {
// Remove captions if switching to a non-mobile layout
jQuery(".custom-caption").remove();
}
}
// Run the function when a new page is shown
window.laytheme.on("newpageshown", function() {
addCaptionsForMobile();
});
// Add a resize listener to recheck the layout on window resize
jQuery(window).on("resize", function() {
addCaptionsForMobile();
});
</script>