在WooCommerce单一产品页面的图像缩略图下方添加图像标题

时间:2019-02-24 18:46:49

标签: wordpress woocommerce thumbnails caption

我试图在WooCommerce单一产品页面的每个图像缩略图下方添加图像标题。

在这里您可以看到我希望在哪里显示文本(目前为“未定义”)

enter image description here

我要添加单个标题文本(而不是产品标题,即图像标题。每个图像都有不同的标题)。

enter image description here

有没有简单的方法可以做到这一点?我正在使用工具集,并且可以根据需要添加JavaScript代码段。

我看到了一篇有关此问题的文章,但无法弄清楚将代码放在何处:

Show caption under product gallery in WooCommerce

我也试图将这段代码添加到我的Toolset JavaScript编辑器中,但是我得到的是产品标题,而不是图像标题(或标题)。 (Suggested as a solution in that post)

jQuery(window).load(function(){
if( jQuery('body').hasClass('single-product') ){
var imgtitles = [];
jQuery('.woocommerce-product-gallery__wrapper').children('div').each(function(){
    var imgTitle = jQuery(this).find('a').find('img.wp-post-image').attr('title');
    console.log(imgTitle);
    imgtitles.push(imgTitle);
});
if( jQuery('ol.flex-control-nav').length && jQuery('ol.flex-control-nav').children().length>1 ){
    for(i=0; i<imgtitles.length; ++i){
        jQuery('ol.flex-control-nav li:nth-child('+(i+1)+')').append('<span class="flexthum-title">'+imgtitles[i]+'</span>');
    }
}
}});

谢谢!

1 个答案:

答案 0 :(得分:1)

jQuery(window).load(function(){
if( jQuery('body').hasClass('single-product') ){
var imgtitles = [];
jQuery('.woocommerce-product-gallery__wrapper').children('div').each(function(){
    var imgTitle = jQuery(this).find('a').find('img').attr('data-caption');
    console.log(imgTitle);
    imgtitles.push(imgTitle);
});
if( jQuery('ol.flex-control-nav').length && jQuery('ol.flex-control-nav').children().length>1 ){
    for(i=0; i<imgtitles.length; ++i){
        jQuery('ol.flex-control-nav li:nth-child('+(i+1)+')').append('<span class="flexthum-title">'+imgtitles[i]+'</span>');
    }
}
}});
相关问题