Skip to content

Instantly share code, notes, and snippets.

@htmgarcia
Last active October 17, 2024 14:50
Show Gist options
  • Save htmgarcia/d420e4a900bfdcfce4ed514f9a064d54 to your computer and use it in GitHub Desktop.
Save htmgarcia/d420e4a900bfdcfce4ed514f9a064d54 to your computer and use it in GitHub Desktop.
Display image title attribute above image in MetaSlider
<?php
// Replace 123 number in .metaslider-123 with the actual slideshow ID
function image_title_above_inline_script() {
wp_enqueue_script('jquery');
$inline_js = "
(function($) {
$(document).on('metaslider/initialized', function (e, identifier) {
if ($(identifier).closest('.metaslider.metaslider-123').length) {
var slideshow = $(identifier);
// We look for slide images with title attribute
slideshow.find('.slides > li.ms-image').each(function() {
var slide = $(this);
var img = slide.find('img');
var title = img.attr('title') && img.attr('title').trim() !== '' ? img.attr('title') : false;
if (title) {
img.before('<p>' + title + '</p>');
}
});
}
});
})(jQuery);
";
wp_add_inline_script('jquery', $inline_js);
}
add_action('wp_enqueue_scripts', 'image_title_above_inline_script');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment